#P13148. [GCJ 2018 #2] Gridception

[GCJ 2018 #2] Gridception

题目描述

The master thief Jom Codd is able to infiltrate the dreams of others. Since dream-viewing technology is not very good yet, Codd sees a dream as a dream grid of unit cells, each of which is white or black.

Given a starting dream grid, Codd can go deeper by replacing each white cell with a 2×22\times 2 grid of white cells, and each black cell with a 2×22\times 2 grid of black cells; this creates a new dream grid that is four times larger. He can go deeper again from that grid, and so on. For example, given this starting dream grid:

BBB
BWB
BBB

then going deeper once produces this new dream grid:

BBBBBB
BBBBBB
BBWWBB
BBWWBB
BBBBBB
BBBBBB

and going deeper again produces this new dream grid:

BBBBBBBBBBBB
BBBBBBBBBBBB
BBBBBBBBBBBB
BBBBBBBBBBBB
BBBBWWWWBBBB
BBBBWWWWBBBB
BBBBWWWWBBBB
BBBBWWWWBBBB
BBBBBBBBBBBB
BBBBBBBBBBBB
BBBBBBBBBBBB
BBBBBBBBBBBB

and so on.

Codd has just infiltrated a dream and viewed its starting dream grid. He is on a very difficult mission, and he knows that he will need to go deeper many times. To help him navigate, he is looking at various patterns in the starting dream grid. A pattern consists of a single group of cells connected by shared edges (shared corners do not count as connections), plus their colors. A pattern might contain internal gaps (as long as the pattern's cells are a single connected group); such gaps are not considered part of the pattern. Two patterns are the same if and only if they have the same number and arrangement of cells (not reflected or rotated), with the same colors.

For example, in the grids above, the following 88-cell pattern is present in the starting grid:

BBB
B B
BBB

It is not present after going deeper once, but it is present after going deeper twice, and three times, and so on for every deeper dream grid.

Codd wants to find the largest pattern from the starting dream grid that will be present in at least a googol (1010010^{100}) of deeper dream grids. For the given example, the pattern above is the largest such pattern. Even though it is not present after going deeper once, it is present in at least a googol of deeper levels. Other such patterns of smaller sizes also meet this condition, but there is no 99-cell pattern that does; the only such pattern would have to be identical to the entire starting dream grid, and that pattern will never show up in any deeper dream grid, let alone in a googol of them.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each begins with one line with two integers RR and CC: the numbers of rows and columns, respectively, in the dream grid. Each case continues with RR more lines of CC characters each; every such character is either B or W. These lines directly represent the dream grid.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the largest possible size of at least one pattern that meets Codd's requirements, as described above.

5
3 3
BBB
BWB
BBB
2 3
BBB
WBW
1 1
W
3 3
WBW
BWB
WBW
2 4
BBWW
BBWW
Case #1: 8
Case #2: 5
Case #3: 1
Case #4: 4
Case #5: 8

提示

Sample Explanation

Sample Case #1 is the one described in the problem statement.

In Sample Case #2, one possible largest pattern is:

BBB
WB

Another equally large one is:

BBB
W W

In Sample Case #3, the entire starting dream grid is a largest pattern.

In Sample Case #4, note that the five Ws would not form a valid pattern, because they are not connected. However, this is a largest pattern:

WB
BW

In Sample Case #5, the entire starting dream grid is a largest pattern. Note that even though this grid happens to be what Codd would get by going deeper starting from BW, that is irrelevant; Codd will never "go shallower".

Limits

  • 1T1001 \leq T \leq 100.

Test set 1 (10 Pts, Visible)

  • 1R31 \leq R \leq 3.
  • 1C41 \leq C \leq 4.

Test set 2 (22 Pts, Hidden)

  • 1R201 \leq R \leq 20.
  • 1C201 \leq C \leq 20.