Test case 3 wrong answer

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int m = sc.nextInt();
	char[][] g = new char[n][m];
	for (int i = 0; i < n; i++) {
		String s = sc.next();
		for (int j = 0; j < m; j++) {
			g[i][j] = s.charAt(j);
		}
	}
	solve(g, 0, 0, new int[n][m]);
}

public static void solve(char[][] g, int row, int col, int[][] visited) {
	if (row == g.length && col == g[0].length) {
		System.out.println("-1");
		System.exit(0);
	}
	if (row == g.length - 1 && col == g[0].length - 1) {
		visited[row][col]=1;
		for (int i = 0; i < g.length; i++) {
			for (int j = 0; j < g[0].length; j++) {
				System.out.print(visited[i][j] + " ");
			}
			System.out.println();
		}
		System.exit(0);		
	}
	if (col == g[0].length || row == g.length || visited[row][col] == 1 || g[row][col] == 'X') {
		return;
	}
	visited[row][col] = 1;
	solve(g, row, col + 1, visited);
	solve(g, row + 1, col, visited);
	visited[row][col] = 0;
}

if going towards right works then simply return true otherwise execute next statement for going down

Not able to get your point, can you please help in my code

do this :slightly_smiling_face:

bool rightsucess=solve(g, row, col + 1, visited);
   if(rightsucess){
         return true;
   }
   bool downsucess=solve(g, row + 1, col, visited);
   visited[row][col]=0;
   if(downsucess)
           return true;
   return false;