Directions traversable from a given a particular cell
int dirs [ 4 ] [ 2 ] = { { 0 , 1 } , { 1 , 0 } , { 0 , -1 } , { -1 , 0 } } ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) q . emplace ( i , j ) ; } }