P27950 [洛谷] [CCPC 2023 Beijing Regional] Tricolor
省选
题目描述

Problem Description

I have a palette consisting of $n$ rows and $m$ columns, forming $n \times m$ grids, each of which is to be filled with a flower. There are 3 colors of flowers to choose from, denoted by $0, 1, 2$.

Each flower looks at the flowers around it and wants to change its appearance. If at any moment, a flower of color $c$ has at least one adjacent flower (up, down, left, or right) of color $c-1$, then this flower will change to color $c-1$ in the next moment; otherwise, it will remain color $c$. Color is considered modulo 3.

For an initial arrangement of flowers on the palette, if all flowers eventually become the same color after a finite number of moments, we call this arrangement beautiful.

It is easy to see that for a beautiful arrangement, each flower has an earliest moment at which it no longer changes color. We call this moment the flower's stable moment. We start timing from moment $0$, so if a flower never changes color, its stable moment is $0$.

Now, I have already placed some flowers in certain grids of the palette, and some grids are empty. I want to know how many ways there are to fill the remaining grids such that the arrangement is beautiful. Also, I want to know the sum of the stable moments of the flower in the top-left grid for all these beautiful arrangements.

You only need to provide the results modulo $998244353$.

Input Format

The first line of input contains two positive integers $n, m$ ($2 \le n \le 5$, $2 \le m \le 50$).

The next $n$ lines, each containing $m$ integers, describe the state of each grid. The $i$-th line and $j$-th integer $a_{i,j} \in \{0, 1, 2, 3\}$ represent the state of the corresponding grid. $a_{i,j} \in \{0, 1, 2\}$ indicates there is a flower of that color, and $a_{i,j} = 3$ indicates the grid is empty.

Output Format

Output one line with two integers, representing the number of beautiful arrangements and the sum of the stable moments of the flower in the top-left grid.

Sample Input and Output

Sample Input #1

2 2
1 0
3 2

Sample Output #1

1 2

Sample Input #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

Sample Output #2

50830224 170059345

Notes

[Sample Explanation 1]

The arrangement only ends when the unknown grid is filled with a flower of color $0$, and after two moments, all flowers become color $2$. At this point, the flower in the top-left grid changes to color $2$ and no longer changes, so its stable moment is $2$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 2 1 0 3 2
样例输出
1 2