Function to find the maximum distance
static int maxDistance ( int [ , ] grid ) { if ( grid . GetLength ( 0 ) == 0 ) return - 1 ; int N = grid . GetLength ( 0 ) ; int INF = 1000000 ; grid [ 0 , 0 ] = grid [ 0 , 0 ] == 1 ? 0 : INF ;