Past two : bottom right to top left
for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 2 ; j >= 0 ; j -- ) { grid [ i , j ] = Math . Min ( grid [ i , j ] , Math . Min ( grid [ i + 1 , j ] + 1 , grid [ i , j + 1 ] + 1 ) ) ; maxi = Math . Max ( grid [ i , j ] , maxi ) ; } } return maxi == 0 ? - 1 : maxi ; }