Rat Chases Its Cheese

Scanner s= new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();

	char maze[][]=new char[n][m];

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			maze[i][j]=s.next().charAt(0);
		}
	}
	int soln[][]=new int[n][m];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			soln[i][j]=0;
		}
	}
	boolean ans=ratInMaze(maze , soln, 0,0,n-1,m-1);
	if(ans==false)
	{
		System.out.println("No Path");
	}
	

}
public static boolean ratInMaze(char maze[][] , int soln[][] , int i , int j ,int n , int m)
{
	if(maze[i][j]=='X')
	{
		return false;
	}
	if(i==n&&j==m)
	{
		soln[n][m]=1;
		for(int k=0;k<=n;k++)
		{
			for(int l=0;l<=m;l++)
			{
				System.out.print(soln[k][l]+" ");
			}
			System.out.println();
		}
		return true;
	}
	if(i>n||j>m||i<0||j<0)
	{
		return false;
	}
	boolean right=false , down=false , up=false ,left=false;
	soln[i][j]=1;
	if(maze[i+1][j]==0)
	{
		down=ratInMaze(maze , soln, i+1 , j , n , m);
	}
	else if(maze[i][j+1]==0)
	{
		right=ratInMaze(maze , soln ,i,j+1 , n ,m);
	}
	else if(maze[i-1][j]==0)
	{
		up=ratInMaze(maze , soln , i-1 ,j, n, m);
	}
	else if(maze[i][j-1]==0)
	{
		left=ratInMaze(maze , soln , i , j-1 , n ,m);
	}
	if(right||left||up||down)
	{
		return true;
	}
	soln[i][j]=0;
	return false;

This is my code. But it is not working. Infinite loop is running when i give input to the program