’#‘代表草,从给定的矩阵中找两点,从这两点开始进行bfs,每次bfs后记录燃尽草地需要时间
用 a[ ] 记录‘#’位置,然后枚举位置
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int maxn = 10 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
struct node {
int x, y, cnt;
node() {}
node(int x_, int y_, int t_) : x(x_), y(y_), cnt(t_) {}
};
queue<node> qu;
int T, n, m, len;
char mp[maxn][maxn];
int vis[maxn][maxn];
int a[maxn*maxn];
int dir[4][2] = { {0,1}, {0,-1}, {1,0}, {-1,0} };
void init() {
memset(a, 0, sizeof a);
while(!qu.empty()) qu.pop();
scanf("%d%d", &n, &m);
int cur = 0;
for(int i = 0; i < n; ++i) {
scanf("%s", mp[i]);
for(int j = 0; j < m; ++j) {
if(mp[i][j] == '#')
a[cur++] = i*m + j;
}
}
len = cur;
}
int bfs(int x, int y) {
qu.push( node(a[x]/m, a[x]%m, 0) ); vis[a[x]/m][a[x]%m] = 1;
if(x != y) {
qu.push( node(a[y]/m, a[y]%m, 0) ); vis[a[y]/m][a[y]%m] = 1;
}
int num = 0;
while(!qu.empty()) {
node t = qu.front(); qu.pop();
for(int i = 0; i < 4; ++i) {
int nx = t.x + dir[i][0];
int ny = t.y + dir[i][1];
if(nx < 0 || nx >= n || ny < 0 || ny >= m || mp[nx][ny] == '.' || vis[nx][ny]) continue;
qu.push( node(nx, ny, t.cnt+1) );
num = t.cnt+1;
vis[nx][ny] = 1;
}
}
return num;
}
void solve() {
int ans = INF;
for(int i = 0; i < len; ++i) {
for(int j = 0; j < len; ++j) {
memset(vis, 0, sizeof vis);
int t = bfs(i, j);
int f = 0;
for(int k = 0; k < n; ++k) {
if(f) break;
for(int y = 0; y < m; ++y) {
if(mp[k][y] == '#' && vis[k][y] == 0) {
f = 1; break;
}
}
}
if(!f) {
ans = min(ans, t);
}
}
}
if(ans == INF) ans = -1;
cout << ans << endl;
}
int main() {
scanf("%d", &T);
int kase = 1;
while(T--) {
init();
printf("Case %d: ", kase++);
solve();
}
return 0;
}