#P15781. [JAG 2025 Summer Camp #3] Grid Painting

[JAG 2025 Summer Camp #3] Grid Painting

题目描述

There is an H×WH \times W grid, where initially every cell is painted white. Let Ci,jC_{i,j} (1iH1 \leq i \leq H, 1jW1 \leq j \leq W) denote the color of the cell in the ii-th row from the top and the jj-th column from the left.

You can paint the grid by repeating the following two operations any number of times, in any order.

Operation 1: This operation may be applied only if the rightmost column of the grid is entirely white.
First, perform a cyclic right shift of the entire grid by one column (i.e., for all 1iH1 \leq i \leq H, 1jW1 \leq j \leq W, replace Ci,(jmodW)+1C_{i, (j \mod W) + 1} with Ci,jC_{i, j} simultaneously).
Then, choose 1lrH1 \leq l \leq r \leq H and paint Cl,1,Cl+1,1,,Cr,1C_{l, 1}, C_{l+1, 1}, \ldots, C_{r, 1} black.

Operation 2: This operation may be applied only if the bottom row of the grid is entirely white.
First, perform a cyclic downward shift of the entire grid by one row (i.e., for all 1iH1 \leq i \leq H, 1jW1 \leq j \leq W, replace C(imodH)+1,jC_{(i \mod H) + 1, j} with Ci,jC_{i, j} simultaneously).
Then, choose 1lrW1 \leq l \leq r \leq W and paint C1,l,C1,l+1,,C1,rC_{1, l}, C_{1, l+1}, \ldots, C_{1, r} black.

Given the final grid after performing some operations, determine the number of operation sequences that could result in it. Since the answer can be very large, output it modulo 998244353=119×223+1998244353 = 119 \times 2^{23} + 1.

Two operation sequences are considered different if they have different lengths, or if at any step either the shift direction or the segment painted black differs.

:::align{center} :::

输入格式

The input consists of multiple test cases.

The first line contains an integer TT (1T1001 \leq T \leq 100), representing the number of test cases.

TT test cases follow. Each test case is given in the following format.

$$\begin{aligned} & H \ W \\ & S_1 \\ & \vdots \\ & S_H \end{aligned} $$

For each test case, the first line contains integers HH (1H1001 \leq H \leq 100) and WW (1W1001 \leq W \leq 100), representing the height and width of the grid, respectively.

Each of the following HH lines contains a string SiS_i of length WW, representing the final grid, consisting only of the characters ‘#’ and ‘.’. If the jj-th character of SiS_i is ‘#’, then Ci,jC_{i,j} is black, and if it is ‘.’, then Ci,jC_{i,j} is white.

输出格式

Output one line per test case: the number of possible operation sequences modulo 998244353998244353.

4
2 2
.#
##
2 3
...
...
3 4
#.#.
.#.#
#.#.
6 5
###..
###..
##.##
##.##
..###
..###
6
1
0
210