Problem Description
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n,m;
char G[][];
bool vis[][];
int ans;
int num;
struct node{
int x,y,step;
};
bool jug(){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
if(G[i][j]=='#'&&!vis[i][j])
return false;
}
return true;
}
void bfs(int x,int y,int x2,int y2){
queue<node > q;
node t; t.step=; t.x=x; t.y=y;
node tt; tt.step=; tt.x=x2; tt.y=y2;
if(!vis[x][y])
vis[x][y]=,q.push(t);
if(!vis[x2][y2])
vis[x2][y2]=,q.push(tt);
while(!q.empty()){
node temp=q.front();
q.pop();
ans=max(ans,temp.step);
for(int i=;i<;i++){
int xx=temp.x+dir[i][];
int yy=temp.y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&G[xx][yy]=='#'&&!vis[xx][yy]){
vis[xx][yy]=;
node te; te.x=xx; te.y=yy; te.step=temp.step+;
q.push(te);
}
}
}
}
void dfs(int x,int y){
for(int i=;i<;i++){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&!vis[xx][yy]&&G[xx][yy]=='#'){
vis[xx][yy]=;
dfs(xx,yy);
}
}
}
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
int w=;
while(t--){
cin>>n>>m;
num=;
pair<int,int> p[];
int sz=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
cin>>G[i][j];
if(G[i][j]=='#'){
sz++;
p[sz].first=i;
p[sz].second=j;
}
}
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(!vis[i][j]&&G[i][j]=='#'){
num++;
vis[i][j]=;
dfs(i,j);
}
if(num<=){
int a=inf;
for(int i=;i<=sz;i++)
for(int j=;j<=sz;j++){
memset(vis,,sizeof(vis));
ans=;
bfs(p[i].first,p[i].second,p[j].first,p[j].second);
if(jug())
a=min(a,ans);
}
cout<<"Case "<<w++<<": "<<a<<endl;
}else cout<<"Case "<<w++<<": -1"<<endl;
}
return ;
}