创建于 2026/07/18 · 更新于 2026/07/18
Rifleman is a student who majors in Computer Science , and he is playing GO now.
Consider an chessboard where each cell can be in one of three states: a black piece, a white piece, or empty. Consider a maximal four-connected component consisting only of pieces of the same color. If there is no empty cell among the cells directly adjacent to this connected component, the configuration is considered invalid(This rule is quite similar to Go).
We call a component maximal if there is no component such that is a proper subset of .
Now, given an incomplete chessboard, you need to fill each ? with one of the three states so that the resulting configuration is valid. Count the number of ways to do this. Since the number may be large, output it modulo .
The first line contains two integers and , the dimensions of the chessboard.
The next lines each contain characters, describing the chessboard. The characters mean:
B: a black piece
W: a white piece
.: an empty cell
?: a cell you need to fill
Print a integer — the answer modulo .
2 2
B?
?W
5
3 3
?W.
W?W
???
108