Go to the adjacent cells
for ( int i = 0 ; i < 4 ; i ++ ) { int newX = x + dx [ i ] ; int newY = y + dy [ i ] ; if ( is_valid ( newX , newY , matrix ) ) { q . push ( { newX , newY } ) ; visited [ newX ] [ newY ] = true ; } } } mp [ COUNT ] ++ ; } } } }