Traverse the matrix
for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i , j ] == 1 ) { q . Enqueue ( new pair ( i , j ) ) ; height [ i , j ] = 0 ; vis [ i , j ] = 1 ; } } }