Rat chase its cheese

even after checing the editorial, the testcases are giving run error or sometines wrong answer

@minal.251298 the editorial code passes all the test cases

could you please check my code??
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
char[][] maze= new char[n][m];
int[][] ans= new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
maze[i][j]=sc.next().charAt(0);
}
}
chaseCheese(maze,n,m,0,0,ans);
if(k==false)
{
System.out.print("-1");
}

}
static boolean k=false;
public static void chaseCheese(char[][]maze,int n, int m, int x, int y, int[][] ans)
{
	if(x<0||x>=n||y>=m||y<0|| maze[x][y]=='X'|| ans[x][y]==1)
	{
		return;
	}
	if(x== n-1 && y== m-1)
	{
		ans[x][y]=1;
		k=true;
		for(int i=0;i<n;i++)
	    {
		for(int j=0;j<m;j++)
		{
			System.out.print(ans[i][j]+" ");
		}
		System.out.println();
	    }
	    return;
	}
	
	ans[x][y]=1;
	chaseCheese(maze,n,m,x,y+1,ans);
	chaseCheese(maze,n,m,x,y-1,ans);
	chaseCheese(maze,n,m,x+1,y,ans);
	chaseCheese(maze,n,m,x-1,y,ans);
	ans[x][y]=0;
}

}

Please Do check my code too…!!
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];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
maze[i][j] = sc.next ().charAt (0);
}
}

chase (maze, n, m, 0, 0, new int[n][m]) ;
if (flag == false)
  {
System.out.print ("NO PATH FOUND");
  }

}

static boolean flag=false;

public static void chase (char[][]maze, int n, int m, int row, int col, int[][]path)
{

if (row < 0 || row >= n || col < 0 || col >= m || path[row][col] == 1 || maze[row][col] == 'X')
  {
return;
  }

if (row == n - 1 && col == m - 1)
{
    
    path[row][col] = 1;
    flag = true;
    for (int i = 0; i < n; i++)
   {
        for (int j = 0; j < m; j++)
      {
    	System.out.print (path[row][col] + " ");
      }
    System.out.println ();
  }
    return;
}

path[row][col] = 1;
chase (maze, n, m, row, col + 1, path);
chase (maze, n, m, row, col - 1, path);
chase (maze, n, m, row + 1, col, path);
chase (maze, n, m, row - 1,col, path);
path[row][col]=0;

}
}

@minal.251298 maze[i][j] = sc.next ().charAt (0); here each j loop takes in a new input and then goes for it 0th char.so u r getting NoSuchElementException coz the loop is running more than reqd.
instead read a string for each i i.e row and then iterate over the strings char
for (int i = 0; i < n; i++) {
String ans = scn.next();
for (int j = 0; j < m; j++) {
maze[i][j] = ans.charAt(j);
}
}

also please save ur code on ide.codingblocks.com anthen share it always.its v diff to understand such raw code

THANKYOU, ITS SOLVED NOW