Codeforces Round 1111 (Div. 2)
F
Paths on a Grid
Codeforces
2900
原题
← E→

Paths on a GridAI (CI翻译)

You are given a grid $a$ of size $n \times m$. The rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. Each cell of the grid is either blocked or free. Cells $(1, 1)$ and $(n, m)$ are free.

{"titleZh":"网格上的路径","bodyZh":"你得到了一个大小为 $n \\times m$ 的网格 $a$。行从上到下编号为 $1$ 到 $n$,列从左到右编号为 $1$ 到 $m$。网格中的每个单元格要么被阻塞,要么是空闲的。单元格 $(1, 1)$ 和 $(n, m)$ 均为空闲。AI (CI翻译)

A set $S$ of cells of $a$, which may include blocked cells, is called good if the following conditions hold:

网格 $a$ 的一个单元格集合 $S$ 可以包含被阻塞的单元格,称为良好的,当且仅当满足以下条件:AI (CI翻译)

  • $S$ is non-empty;
  • for every cell $(i, j)$ belonging to $S$, every path from $(1, 1)$ to $(n, m)$ that passes only through free cells, moves one cell down or one cell right at each step, and passes through $(i, j)$ also passes through all other cells of $S$.
  • $S$ 非空;
  • 对于属于 $S$ 的每个单元格 $(i, j)$,从 $(1, 1)$ 到 $(n, m)$ 的每一条路径——该路径只经过空闲单元格,每一步向下或向右移动一个单元格,并且经过 $(i, j)$——也会经过 $S$ 中的所有其他单元格。
AI (CI翻译)

Count the number of good sets of cells of $a$ modulo $998\,244\,353$.

请计算网格 $a$ 的良好单元格集合数量,答案对 $998\\,244\\,353$ 取模。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多组测试用例。第一行包含测试用例数量 $t$($1 \\le t \\le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \cdot m \le 10^6$).

每个测试用例的第一行包含两个整数 $n$ 和 $m$($1 \\le n \\cdot m \\le 10^6$)。AI (CI翻译)

The $i$-th of the following $n$ lines contains a string $a_{i, 1} a_{i, 2} \ldots a_{i, m}$ ($a_{i, j} \in \{0, 1\}$) — the $i$-th row of the grid. If $a_{i, j} = 1$, then cell $(i, j)$ is free; otherwise, it is blocked. It is guaranteed that $a_{1, 1} = a_{n, m} = 1$.

接下来的 $n$ 行中,第 $i$ 行包含一个字符串 $a_{i, 1} a_{i, 2} \\ldots a_{i, m}$($a_{i, j} \\in \\{0, 1\\}$),表示网格的第 $i$ 行。若 $a_{i, j} = 1$,则单元格 $(i, j)$ 为空闲;否则被阻塞。保证 $a_{1, 1} = a_{n, m} = 1$。AI (CI翻译)

It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$.

保证所有测试用例的 $n \\cdot m$ 之和不超过 $10^6$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the answer to the problem modulo $998\,244\,353$.

对于每个测试用例,输出一个整数,表示问题的答案,结果对 $998\\,244\\,353$ 取模。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first example, the only non-empty set of cells is $\{(1, 1)\}$, and it is good. Therefore, the answer is $1$.

在第一个样例中,唯一的非空单元格集合是 $\{(1, 1)\}$,并且它是良好的。因此,答案为 $1$。AI (CI翻译)

In the third example, there is no path from $(1, 1)$ to $(2, 2)$ that passes only through free cells. Therefore, every non-empty set of cells is good, so the answer is $2^4 - 1 = 15$.

在第三个样例中,不存在一条从 $(1, 1)$ 到 $(2, 2)$ 且只经过空闲单元格的路径。因此,每个非空集合都是良好的,答案为 $2^4 - 1 = 15$。AI (CI翻译)

In the fifth example, the set $\{(2, 2), (3, 2)\}$ is good because every path from $(1, 1)$ to $(4, 4)$ that passes only through free cells and passes through either of these cells also passes through the other. On the other hand, the set $\{(3, 3), (4, 3)\}$ is not good: the path $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4) \rightarrow (4, 4)$ passes through $(3, 3)$ but does not pass through $(4, 3)$. It can be shown that the total number of good sets is $162$.

在第五个样例中,集合 $\{(2, 2), (3, 2)\}$ 是良好的,因为从 $(1, 1)$ 到 $(4, 4)$ 的每一条只经过空闲单元格且经过其中任意一个单元格的路径,也都会经过另一个单元格。另一方面,集合 $\{(3, 3), (4, 3)\}$ 不是良好的:路径 $(1, 1) \\rightarrow (2, 1) \\rightarrow (2, 2) \\rightarrow (3, 2) \\rightarrow (3, 3) \\rightarrow (3, 4) \\rightarrow (4, 4)$ 经过了 $(3, 3)$,但没有经过 $(4, 3)$。可以证明,良好集合的总数为 $162$。"}AI (CI翻译)

样例

Input #1
6
1 1
1
2 2
11
11
2 2
10
01
2 2
11
01
4 4
1011
1101
0111
1111
1 32
10010110010001010110011111010011
Output #1
1
5
15
8
162
301989883
InputCopy OutputCopy
题目信息
题号
2247F
时间限制
3 s
内存限制
1024 MB
标签
数据结构
动态规划
哈希

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。