A game is performed on a board with rows and columns. The pieces are placed on the intersections. Let the upper left corner be and the lower right corner be .
Two players respectively use black pieces and white pieces.
Each piece has a color and a level. Let be the color of piece and let be the level of piece . Also, an edge on the board has one of the states explained later. The state of the -th edge is .
In a player's turn, he can choose any of his own pieces and move that piece along the edges to another intersection. This is called a move. Formally, a move is the following process: Choose a sequence of coordinates where is a positive integer at the player's choice, is the initial position of the piece, is the final position the piece reaches, satisfying:
- For any , and must be connected directly by an edge. In other words, players must move the pieces along the edges.
- For any , we must have . In other words, a piece cannot pass through the same intersection twice. In particular, , or in other words, it is not possible to stay at the origin or return to the origin.
- For any , there cannot be any piece on . In other words, in a move a piece cannot pass through pieces that are already present.
- If there are no pieces on , we say the move is a normal move. Otherwise, we say it is a capture. In a capture, assume the piece that is currently being moved has color and level , and the piece that is going to be captured has color and level , then we must have and . In other words, we can only capture a piece that has a different color and has a level that is no greater than the piece being moved.
- It shall be noted that by the above definition, moving the piece after a capture is not allowed.
The states of an edge are described below:
- If , then the edge is not accessible. It is not possible to move through the edge in a move.
- If , then the edge is a normal edge. In each move, a piece can pass through at most one normal edge.
- If , then the edge is a no-turn edge. In each move, the piece can pass through an arbitrary number of no-turn edges, but the piece can only move horizontally or vertically and the piece cannot make a turn. So for example, it is possible to move along a non-turn edge to go from to via , but it is not possible to move along a non-turn edge to go from to via .
- If , then the edge is an interchange edge. In each move, a piece can move through an arbitrary number of interchange edges, and it is possible to take turns.
- Finally, in a move, the states of all the edges a piece goes through must be the same. So for example, it is not possible to start from , go to via a no-turn edge, and go to via an interchange edge.
Now initially, the chessboard is empty. In each turn, a piece will be put at some empty intersection of the chessboard, and you need to compute how many intersections on the chessboard are accessible if we move the new piece. Please note that the piece will not be actually moved.
Each test set has multiple test cases.
In the first line, there is a positive integer denoting the number of test cases.
For each test case,
- In the first line, there are three positive integers denoting the number of rows, the number of columns, and the number of rounds.
- In the following lines, each line contains a string of length . Each character in the string is one from . The -th character on the -th line denotes the state of the edge from intersection to intersection .
- In the following lines, each line contains a string of length . Each character in the string is one from , and the -th character on the -th line denotes the state of the edge from intersection to intersection .
- In the following lines, each line has 4 non-negative integers , denoting in round , there is a piece with color and level that is put at intersection . denotes the piece is black, denotes the piece is white. It is guaranteed that before this round there are no pieces at intersection .
For each test case, output lines with a non-negative integer on each line denoting the number of intersections that can be reached by the -th piece on the chessboard.
Sample Input 1
1 3 3 5 13 22 23 010 233 0 1 2 3 1 2 2 1 1 3 1 2 0 2 3 2 1 3 2 2
Sample Output 1
4 3 3 3 2
Explanation for Sample 1
The intersections piece 1 can reach are .
The intersections piece 2 can reach are .
The intersections piece 3 can reach are .
The intersections piece 4 can reach are .
The intersections piece 5 can reach are .
Sample Input 2
2 2 3 4 22 33 123 0 2 1 2 0 1 2 1 1 2 1 3 0 3 2 2 3 2 3 3 1 3 32 32 0 2 1 2 1 2 3 2 0 1 2 2
Sample Output 2
3 4 4 2 5 5 1
Additional samples can be found here.
For all test data, , , , , , , , .
|Test case||Special properties|
|7-8||No no-turn edges or interchange edges|
|9-11||No interchange edges|
|12-14||No no-turn edges|
Since the input is large, using efficient I/O methods is recommended.