#include<bits/stdc++.h>
using namespace std;
int dx[] = {-1,0,0,1}, dy[] = {0,-1,1,0};
bool check(vector<vector<int>>& dist, int& r, int& C, int& k) {
pair<int,int> a = {INT_MIN,0}, b = {INT_MAX,0}, c = {0,INT_MIN}, d = {0,INT_MAX};
bool already_satisfied = true;
for (int i=0; i<r; i++) {
for (int j=0; j<C; j++) {
if (dist[i][j]>k) {
already_satisfied = false;
if (i+j>a.first)
a = {i+j,i-j};
if (i+j<b.first)
b = {i+j,i-j};
if (i-j>c.second)
c = {i+j,i-j};
if (i-j<d.second)
d = {i+j,i-j};
}
}
}
if (already_satisfied) return true;
for (int i=0; i<r; i++) {
for (int j=0; j<C; j++) {
if (dist[i][j]>0) {
int parm1 = max(abs(i+j-a.first),abs(i-j-a.second));
int parm2 = max(abs(i+j-b.first),abs(i-j-c.second));
int parm3 = max(abs(i+j-c.first),abs(i-j-b.second));
int parm4 = max(abs(i+j-d.first),abs(i-j-d.second));
if (max(max(parm1,parm2),max(parm3,parm4))<=k)
return true;
}
}
}
return false;
}
int solve() {
int r,c;
cin>>r>>c;
vector<vector<int>> dist(r,vector<int>(c,-1));
string s;
queue<pair<int,int>> _bfs;
for (int i=0; i<r; i++) {
cin>>s;
for (int j=0; j<c; j++) {
if (s[j]=='1') {
dist[i][j] = 0;
_bfs.push({i,j});
}
}
}
while (!_bfs.empty()) {
pair<int,int> p = _bfs.front();
_bfs.pop();
int x = p.first, y = p.second;
for (int k=0; k<4; k++) {
int _x = x + dx[k], _y = y + dy[k];
if (_x>=0 && _x<r && _y>=0 && _y<c && dist[_x][_y]<0) {
dist[_x][_y] = 1 + dist[x][y];
_bfs.push({_x,_y});
}
}
}
int lo = 0, hi = r+c-2, mi;
while (lo<hi) {
mi = (lo+hi)/2;
if (check(dist,r,c,mi))
hi = mi;
else lo = mi + 1;
}
return lo;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
for (int i=1;i<=t;i++) {
cout<<"Case #"<<i<<": "<<solve()<<"\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBkeFtdID0gey0xLDAsMCwxfSwgZHlbXSA9IHswLC0xLDEsMH07Cgpib29sIGNoZWNrKHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIGRpc3QsIGludCYgciwgaW50JiBDLCBpbnQmIGspIHsKICAgIHBhaXI8aW50LGludD4gYSA9IHtJTlRfTUlOLDB9LCBiID0ge0lOVF9NQVgsMH0sIGMgPSB7MCxJTlRfTUlOfSwgZCA9IHswLElOVF9NQVh9OwogICAgYm9vbCBhbHJlYWR5X3NhdGlzZmllZCA9IHRydWU7CiAgICBmb3IgKGludCBpPTA7IGk8cjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPEM7IGorKykgewogICAgICAgICAgICBpZiAoZGlzdFtpXVtqXT5rKSB7CiAgICAgICAgICAgICAgICBhbHJlYWR5X3NhdGlzZmllZCA9IGZhbHNlOwogICAgICAgICAgICAgICAgaWYgKGkraj5hLmZpcnN0KQogICAgICAgICAgICAgICAgICAgIGEgPSB7aStqLGktan07CiAgICAgICAgICAgICAgICBpZiAoaStqPGIuZmlyc3QpCiAgICAgICAgICAgICAgICAgICAgYiA9IHtpK2osaS1qfTsKICAgICAgICAgICAgICAgIGlmIChpLWo+Yy5zZWNvbmQpCiAgICAgICAgICAgICAgICAgICAgYyA9IHtpK2osaS1qfTsKICAgICAgICAgICAgICAgIGlmIChpLWo8ZC5zZWNvbmQpCiAgICAgICAgICAgICAgICAgICAgZCA9IHtpK2osaS1qfTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGlmIChhbHJlYWR5X3NhdGlzZmllZCkgcmV0dXJuIHRydWU7CiAgICBmb3IgKGludCBpPTA7IGk8cjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPEM7IGorKykgewogICAgICAgICAgICBpZiAoZGlzdFtpXVtqXT4wKSB7CiAgICAgICAgICAgICAgICBpbnQgcGFybTEgPSBtYXgoYWJzKGkrai1hLmZpcnN0KSxhYnMoaS1qLWEuc2Vjb25kKSk7CiAgICAgICAgICAgICAgICBpbnQgcGFybTIgPSBtYXgoYWJzKGkrai1iLmZpcnN0KSxhYnMoaS1qLWMuc2Vjb25kKSk7CiAgICAgICAgICAgICAgICBpbnQgcGFybTMgPSBtYXgoYWJzKGkrai1jLmZpcnN0KSxhYnMoaS1qLWIuc2Vjb25kKSk7CiAgICAgICAgICAgICAgICBpbnQgcGFybTQgPSBtYXgoYWJzKGkrai1kLmZpcnN0KSxhYnMoaS1qLWQuc2Vjb25kKSk7CiAgICAgICAgICAgICAgICBpZiAobWF4KG1heChwYXJtMSxwYXJtMiksbWF4KHBhcm0zLHBhcm00KSk8PWspCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZmFsc2U7Cn0KCmludCBzb2x2ZSgpIHsKICAgIGludCByLGM7CiAgICBjaW4+PnI+PmM7CiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IGRpc3Qocix2ZWN0b3I8aW50PihjLC0xKSk7CiAgICBzdHJpbmcgczsKICAgIHF1ZXVlPHBhaXI8aW50LGludD4+IF9iZnM7CiAgICBmb3IgKGludCBpPTA7IGk8cjsgaSsrKSB7CiAgICAgICAgY2luPj5zOwogICAgICAgIGZvciAoaW50IGo9MDsgajxjOyBqKyspIHsKICAgICAgICAgICAgaWYgKHNbal09PScxJykgewogICAgICAgICAgICAgICAgZGlzdFtpXVtqXSA9IDA7CiAgICAgICAgICAgICAgICBfYmZzLnB1c2goe2ksan0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgd2hpbGUgKCFfYmZzLmVtcHR5KCkpIHsKICAgICAgICBwYWlyPGludCxpbnQ+IHAgPSBfYmZzLmZyb250KCk7CiAgICAgICAgX2Jmcy5wb3AoKTsKICAgICAgICBpbnQgeCA9IHAuZmlyc3QsIHkgPSBwLnNlY29uZDsKICAgICAgICBmb3IgKGludCBrPTA7IGs8NDsgaysrKSB7CiAgICAgICAgICAgIGludCBfeCA9IHggKyBkeFtrXSwgX3kgPSB5ICsgZHlba107CiAgICAgICAgICAgIGlmIChfeD49MCAmJiBfeDxyICYmIF95Pj0wICYmIF95PGMgJiYgZGlzdFtfeF1bX3ldPDApIHsKICAgICAgICAgICAgICAgIGRpc3RbX3hdW195XSA9IDEgKyBkaXN0W3hdW3ldOwogICAgICAgICAgICAgICAgX2Jmcy5wdXNoKHtfeCxfeX0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaW50IGxvID0gMCwgaGkgPSByK2MtMiwgbWk7CiAgICB3aGlsZSAobG88aGkpIHsKICAgICAgICBtaSA9IChsbytoaSkvMjsKICAgICAgICBpZiAoY2hlY2soZGlzdCxyLGMsbWkpKQogICAgICAgICAgICBoaSA9IG1pOwogICAgICAgIGVsc2UgbG8gPSBtaSArIDE7CiAgICB9CiAgICByZXR1cm4gbG87Cn0KaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKCiAgICBpbnQgdDsKICAgIGNpbj4+dDsKICAgIGZvciAoaW50IGk9MTtpPD10O2krKykgewogICAgICAgIGNvdXQ8PCJDYXNlICMiPDxpPDwiOiAiPDxzb2x2ZSgpPDwiXG4iOwogICAgfQp9Cg==