#P15060. 过河卒

过河卒

Background

It is recommended to be rated Brown.

Problem Description

Yuki has a chessboard with (n+2)(n+2) rows and (m+2)(m+2) columns. The row indices are 0∼n+10 \sim n+1, and the column indices are 0∼m+10 \sim m+1.

The cell in row ii and column jj is denoted by (i,j)(i,j). Each cell can be either white or black, denoted by si,js_{i,j}. If si,j=0s_{i,j}=\texttt0, it is white; if si,j=1s_{i,j}=\texttt1, it is black. It is guaranteed that the outermost ring of the board is white, i.e., s0,i=sn+1,i=si,0=si,m+1=0s_{0,i}=s_{n+1,i}=s_{i,0}=s_{i,m+1}=\texttt0 is guaranteed.

Yuki plans to place some rooks on the board. For a cell (i,j)(i,j), it is called safe if and only if there exists at least one rook in row ii or column jj, and there is no rook on cell (i,j)(i,j).

Yuki has the following requirements for a rook placement:

  • All rooks are placed on black cells.
  • No two rooks are in the same row or the same column.
  • A pawn starts from row 00 of the board and cannot reach row n+1n+1 while passing only through safe cells.

The pawn moves as follows: suppose the pawn is currently at (i,j)(i,j). Then it can move to any one of (i+1,j)(i+1,j), (i,j−1)(i,j-1), (i,j+1)(i,j+1), as long as the destination is inside the board.

You need to help Yuki compute the number of placements that satisfy the conditions. Since the answer may be large, output the result modulo 109+710^9+7.

Input Format

This problem contains multiple test cases.

The first line of input contains two positive integers t,ct,c, representing the number of test cases and the test point ID. The sample satisfies c=0c=0.

For each test case:

  • The first line contains two positive integers n,mn,m.
  • The next nn lines describe the board. Line ii contains a 01\texttt 01 string of length mm, si,1,…,si,ms_{i,1},\dots,s_{i,m}.

Output Format

For each test case, output one line containing one integer, the answer.

3 0
2 2
11
00
2 2
01
01
3 3
100
000
001
3
3
4

Hint

Explanation of Sample 1

This sample has 33 test cases.

For test case 11, the 33 valid placements are:

  • Place no rooks.
  • Place a rook at (1,1)(1,1).
  • Place a rook at (1,2)(1,2).

For test case 22, the 33 valid placements are:

  • Place no rooks.
  • Place a rook at (1,2)(1,2).
  • Place a rook at (2,2)(2,2).

For test case 33, the 44 valid placements are:

  • Place no rooks.
  • Place a rook at (1,1)(1,1).
  • Place a rook at (3,3)(3,3).
  • Place rooks at (1,1),(3,3)(1,1),(3,3).

Sample 2

See zu2.in\boldsymbol{zu2.in} and zu2.ans\boldsymbol{zu2.ans} in the additional files.

This sample has 33 test cases.

In it, test case 11 satisfies n,m≤4n,m \le 4, test case 22 satisfies n≤100n \le 100, m≤4m \le 4, and test case 33 satisfies n≤200n \le 200, m≤8m \le 8.

Sample 3

See zu3.in\boldsymbol{zu3.in} and zu3.ans\boldsymbol{zu3.ans} in the additional files.

This sample has 33 test cases. All testdata in this sample satisfies si,j=1s_{i,j}=1.

In it, test case 11 satisfies n,m≤80n,m \le 80, test case 22 satisfies n,m≤300n,m \le 300, and test case 33 satisfies n,m≤1500n,m \le 1500.

Sample 4

See zu4.in\boldsymbol{zu4.in} and zu4.ans\boldsymbol{zu4.ans} in the additional files.

This sample has 33 test cases.

In it, test case 11 satisfies n,m≤80n,m \le 80, test case 22 satisfies n,m≤500n,m \le 500, and test case 33 satisfies n,m≤3000n,m \le 3000.

Constraints

For all testdata, it is guaranteed that:

  • 1≤t≤31 \le t \le 3.
  • 1≤n,m≤30001 \le n,m \le 3000.
  • si,j∈{0,1}s_{i,j} \in \{\texttt0,\texttt1\}.

For test points where c\boldsymbol c is odd, it is guaranteed that n=m\boldsymbol{n=m}.

::cute-table{tuack}

Test Point ID n≤n \le m≤m \le Special Property
1∼41 \sim 4 100100 44 No
5∼85 \sim 8 200200 88
9,109,10 11 15001500
11,1211,12 15001500 11
13,1413,14 8080 Yes
15,1615,16 300300
17,1817,18 15001500
19∼2119 \sim 21 8080 No
22,2322,23 500500
24,2524,25 30003000

Special Property: for all i∈[1,n],j∈[1,m]i \in [1,n],j \in [1,m], it is guaranteed that si,j=1s_{i,j}=\texttt1.

Translated by ChatGPT 5