Problem in my code

Can you please tell what is problem in my code for the challenge Rat in A Maze.

import java.util.*;

import java.io.*;

public class Main {

private static boolean flag=false;

private static void getPath(char[][] grid,int row,int col, int[][] path){
	if(row==grid.length-1 && col==grid[0].length-1){
		path[row][col]=1;
		flag=true;
		for(int i=0;i<=row;i++){
			for(int j=0;j<=col;j++){
				System.out.print(path[i][j]);
			}
			System.out.println();
		}
		return;
	}
	
	if(row==grid.length || col==grid[0].length || grid[row][col]=='X'){
		return;
	}
	path[row][col]=1;
	getPath(grid,row,col+1,path);
	if(!flag){
		getPath(grid,row+1,col,path);
		path[row][col]=0;			
	}else
		return;
}
public static void main(String[] args) throws IOException {
	Scanner cin=new Scanner(System.in);
	InputStreamReader isr=new InputStreamReader(System.in);
	BufferedReader in=new BufferedReader(isr);
	int n=cin.nextInt();
	int m=cin.nextInt();
	if(n>=1 && m>=1 && n<=1000 && m<=1000){
		char[][] grid=new char[n][m];
		for(int i=0;i<n;i++){
			char[] cc=in.readLine().toCharArray();
			for(int j=0;j<m;j++){
				if(cc[j]=='X' || cc[j]=='O'){
				grid[i][j]=	cc[j];	
				}
			}
		}
		getPath(grid,0,0,new int[n][m]);
     }
}

}

@Keshav,
https://ide.codingblocks.com/s/237527 code. The input format has an error. Don’t use both the bufferreader and scanner together.

Also print -1 if there is not path.

Suggested approach:

a) Mark current cell in solution matrix as 1.
b) Move forward in the horizontal direction and recursively check if this move leads to a solution.
c) If the move chosen in the above step doesn’t lead to a solution then move down and check if this move leads to a solution.
d) If none of the above solutions works then unmark this cell as 0 (BACKTRACK) and return false

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.