Generate prefix sum of the matrix
prefsum = [ [ 0 for x in range ( m ) ] for y in range ( m ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for x in range ( m ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT prefsum [ i ] [ x ] = grid [ i ] [ x ] NEW_LINE DEDENT else : NEW_LINE INDENT prefsum [ i ] [ x ] = ( prefsum [ i ] [ x - 1 ] + grid [ i ] [ x ] ) NEW_LINE DEDENT DEDENT DEDENT