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