Initializing left and up array .
for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { left [ i ] [ j ] = max ( 0LL , ( j ? left [ i ] [ j - 1 ] : 0LL ) ) + arr [ i ] [ j ] ; up [ i ] [ j ] = max ( 0LL , ( i ? up [ i - 1 ] [ j ] : 0LL ) ) + arr [ i ] [ j ] ; } }