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