#P10042. [CCPC 2023 北京市赛] 三染色

[CCPC 2023 北京市赛] 三染色

Problem Description

I have a palette with nn rows and mm columns, forming n×mn \times m cells, and each cell must contain a flower. There are 33 possible colors of flowers, denoted by 0,1,20, 1, 2.

Flowers watch the surrounding flowers and want to become like other flowers. At a moment, if a flower of color cc has at least one adjacent flower (up, down, left, or right) whose color is c1c-1, then at the next moment this flower will become color c1c-1; otherwise, its color at the next moment is still cc. Here colors are considered mod3\bmod 3.

For an initial placement of flowers on the palette, if after a finite number of moments all flowers become the same color, then we call this placement good.

It is not hard to see that for a good placement, each flower has an earliest moment such that after this moment its color never changes again. We call this moment the flower's stabilization time. We start counting from moment 00, so if a flower never changes color, then its stabilization time is 00.

Now I have already placed flowers in some cells of the palette, and some cells are empty. I want to know: how many ways are there to place flowers in the remaining cells such that the resulting placement is good? Also, among these good placements, what is the sum of the stabilization times of the flower in the cell at row 11, column 11?

You only need to output these two results modulo 998244353998244353.

Input Format

The first line contains two positive integers n,mn, m (2n52 \le n \le 5, 2m502 \le m \le 50).

Then follow nn lines, each containing mm integers. The jj-th integer in the ii-th line is ai,j{0,1,2,3}a_{i,j} \in \{0,1,2,3\}, describing the state of the corresponding cell. Here ai,j{0,1,2}a_{i,j} \in \{0,1,2\} means there is a flower and its color, and ai,j=3a_{i,j} = 3 means there is no flower.

Output Format

Output one line with two integers: the number of good placements, and the sum of the stabilization times of the flower in the top-left cell.

2 2
1 0
3 2
1 2
5 5 
3 3 3 3 2
2 3 3 3 1
1 3 3 3 3
3 3 3 3 3
3 3 3 3 3
50830224 170059345

Hint

[Sample Explanation 1]

Only when the unknown cells are filled with flowers of color 00 will the process end, and after two moments all flowers' colors become 22. At this time, the flower in the top-left cell changes to color 22 and will not change anymore, so its stabilization time is 22.

Translated by ChatGPT 5