Four coloring of a map

You have a color map of an ancient kingdom. The map is a grid with R rows and C columns; each region is a set of 4-connected cells. Currently, adjacent regions are painted with different colors, but different regions may use the same color. You decided to re-color it with 4 col- ors: RED, GREEN, BLUE and YELLOW, according to the same rule (adjacent regions have different colors). Coloring by yourself is tedious, so you invited your girl- friend to work with you. You paint with RED and GREEN, and your girlfriend paints with BLUE and YELLOW. You don’t want your girlfriend to be tired, so you ask her not to paint more than 5 regions (painting exactly 5 regions is ok). Your task is to count the number of different maps that could be painted. Note that each color must be used at least once. Input There will be at most 100 test cases. Each case begins with two integers R and C in the first line (1 ≤ R, C ≤ 20), the number of rows and columns in the map. Each of the next R lines contains C upper-case letters, the current color of each cell. There will be at most 30 regions in each map, and most test cases have fewer regions. Output For each test case, print the case number and the number of ways to color the map. Sample Input 24 AABB BBAA 15 ABABA 47 AABAABB ABBCCCB BBAACBB CCABBAC Sample Output Case 1: 24 Case 2: 144 Case 3: 3776