Function checks if a cell is valid , i . e . it is inside the grid and equal to 1
bool is_valid ( int x , int y , int matrix [ n ] [ m ] ) { if ( x < n && y < m && x >= 0 && y >= 0 ) { if ( visited [ x ] [ y ] == false && matrix [ x ] [ y ] == 1 ) return true ; else return false ; } else return false ; }