Facing problem in RatMaze problem using backtracking

I am solving the Ratmaze problem where a rat has to reach from the index (0,0) to (N-1,M-1).
PROBLEM:-
You are given an N*M grid. Each cell (i,j) in the grid is either blocked, or empty. The rat can move from position (i,j), down or right on the grid.
Initially rat is on the position (1,1). It wants to reach position (N,M). Find the rightmost path through which, rat can reach this position. A path is rightmost, if the rat is at position (i,j), it will always move to (i,j+1), if there exists a path from (i,j+1) to (N,M).

Input Format

First line contains 2 integers, N and M, denoting the rows and columns in the grid. Next N line contains. M characters each. An ‘X’ in position (i,j) denotes that the cell is blocked and ans ‘O’ denotes that the cell is empty.

Constraints

1<=N,M<=1000 GRID(i,j)=‘X’ or ‘O’

Output Format

If a solution exists: Print N lines, containing M integers each. A 1 at a position (i,j) denotes that the (i,j)th cell is covered in the path and a 0 denotes that the cell is not covered in the path.
If solution doesn’t exist, just print “-1”.

Sample Input

5 4
OXOO
OOOX
OOXO
XOOO
XXOO

Sample Output

1 0 0 0
1 1 0 0
0 1 0 0
0 1 1 1
0 0 0 1
Can anyone tell me the mistake i am making due to which i am getting segmentation fault.Here is my soln https://ide.codingblocks.com/s/302738 .