You are given a 2-D binary matrix "Mat" of dimensions N x M consisting only of 0s and 1s. The cell consisting of 0 means that the cell is blocked and it cannot be visited whereas a cell with 1 as a value means it can be visited.
You are given a source cell and a destination cell. You need to find the length of the longest possible path from source to destination, given you can only move in 4 possible directions north(i.e from (i,j) to (i-1,j)), south(i.e from (i,j) to (i+1,j)), east(i.e from (i,j) to (i,j-1)), and west(i.e from (i,j) to (i,j+1)), and without visiting a cell twice.
Note:1. If the move isn’t possible you remain in that cell only.
The first line of the input contains two space-separated integers 'N' and ‘M’, denoting the number of rows and columns of the matrix respectively.
Next, there will be N lines each containing M space-separated integers denoting the description of the matrix.
The next line contains two space-separated integers ‘Sx’, ‘Sy’ denoting the indexes of the source cell
The next line contains two space-separated integers ‘Dx’, ‘Dy’ denoting the indexes of the destination cell
Output Format:
If a path exists then print the length of the longest possible path from source to destination.
Otherwise, print -1.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= N, M <= 12 , such that N+M <=12
0 <= Mat[i][j] <= 1 ,
0 <= Sx, Dx <= N-1
0 <= Sy, Dy <= M-1
Where Mat[i][j] is the value at position i,j in the matrix.
3 3
1 1 0
0 1 1
1 0 1
0 0
2 2
4
The only and longest path is : (0,0) -> (0,1) -> (1,1) -> (1,2) -> (2,2)

3 3
0 1 0
0 1 1
1 0 1
0 0
2 2
-1
Use a backtracking solution while keeping track of visited cells
The algorithm will be like this:
O(n+m)!) where ‘n’ and ‘m’ are dimensions of the given matrix.
The total number of paths from the top-left corner to reach the bottom-right corner of a matrix is (N+M-2) C (N-1).
O(n*m) where ‘n’ and ‘m’ are the dimensions of the given matrix.
Since the only additional space, we are using is while building the visited matrix, which has NxM cells, thus complexity is O(N*M)