Check if the cell is within the boundaries or contains a 1
if ( x < 0 x >= M y < 0 y >= N grid [ x , y ] > 0 ) continue ; q . Enqueue ( new pair ( x , y ) ) ; grid [ x , y ] = 1 ; } } ++ ans ; } return ans ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; }