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