Function to find the minimum number of moves to traverse the given matrix
static int solve ( int [ , ] A , int N , int M ) { int [ , ] B = new int [ N , M ] ; int c = 1 ; HashSet < int > s = new HashSet < int > ( ) ;