Select the path we are going to follow
for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = Math . Max ( sum , b [ i , j ] + b [ i - 1 , j ] ) ; b [ i , j ] = Math . Max ( b [ i , j ] , b [ i - 1 , j ] + arr [ i , j ] ) ; } } return sum ; }