

You are given a Binary Matrix A of dimensions N*M. You are standing at the top left corner of the matrix and want to reach the bottom right corner of the matrix. You are only allowed to move rightwards or downwards. If the value of any cell is ‘1’, then you cannot move to this cell(or in other words the move to this cell is forbidden), whereas if the cell value is ‘0’, then you can freely move to this cell(or in other words the move to this cell is permissible).
You have to count the number of ways to reach the bottom right corner of the matrix. As the number of ways can be large in some cases, hence find it modulo 10^9+7.
Note :
1. It is impossible to move outside the matrix.
2. The starting point which is the top left corner of the matrix and the destination point which is the bottom right corner of the matrix is always empty, ie A[1][1] = A[N][M] = 0 (taking 1 based indexing).
3. By rightwards move it means you can go to A[x][y+1] from some cell A[x][y], if A[x][y+1] exists and moving to A[x][y+1] is not forbidden.
4. By downwards move it means you can go to A[x+1][y] from some cell A[x][y], if A[x+1][y] exists and moving to A[x+1][y] is not forbidden.
The first line of input contains an integer T, denoting the number of test cases.
The first line of each test case consists of two space-separated integers N and M denoting the number of rows and number of columns of the Binary Matrix A respectively.
The following N lines contain M space-separated booleans ( 0’s or 1’s) denoting the permissible and forbidden positions respectively, as described in the problem statement.
Output Format :
For each test case, print the number of ways to go from the top left corner to the bottom right corner of the given matrix.
Note :
You don't have to print anything, it has already been taken care of. Just Implement the given function.
1 <= T <= 10
1 <= N, M <= 1000
0 <= A[i][j] <= 1, for each cell position (i, j)
The summation of N*M for all test cases won’t exceed 10^6.
Time Limit: 1 sec
1
2 2
0 0
0 0
2
There are 2 possible paths to go from (1,1) to (N, M) which is (2,2).
ie. {(1, 1), (1, 2), (2, 2)} and {(1, 1), (2, 1), (2, 2)}.
1
2 3
0 0 1
0 0 0
2
There are 2 possible paths to go from (1, 1) to (N, M) which is (2, 3).
ie. {(1, 1), (1, 2), (2, 2), (2, 3)} and {(1, 1), (2, 1), (2, 2), (2, 3)}.
Try all possible paths and increment the counter after reaching the bottom right corner of the matrix.
Eg. For following input binary matrix:
This algorithm works like this.
O(2^(N+M)), where N and M are the given number of rows and columns of the matrix.
As there are two options from each cell, either to move towards the right or going downwards. Also, the maximum number of cells we go through in our path will be N+M-1.
O(1).
As we are using constant extra memory.