Rat Chases its cheese

import java.util.*;
public class Main {
public static boolean ratInAMaze(char maze[][],int[][]ans,int i,int j,int m,int n){
if(i==m && j==n){
ans[i][j]=1;
display(ans,m,n);
return true;
}
if(i>m || j>n || i<0 || j<0){
return false;
}
if(maze[i][j]==‘X’){
return false;
}
ans[i][j]=1;
boolean rightSuccess=false,downSuccess=false,topSuccess=false,leftSuccess=false;
if(i < m && ans[i+1][j]==0){
downSuccess=ratInAMaze(maze,ans,i+1,j,m,n);
}
if(j<n && ans[i][j+1]==0){
rightSuccess=ratInAMaze(maze,ans,i,j+1,m,n);
}
if(i!=0 && ans[i-1][j]==0)
{
topSuccess=ratInAMaze(maze,ans,i-1,j,m,n);
}
if(j!=0 && ans[i][j-1]==0)
{
leftSuccess=ratInAMaze(maze,ans,i,j-1,m,n);
}
if(leftSuccess || rightSuccess || topSuccess || downSuccess)
{
return true;
}
ans[i][j]=0;
return false;
}

public static void display(int[][] sol, int m, int n) {
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            System.out.print(sol[i][j] + " ");
        }
        System.out.println();
    }
}
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
	int m = sc.nextInt();
    int n = sc.nextInt();
    char[][] maze = new char[m][n];
	int ans[][]=new int[m][n];
    String [] str = sc.nextLine().split(" ");
	for(int i=0; i<m; i++)
    {
        String lineInput = sc.nextLine();
        for(int j=0; j<lineInput.length(); j++)
        {
            maze[i][j] = lineInput.charAt(j);
        }
    }
		if(!ratInAMaze(maze,ans,0,0,m-1,n-1)){	
			System.out.println("NO PATH FOUND");
		}
}

}

Only one test case shows run time error. Help me with the solution.

@jeevanshu,
Your code is absolutely correct. But one of the test cases contain a stray " " at the end of the string which is causing the error. Kindly just modify the input format a little. Rest of the code is correct.

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.