//By Tushar Gautam
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define memreset(a) memset(a,0,sizeof(a))
#define testcase(t) int t;cin>>t;while(t--)
#define forstl(i,v) for(auto &i: v)
#define forn(i,e) for(int i = 0; i < e;++i)
#define forsn(i,s,e) for(int i = s; i < e;++i)
#define rforn(i,s) for(int i = s; i >= 0;--i)
#define rforsn(i,s,e) for(int i = s; i >= e;--i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime ((double)clock()/CLOCKS_PER_SEC)
#define dbgarr(v,s,e) cerr<<#v<<" = "; forsn(i,s,e) cerr<<v[i]<<", "; cerr<<endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) { cerr<<endl; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t"; err(++it, args...);
}
template<typename T1,typename T2>
ostream& operator <<(ostream& c,pair<T1,T2> &v){
c<<"("<<v.fi<<","<<v.se<<")"; return c;
}
template <template <class...> class TT, class ...T>
ostream& operator<<(ostream& out,TT<T...>& c){
out<<"{ ";
forstl(x,c) out<<x<<" ";
out<<"}"; return out;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
const int LIM=1e5+5,MOD=1e9+7;
int t,n,m,k,x,y,w;
vector<string> in;
vv32 d;
bool valid(int i,int j,int di){
return i<n && i>=0 && j<m && j>=0 && d[i][j]>di;
}
bool P(int k){
int sm=MOD,sM=-MOD,dm=MOD,dM=-MOD;
forn(i,n){
forn(j,m){
if(d[i][j]>k){
sm=min(sm,i+j);
sM=max(sM,i+j);
dm=min(dm,i-j);
dM=max(dM,i-j);
}
}
}
forn(i,n){
forn(j,m){
bool y=1;
if(i+j+k<sM) y=0;
if(i+j-k>sm) y=0;
if(i-j+k<dM) y=0;
if(i-j-k>dm) y=0;
if(y) return 1;
}
}
return 0;
}
void solve(int c){
cin>>n>>m;
in.resize(n);
d.assign(n,v32(m,MOD));
queue<p32> q;
forn(i,n){
cin>>in[i];
forn(j,m){
if(in[i][j]=='1') d[i][j]=0,q.push(mp(i,j));
}
}
while(!q.empty()){
auto tp=q.front(); q.pop();
tie(x,y)=tp;
int dd=d[tp.fi][tp.se];
if(valid(x-1,y,1+dd)) d[x-1][y]=1+dd,q.push(mp(x-1,y));
if(valid(x+1,y,1+dd)) d[x+1][y]=1+dd,q.push(mp(x+1,y));
if(valid(x,y-1,1+dd)) d[x][y-1]=1+dd,q.push(mp(x,y-1));
if(valid(x,y+1,1+dd)) d[x][y+1]=1+dd,q.push(mp(x,y+1));
}
int l=0,r=500;
while(l<r){
int mid=(l+r)/2;
if(P(mid)) r=mid;
else l=mid+1;
}
cout<<"Case #"<<c<<": "<<l<<ln;
}
signed main(){
fastio;
int c=0;
testcase(tt) solve(++c);
return 0;
}
Ly9CeSBUdXNoYXIgR2F1dGFtCiNwcmFnbWEgR0NDIG9wdGltaXplICgiLU8yIikKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZmFzdGlvIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7Y2luLnRpZSgwKTtjb3V0LnRpZSgwKQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1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