Initializing left and up array .
for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT left [ i ] [ j ] = ( max ( 0 , ( left [ i ] [ j - 1 ] if j else 0 ) ) + arr [ i ] [ j ] ) NEW_LINE up [ i ] [ j ] = ( max ( 0 , ( up [ i - 1 ] [ j ] if i else 0 ) ) + arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT