Bob's Vacuum

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 4.0s
Memory limit: 512M

Problem types

Bob just bought a robot vacuum. His house is structured as an n \times m grid, with coordinates ranging from (1, 1) to (n, m).

Each night, Bob places the robot at position (1, 1) and starts it. The robot follows a predefined path, moving only right (increasing column by 1) or down (increasing row by 1). The robot stops when it returns to (1, 1).

To make this possible, Bob installed wrap-around passages in his house:

  • If the robot is at (i, m) and moves right, it will be transported to (i, 1) (i.e., wraps around horizontally).
  • If the robot is at (n, j) and moves down, it will be transported to (1, j) (i.e., wraps around vertically).

Bob hopes to design paths where the robot visits every cell exactly once before returning to (1,1). After some calculations, Bob listed all such valid paths (two paths are different if they visit the grid cells in a different order).

However, Bob got some new furnitures in his house, which are obstacles in some cells that the robot cannot pass through. Now, every previously valid path becomes blocked at some point. For a given path P, Bob defines f(P) as the number of empty cells the robot visits before hitting an obstacle. Bob wants to compute the sum of f(P) over all valid sweeping paths, given the current configuration of obstacles. Can you write a program to help him?

Input Specification

The first line of input contains two integers n and m (1 \le n, m \le 50), the dimensions of Bob's house.

Each of the following n lines contain a binary string of length m, representing the grid, where 0 means an empty cell while 1 means an obstacle.

Output Specification

Print a single integer -- the sum of f(P) over all valid full-coverage paths, \bmod 998244353

Constraints

Subtask Points Additional constraints
1 20 n, m \le 4
2 30 Every cell is an obstacle, except the cell (1, 1).
3 50 No additional constraints

Sample Input 1

2 4
0111
1111

Sample Output 1

2

Sample Input 2

2 4
0010
1000

Sample Output 2

5

Explanation for Samples

There are two valid paths for a 2 \times 4 room.

  • For the first case, the robot will only visit one cell in each path. Thus, the answer is 2.
  • For the second case, the robot will visit four cells in the first path and one cell in the second path. Thus the answer is 5.

\displaystyle \begin{array}{|c|c|c|c|}\hline
\to & \downarrow & \to & \downarrow \\\hline
\downarrow & \to & \downarrow & \to \\\hline
\end{array}\quad\begin{array}{|c|c|c|c|}\hline
\downarrow & \to & \downarrow & \to \\\hline
\to & \downarrow & \to & \downarrow \\\hline
\end{array}


Comments

There are no comments at the moment.