RAT IN MAXE PROBLEM

Why TLE in this code???

import java.util.Scanner;

public class Main {
public static void main(String agrsp[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
char a[][]=new char[n][m];
for(int i=0;i<n;i++){
String str=sc.next();
for(int j=0;j<str.length();j++){
a[i][j]=str.charAt(j);
}
}
boolean b[][]=new boolean[n][m];
fun(a,0,0,b);

    //
    if(a[0][0]!='O'){
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
    else
        System.out.println(-1);

}
static int count=0;
static String str="";
public static void fun(char ch[][],int row,int col,boolean b[][]){
    if(col>=ch[0].length)
    {
        fun(ch,row+1,col-1,b);
        return;
    }
    if(row>=ch.length || ch[row][col]=='X')
        return;
    if(row==ch.length-1 && col==ch[0].length-1 && count==0)
    {
        b[row][col]=true;
        //System.out.println("Done");
        for(int i=0;i<b.length;i++){
            for(int j=0;j<b[0].length;j++){
                if(b[i][j])
                    ch[i][j]='1';
                else
                    ch[i][j]='0';
            }
            //System.out.println();
        }
        b[row][col]=false;
        count++;
        return;
    }
    //return;

    if(ch[row][col]!='X'){
        b[row][col]=true;
        fun(ch,row,col+1,b);
        fun(ch,row+1,col,b);
        b[row][col]=false;
    }
    else
        fun(ch,row+1,col-1,b);
}

}

hey @Anubhav44044
Optimize your solution
use Back Tracking

if N =1000
and M = 1000
Each cell (i,j) contains ‘O’
your code Gives TLE
Algo
If destination is reached print the solution matrix .
Else .
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