Uniting the Three Kingdoms

No attempts yetTime limit1sMemory limit256 MB

Problem

A continent is split into three countries. The map is a grid with RR rows and CC columns, and every cell is either sea, written '.', or land, written 'X'. Land cells that touch up, down, left, or right belong to the same country, and a map holds exactly three countries.

You may fill sea cells with land. Find the smallest number of sea cells you have to fill so that the three countries become one connected stretch of land.

The picture below shows a map with R=6R = 6 and C=14C = 14. The left panel is the original map, the middle panel labels each land cell with the number of its country, and the right panel fills two sea cells, marked x, so that the three countries connect.

XXX...........  111...........  111...........
X.X.XXXX......  1.1.2222......  1.1.2222......
XXX.X....XXXXX  111.2....33333  111.2....33333
X.X.X....X.X.X  1.1.2....3.3.3  1.1x2....3.3.3
....XXXX.X.X.X  ....2222.3.3.3  ....2222x3.3.3
.........X.X.X  .........3.3.3  .........3.3.3

Input

The first line holds the number of maps QQ (1Q151 \le Q \le 15).

Each map is given in this format. The first line holds RR and CC separated by a space (1R,C501 \le R, C \le 50). The next RR lines each hold a string of length CC, where '.' is sea and 'X' is land.

Every map holds exactly three countries.

Output

For each map, print on one line the smallest number of sea cells you have to fill so that the three countries connect.