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