Regarding rat in maze

Sir why i am having the run error and what it is?

See my code:
import java.util.Scanner;
public class Main {
static int N,M;
public static void main(String[] args) {

	Scanner s=new Scanner(System.in);
	N=s.nextInt();
	M=s.nextInt();
	char maze[][]=new char[1001][1001];
	for(int i=0;i<N;i++) {
		for(int j=0;j<M;j++) {
			maze[i][j]=s.next().charAt(0);
		}
	}
	
			 
	 
	 
	
	printpath(maze);
	
	
			

}



public static boolean printpath(char maze[][]) {
	int sol[][]=new int[N][M];
	if (solvemaze(maze, 0, 0, sol) == false) { 
         printnegativeSolution();
        return false; 
    } 
	printSolution(sol);
	return true;
}


public static boolean isitsafe(char maze[][],int row,int col) {
	
	

return (row>= 0 && row < N && col >= 0 && col < M && maze[row][col] == 'O' );
}


public static boolean solvemaze(char[][] maze,int row,int col,int[][] sol){
	
	if(row==N-1 && col==M-1) {
		sol[row][col]=1;
        return true;
	
	
	}
	
	if(isitsafe(maze, row, col)==true) {
		sol[row][col]=1;
	
	
	
	if(solvemaze(maze, row, col+1,sol)) {
	
		return true;
	}
	
	
	if(solvemaze(maze, row+1, col,sol)) {
	
		return true;
	}
	
	sol[row][col]=0;
	return false;
	}		
	return false;
}




		




public static void printSolution(int sol[][]) 
{ 
    for (int i = 0; i < N; i++) { 
        for (int j = 0; j < M; j++) 
            System.out.print(sol[i][j]+" "); 
        System.out.println(); 
    } 
} 

public static void printnegativeSolution()
{
int a=-1;
System.out.println(a);
}

}

Hi Saurabh,
When you are taking input for the maze, the input format is wrong. You are taking N*M input strings, when you should be taking only N strings input of M length.

Can you please do the debuging i m not able to understand what you want to say.

In the sample input, you are given input as:
5 4
OXOO
OOOX
OOXO
XOOO
XXOO
Here, N=5 and M=4.
There are no spaces in between the chars, hence we will treat it as 1 string of M length and not M different strings.
Your code will work fine if there are spaces in between the chars of the input string, because then we will have M different strings of 1 length.

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.