Winmine.exe#3088

Winmine.exe

10 seconds
64 megabytes
Very hard

The Windows Minesweeper (WinMine) is one of the most well-known games on the Windows system. The rules are quite simple, and playing a round takes only a few minutes. First, let's briefly introduce this game (if you are already familiar with it, you may skip the next paragraph): The goal of the game is to uncover all the squares that do not contain mines (with the left mouse button) without exploding by clicking on a mine. The locations of the mines are determined through logic. Clicking on the game board will reveal what is hidden underneath the selected square or group of adjacent squares (if they are adjacent, a large number of blank squares may be revealed at once). Some squares are blank, while others contain numbers from 1 to 8, each number being the count of mines adjacent to that uncovered square. The game is won once all blank squares are uncovered without hitting any mine; any remaining mines not identified with flags are automatically flagged by the computer. A distinctive feature of Minesweeper is the element of randomness at the initial and some intermediate stages.

Wikipedia

Jaddy loves playing WinMine in his free time because it is simple and ingenious. However, sometimes he gets frustrated when he reaches some undeterminable states during the game. Consider the following state as an example:

In the red circle of this example, the two remaining squares are completely indeterminable, and it is apparent that there are two possible distributions for the remaining ONE mine. When Jaddy gets into this kind of trouble, he can only guess. Sometimes there are only two possible choices (as shown in the example), but sometimes there are more. For this reason, he needs an assistant to count the number of different possible distributions of mines based on the current state of the game board.

This assistant for the game would be very useful and interesting for him, so he immediately starts coding. Unfortunately, Jaddy spent all his time in Programming and Algorithms class playing WinMine instead of learning, so this complex task feels impossible for him. Since Jaddy loves WinMine so much, he is asking for help from you, an excellent programmer. To make the problem simpler, he has added three constraints:

  1. It is guaranteed that the given game state can always be created by making only ONE click on the initial board.

  2. In the given state, if two unrevealed squares are connected directly or through other unrevealed squares, they are also biconnected. That is, even after removing any of the other unrevealed squares, these two will remain connected. Here, two squares are considered connected only if they share an edge. This means a square can have at most four connected neighbors.

  3. In the given state, if two numbers are connected by sharing an edge directly or through other numbers, all the unrevealed cells adjacent to the connected set of numbers must also be connected.

Help Jaddy, please!

Input

There are multiple test cases. The first line of input is a positive integer (1 \leq T \leq 50) indicating the number of test cases. Then, cases follow. For each case, the first line contains three positive integers , , and (1 \leq n, m \leq 100, 1 \leq M \leq 1000), where and are the number of rows and columns of the board, and is the total number of mines on the board. Then an n \times m matrix of characters will describe the current state of the board. In this matrix, there are two types of symbols:

  1. Digits from to : this means the number of mines around this square ('' indicates a blank square).

  2. '.' (quotes for clarity): this means the square is unrevealed.

Output

For each test case, output an integer prefixed by the case number, denoting the number of possible distributions of mines in the given state, modulo . See the sample output for further details.

Examples

Example 1

Loading
Loading