Rat Chases Cheese

I’m confused where to put the condition if no path is found
please help me.

Its passing 3 cases and 2 cases are failing

import java.util.*;

public class Main {

public static void main(String[] args) {

	Scanner sc = new Scanner(System.in);
	int n =sc.nextInt();
	int m= sc.nextInt();
	char[][] maze = new char[n][m];
	sc.nextLine();
	for(int i=0;i<n;i++) {
		String str= sc.next();
		maze[i]=str.toCharArray();
	}

	ratChesse(maze, 0, 0, "", new boolean[maze.length][maze[0].length]);
}


public static void ratChesse(char[][] board, int row, int col ,String ans, boolean[][] visited) {

	//pbc
	if(row==board.length-1 && col==board[0].length-1) {
		
		visited[row][col]=true;
		for(int i=0;i<visited.length;i++) {
			for(int j=0;j<visited[0].length;j++) {
				System.out.print(boolToint(visited[i][j]) + " ");
			}
			System.out.println(); 
		}
		visited[row][col]=false;
		return;
	}


	//nbc
	if(row>=board.length || col>=board[0].length || row==-1 || col==-1 || 
			board[row][col]=='X' || visited[row][col]==true) {
		return;
	}


	visited[row][col] = true;

	//top call
	ratChesse(board, row-1, col, ans+"T", visited);

	//down call
	ratChesse(board, row+1, col, ans+"D", visited);


	//left call
	ratChesse(board, row, col-1, ans+"L", visited);

	//right call
	ratChesse(board, row, col+1, ans+"R", visited);

	visited[row][col]=false;
}

static int boolToint(Boolean b) {
	return b.compareTo(false);
}

}

@Ritik488.
I see that you have got a correct answer. Do you have any other doubts regarding the question?

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.