#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>usingnamespacestd;
#define MAX 100000#define LL long longint cas=1,T;
constint INF = 99999999 ;
int n,m;
char ch[15][15];
int cnt;
int vis[110][110];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
int x,y;
int step;
}q1,q2,mapp[150];
int bfs(int x1,int y1,int x2,int y2)
{
int maxx = 0;
queue<Node>que;
q1.x=x1,q1.y=y1,q1.step=0;
q2.x=x2,q2.y=y2,q2.step=0;
que.push(q1);
que.push(q2);
while (!que.empty())
{
struct Node st1,st=que.front();
que.pop();
for (int i =0;i<4;i++)
{
int xx = st.x+dir[i][0];
int yy = st.y+dir[i][1];
if (!vis[xx][yy]&& ch[xx][yy]=='#'&&(xx>=0 && xx<n&&yy>=0&&yy<m))
{
vis[xx][yy]=1;
st1.x=xx;
st1.y==yy;
st1.step=st.step+1;
que.push(st1);
}
}
maxx=max(maxx,st.step);
}
return maxx;
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
cnt=0;
for (int j = 0;j<n;j++)
{
scanf("%s",ch[j]);
for (int k = 0;k<m;k++)
if (ch[j][k]=='#')
{
cnt++;
mapp[cnt].x=j;
mapp[cnt].y=k;
}
}
printf("Case %d: ",cas++);
if (cnt<=2)
{
printf("0\n");
continue;
}
int minn=INF;
for (int j = 0;j<cnt;j++)
for (int k = j;k<cnt;k++)
{
memset(vis,0,sizeof(vis));
vis[mapp[j].x][mapp[j].y]=1;
vis[mapp[k].x][mapp[k].y]=1;
bool flag = false;
int minnn=bfs(mapp[j].x,mapp[j].y,mapp[k].x,mapp[k].y);
for (int h = 0;h<n;h++)
{
for (int l = 0;l<m;l++)
{
if (ch[h][l]!='#')
continue;
if (!vis[h][l])
{
flag=true;
break;
}
if (flag)
break;
}
if (!flag)
minn=min(minn,minnn);
}
}
if (minn==INF)
printf("-1\n");
elseprintf("%d\n",minn);
}
//freopen("in","r",stdin);//scanf("%d",&T);//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return0;
}