#include <iostream>
#include <queue>
using namespace std;
int dx[5] = {-1,0,1,0};
int dy[5] = {0,1,0,-1};
int r,c;
const int MAXN = 300;
int grid[MAXN][MAXN];
int dp[MAXN][MAXN];
bool check(int x,int y){
if(x<=0||y<=0||x>r||y>c||dp[x][y]!=-1){
return false;
}
return true;
}
bool check2(int k){
int max1 = 0;
int min1 =r+c;
int max2 = 0;
int min2 = r+c;
bool ok = false;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(dp[i][j]<=k){
continue;
}
ok = true;
max1 = max(max1,i+j);
min1 = min(min1,i+j);
max2 = max(max2,i-j);
min2 = min(min2,i-j);
}
}
if(!ok){
return true;
}
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(dp[i][j] == 0){
continue;
}
int ok1 = abs(max1-(i+j));
int ok2 = abs(min1-(i+j));
int ok3 = abs(max2-(i-j));
int ok4 = abs(min2-(i-j));
if(ok1<=k && ok2<=k && ok3<=k && ok4<=k){
return true;
}
}
}
return false;
}
void fix(){
for(int i=0;i<=r;i++){
for(int j=0;j<=c;j++){
dp[i][j] = -1;
grid[i][j] = 0;
}
}
}
int main() {
int t;
cin>>t;
for(int h=1;h<=t;h++){
cin>>r>>c;
fix();
deque<pair<int,int>> q1;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
char x;
cin>>x;
if(x == '1'){
grid[i][j] = 1;
dp[i][j] = 0;
q1.push_back(make_pair(i,j));
}else{
grid[i][j] = 0;
}
}
}
while(!q1.empty()){
auto hold = q1.front();
q1.pop_front();
int currr = hold.first;
int currc = hold.second;
for(int i=0;i<4;i++){
int nextr = currr+dx[i];
int nextc = currc+dy[i];
if(check(nextr,nextc)){
dp[nextr][nextc] = dp[currr][currc] +1;
q1.push_back(make_pair(nextr,nextc));
}
}
}
int l =0 ;
int r =500;
int ans = 0;
while(l<=r){
int mid = (l+r)/2;
if(check2(mid)){
ans = mid;
r = mid-1;
}else{
l = mid+1;
}
}
cout<<"Case #"<<h<<": "<<ans<<endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBkeFs1XSA9IHstMSwwLDEsMH07CmludCBkeVs1XSA9IHswLDEsMCwtMX07CmludCByLGM7CmNvbnN0IGludCBNQVhOID0gMzAwOwppbnQgZ3JpZFtNQVhOXVtNQVhOXTsKaW50IGRwW01BWE5dW01BWE5dOwpib29sIGNoZWNrKGludCB4LGludCB5KXsKICAgIGlmKHg8PTB8fHk8PTB8fHg+cnx8eT5jfHxkcFt4XVt5XSE9LTEpewogICAgICAgIAogICAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KICAgIHJldHVybiB0cnVlOwp9CmJvb2wgY2hlY2syKGludCBrKXsKICAgIGludCBtYXgxID0gMDsKICAgIGludCBtaW4xID1yK2M7CiAgICBpbnQgbWF4MiA9IDA7CiAgICBpbnQgbWluMiA9IHIrYzsKICAgIGJvb2wgb2sgPSBmYWxzZTsKICAgIGZvcihpbnQgaT0xO2k8PXI7aSsrKXsKICAgICAgICBmb3IoaW50IGo9MTtqPD1jO2orKyl7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZihkcFtpXVtqXTw9ayl7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBvayA9IHRydWU7CiAgICAgICAgICAgIG1heDEgPSBtYXgobWF4MSxpK2opOwogICAgICAgICAgICBtaW4xID0gbWluKG1pbjEsaStqKTsKICAgICAgICAgICAgbWF4MiA9IG1heChtYXgyLGktaik7CiAgICAgICAgICAgIG1pbjIgPSBtaW4obWluMixpLWopOwogICAgICAgICAgICAKICAgIAogICAgICAgIH0KICAgIH0KICAgIGlmKCFvayl7CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9CiAgICBmb3IoaW50IGk9MTtpPD1yO2krKyl7CiAgICAgICAgZm9yKGludCBqPTE7ajw9YztqKyspewogICAgICAgICAgICBpZihkcFtpXVtqXSA9PSAwKXsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGludCBvazEgPSBhYnMobWF4MS0oaStqKSk7CiAgICAgICAgICAgIGludCBvazIgPSBhYnMobWluMS0oaStqKSk7CiAgICAgICAgICAgIGludCBvazMgPSBhYnMobWF4Mi0oaS1qKSk7CiAgICAgICAgICAgIGludCBvazQgPSBhYnMobWluMi0oaS1qKSk7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZihvazE8PWsgJiYgb2syPD1rICYmIG9rMzw9ayAmJiBvazQ8PWspewogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gZmFsc2U7Cn0Kdm9pZCBmaXgoKXsKICAgIGZvcihpbnQgaT0wO2k8PXI7aSsrKXsKICAgICAgICBmb3IoaW50IGo9MDtqPD1jO2orKyl7CiAgICAgICAgICAgIGRwW2ldW2pdID0gLTE7CiAgICAgICAgICAgIGdyaWRbaV1bal0gPSAwOwogICAgICAgIH0KICAgIH0KICAgIAp9CmludCBtYWluKCkgewogICAgaW50IHQ7CiAgICBjaW4+PnQ7CiAgICBmb3IoaW50IGg9MTtoPD10O2grKyl7CiAgICAgICAKICAgICAgICAKICAgICAgICBjaW4+PnI+PmM7CiAgICAgICAgIGZpeCgpOwogICAgICAgIGRlcXVlPHBhaXI8aW50LGludD4+IHExOwogICAgICAgIGZvcihpbnQgaT0xO2k8PXI7aSsrKXsKICAgICAgICAgICAgZm9yKGludCBqPTE7ajw9YztqKyspewogICAgICAgICAgICAgICAgY2hhciB4OwogICAgICAgICAgICAgICAgY2luPj54OwogICAgICAgICAgICAgICAgaWYoeCA9PSAnMScpewogICAgICAgICAgICAgICAgICAgIGdyaWRbaV1bal0gPSAxOwogICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gMDsKICAgICAgICAgICAgICAgICAgICBxMS5wdXNoX2JhY2sobWFrZV9wYWlyKGksaikpOwogICAgICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICAgICAgZ3JpZFtpXVtqXSA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd2hpbGUoIXExLmVtcHR5KCkpewogICAgICAgICAgICBhdXRvIGhvbGQgPSBxMS5mcm9udCgpOwogICAgICAgICAgICBxMS5wb3BfZnJvbnQoKTsKICAgICAgICAgICAgaW50IGN1cnJyID0gaG9sZC5maXJzdDsKICAgICAgICAgICAgaW50IGN1cnJjID0gaG9sZC5zZWNvbmQ7CiAgICAgICAgICAgIGZvcihpbnQgaT0wO2k8NDtpKyspewogICAgICAgICAgICAgICAgaW50IG5leHRyID0gY3VycnIrZHhbaV07CiAgICAgICAgICAgICAgICBpbnQgbmV4dGMgPSBjdXJyYytkeVtpXTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgaWYoY2hlY2sobmV4dHIsbmV4dGMpKXsKICAgICAgICAgICAgICAgICAgICBkcFtuZXh0cl1bbmV4dGNdID0gZHBbY3VycnJdW2N1cnJjXSArMTsKICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICBxMS5wdXNoX2JhY2sobWFrZV9wYWlyKG5leHRyLG5leHRjKSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIAogICAgICAgIGludCBsID0wIDsKICAgICAgICBpbnQgciA9NTAwOwogICAgICAgIGludCBhbnMgPSAwOwogICAgICAgIHdoaWxlKGw8PXIpewogICAgICAgICAgICBpbnQgbWlkID0gKGwrcikvMjsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKGNoZWNrMihtaWQpKXsKICAgICAgICAgICAgICAgIGFucyA9IG1pZDsKICAgICAgICAgICAgICAgIHIgPSBtaWQtMTsKICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICBsID0gbWlkKzE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dDw8IkNhc2UgIyI8PGg8PCI6ICI8PGFuczw8ZW5kbDsKICAgIH0KfQ==