Test case are giving run error

#include
using namespace std;

bool ratMaze(char a[10][10], int sol[][10], int i, int j, int m, int n)
{
if (i == m && j == n)
{
sol[i][j] = 1;
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
cout << sol[i][j] << " ";
}
cout << endl;
}
return true;
}
if (i > m || j > n || i < 0 || j < 0)
{
return false;
}

if (a[i][j] == 'X')
{
    return false;
}

sol[i][j] = 1;
bool down = ratMaze(a, sol, i + 1, j, m, n);
if (down)
    return true;
bool left = ratMaze(a, sol, i, j + 1, m, n);
if (left)
    return true;
bool right = ratMaze(a, sol, i, j - 1, m, n);
if (right)
    return true;


bool up = ratMaze(a, sol, i - 1, j, m, n);

if (up)
    return true;

sol[i][j] = 0;

return false;

}

int main()
{

int m, n;
cin >> m >> n;
char a[10][10];

int sol[10][10] = {0};

for (int i = 0; i < m; i++)
{
    for (int j = 0; j < n; j++)
    {
        cin >> a[i][j];
    }
}

int i = 0, j = 0;

bool ans = ratMaze(a, sol, i, j, m - 1, n - 1);

if (ans == false)
{
    cout << -1;
}

return 0;

}

this is because you stuck in a loop
first go left then up and then right then down and so on…

you also have to handle this case

to handle this case
use this condition
if(sol[i][j]==1) return false;

what is this condition i don’t understand if(sol[i][j]==1) return false;

as i explain you
also consider the condition when ans doesn’t exist
I hope now your doubt is resolved

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.