#include<bits/stdc++.h>
using namespace std;
const int N=15,M=N*N;
char g[N][N];
bool st[N][N][N][N];
int sum;
typedef pair<int, int> PII;
int a,b;
queue<PII>q;
int ans;
int bu[N][N];
int dfs(int x,int y,int x1,int y1)
{
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
memset(bu,0x3f,sizeof bu);
q.push({x,y});
q.push({x1,y1});
int shu=2;
int k=0;
bu[x1][y1]=0;
bu[x][y]=0;
while(!q.empty()){
PII p=q.front();
q.pop();
for(int i=0;i<4;i++){
int hh=p.first+dx[i],tt=p.second+dy[i];
if(hh<0||hh>=a||tt<0||tt>=b) continue;
if(bu[hh][tt]!=0x3f3f3f3f||g[hh][tt]=='.') continue;
q.push({hh,tt});
shu++;
bu[hh][tt]=bu[p.first][p.second]+1;
k=max(k,bu[hh][tt]);
}
}
if(shu==sum) return k;
return -1;
}
int main()
{
int t;
cin>>t;
int v=1;
while(t--){
memset(st, 0, sizeof st);
sum=0;
ans=1e5;
cin>>a>>b;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
cin>>g[i][j];
if(g[i][j]=='#') sum++;
}
}
if(sum==1||sum==2){
cout<<"Case "<<v++<<": "<<0<<endl;
continue;
}
if(sum==0){
cout<<"Case "<<v++<<": "<<-1<<endl;
continue;
}
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
for(int k=0;k<a;k++){
for(int p=0;p<b;p++){
if(g[i][j]=='.'||g[k][p]=='.') continue;
if(st[i][j][k][p]) continue;
st[i][j][k][p]=st[k][p][i][j]=true;
int hh=dfs(i,j,k,p);
if(hh!=-1) ans=min(ans,hh);
}
}
}
}
if(ans==1e5) ans=-1;
cout<<"Case "<<v++<<": "<<ans<<endl;
}
return 0;
}