Initialize rest table with sum
for i in range ( 1 , dim ) : NEW_LINE INDENT for j in range ( 1 , dim ) : NEW_LINE INDENT dp [ i ] [ j ] = ( mtrx [ i ] [ j ] + dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT