#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define INF 0x3f3f3f
int gcd(int a, int b) {return b==0 ? a:gcd(b,a%b);}
const int N = 15;
int n, m;
int flag;
int minn;
char s[N][N];
int vis[N][N];
int dis[N][N];
int xx[] = {0,0,-1,1};
int yy[] = {-1,1,0,0};
struct node
{
int x, y;
};
struct no
{
int x, y, step;
};
void bfs(int sx1, int sy1, int sx2, int sy2)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++) dis[i][j] = 0;
}
memset(vis,0,sizeof(vis));
queue<no>q;
int maxn = 0;
if(sx1==sx2&&sy1==sy2){
q.push((no){sx1,sy1,0});
dis[sx1][sy1] = 0;
vis[sx1][sy1] = 1;
}else{
q.push((no){sx1,sy1,0});
q.push((no){sx2,sy2,0});
dis[sx1][sy1] = 0;
dis[sx2][sy2] = 0;
vis[sx1][sy1] = 1;
vis[sx2][sy2] = 1;
}
while(!q.empty())
{
no p = q.front(); q.pop();
for(int i = 0; i < 4; i++)
{
int tx = p.x+xx[i];
int ty = p.y+yy[i];
if(tx<1||ty<1||tx>n||ty>m||vis[tx][ty]||s[tx][ty]=='.') continue;
if(dis[tx][ty]>=p.step+1) continue;
vis[tx][ty] = 1;
dis[tx][ty] = p.step+1;
maxn = max(maxn,dis[tx][ty]);
q.push((no){tx,ty,p.step+1});
}
}
int op = 1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(s[i][j]=='#'&&vis[i][j]==0){
op = 0;
break;
}
}
if(!op) break;
}
if(op){
flag = 1;
minn = min(minn,maxn);
}
}
int main()
{
int t;
scanf("%d", &t);
for(int tt = 1; tt <= t; tt++)
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%s", s[i]+1);
printf("Case %d: ", tt);
vector<node>a;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(s[i][j]=='#') a.push_back((node){i,j});
}
}
flag = 0;
minn = INF;
int l = a.size();
for(int i = 0; i < l; i++)
{
for(int j = i; j < l; j++)
{
bfs(a[i].x,a[i].y,a[j].x,a[j].y);
}
}
if(!flag) printf("-1\n");
else printf("%d\n", minn);
}
return 0;
}
09-08
08-19
08-19
08-19