QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 100

#3777. Four-Coloring of Maps

統計

There is a grid map with $R$ rows and $C$ columns, where each country is a 4-connected region. Your task is to color the map using four colors: red, green, blue, and yellow, such that no two adjacent countries have the same color.

Coloring alone is boring, so you want to invite your girlfriend to help—you will color with red and green, and she will color with blue and yellow. Of course, a gentleman would not let his girlfriend get overworked, so she only needs to color at most 5 countries (exactly 5 is also fine).

Your task is to count the number of ways to color the map. Note that each of the four colors must be used at least once.

Input

The input contains no more than 100 test cases. The first line of each test case contains two integers $R$ and $C$ ($1 \le R, C \le 20$), representing the number of rows and columns of the grid. The following $R$ lines each contain $C$ uppercase letters. A 4-connected region of identical letters represents a country. It is guaranteed that the number of countries does not exceed 30, and for most test cases, the number of countries is relatively small.

Output

For each test case, output the test case number and the number of coloring schemes.

Examples

Input 1

2 4
AABB
BBAA
1 5
ABABA
4 7
AABAABB
ABBCCCB
BBAACBB
CCABBAC

Output 1

Case 1: 24
Case 2: 144
Case 3: 3776

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.