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