How to resolve the issue of TLE and test case 4

#include
using namespace std;

bool rat(char maze[][1000],int sol[][1000],int m,int n,int i,int j){
if(i==m && j==n){
sol[i][j]=1;
for(i=0;i<=m;i++){
for(j=0;j<=n;j++){
cout<<sol[i][j]<<" ";

        }
        cout<<endl;
    }
    cout<< endl;
    return true;
}
if(i>m || j>n){
    return false;
}
if(maze[i][j]=='X'){
    return false;
}
sol[i][j]=1;
bool right=rat(maze,sol,m,n,i,j+1);
bool down=rat(maze,sol,m,n,i+1,j);
sol[i][j]=0;
if(right || down){
    return true;
}
return false;

}
int main(){
int m,n,i,j;
cin>>m>>n;
int sol[1000][1000]={0};

char maze[1000][1000];
for(i=0;i<m;i++){
    for(j=0;j<n;j++){
        cin>>maze[i][j];
    }
    maze[i][j]='\0';


}
bool ans=rat(maze,sol,m-1,n-1,0,0);
if(ans==false){
    cout<<"-1";
}

}

hi @madhuragarwal2027_a8d8220ac96f9af4 buddy updated and commented

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.