#include<bits/stdc++.h>
using namespace std;
int n,m;
int dis[190][190];
bool visit[190][190];
void bfs(pair<int,int> p)
{
int fixi=p.first;
int fixj=p.second;
memset(visit,false,sizeof(visit));
queue<pair<int,int> >q;
q.push(p);
visit[fixi][fixj]=true;
while(!q.empty())
{
int i=q.front().first;
int j=q.front().second;
q.pop();
dis[i][j]=min(dis[i][j],abs(i-fixi)+abs(j-fixj));
if(i>0 && !visit[i-1][j])
{
q.push(make_pair(i-1,j));
visit[i-1][j]=true;
}
if(j>0 && !visit[i][j-1])
{
visit[i][j-1]=true;
q.push(make_pair(i,j-1));
}
if(i<n-1 && !visit[i+1][j])
{
q.push(make_pair(i+1,j));
visit[i+1][j]=true;
}
if(j<m-1 && !visit[i][j+1])
{
visit[i][j+1]=true;
q.push(make_pair(i,j+1));
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
queue<pair<int,int> > q1s;
cin>>n>>m;
memset(dis,10000,sizeof(dis));
for(int i=0;i<n;i++)
{
string s;
cin>>s;
for(int j=0;j<m;j++)
{
if(s[j]=='1')
{
q1s.push(make_pair(i,j));
dis[i][j]=0;
}
}
}
while(!q1s.empty())
{
bfs(q1s.front());
q1s.pop();
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<dis[i][j]<<" "; //One thing I have figured out is that when I try to print dis for values of
cout<<"\n"; //i,j larger than n and m it shows some values instead of 0
} //i.e dis table is being filled even for values of i,j greater than n,m
}
}
/*
1
3 4
0001
0011
0110
*/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG4sbTsKaW50IGRpc1sxOTBdWzE5MF07CmJvb2wgdmlzaXRbMTkwXVsxOTBdOwp2b2lkIGJmcyhwYWlyPGludCxpbnQ+IHApCnsKICAgIGludCBmaXhpPXAuZmlyc3Q7CiAgICBpbnQgZml4aj1wLnNlY29uZDsKICAgIG1lbXNldCh2aXNpdCxmYWxzZSxzaXplb2YodmlzaXQpKTsKICAgIHF1ZXVlPHBhaXI8aW50LGludD4gPnE7CiAgICBxLnB1c2gocCk7CiAgICB2aXNpdFtmaXhpXVtmaXhqXT10cnVlOwogICAgd2hpbGUoIXEuZW1wdHkoKSkKICAgIHsKICAgICAgICBpbnQgaT1xLmZyb250KCkuZmlyc3Q7CiAgICAgICAgaW50IGo9cS5mcm9udCgpLnNlY29uZDsKICAgICAgICBxLnBvcCgpOwogICAgICAgIGRpc1tpXVtqXT1taW4oZGlzW2ldW2pdLGFicyhpLWZpeGkpK2FicyhqLWZpeGopKTsKCiAgICAgICAgaWYoaT4wICYmICF2aXNpdFtpLTFdW2pdKQogICAgICAgIHsKICAgICAgICAgICAgcS5wdXNoKG1ha2VfcGFpcihpLTEsaikpOwogICAgICAgICAgICB2aXNpdFtpLTFdW2pdPXRydWU7CiAgICAgICAgfQoKICAgICAgICBpZihqPjAgJiYgIXZpc2l0W2ldW2otMV0pCiAgICAgICAgewogICAgICAgICAgICB2aXNpdFtpXVtqLTFdPXRydWU7CiAgICAgICAgICAgIHEucHVzaChtYWtlX3BhaXIoaSxqLTEpKTsKICAgICAgICB9CgogICAgICAgIGlmKGk8bi0xICYmICF2aXNpdFtpKzFdW2pdKQogICAgICAgIHsKICAgICAgICAgICAgcS5wdXNoKG1ha2VfcGFpcihpKzEsaikpOwogICAgICAgICAgICB2aXNpdFtpKzFdW2pdPXRydWU7CiAgICAgICAgfQoKICAgICAgICBpZihqPG0tMSAmJiAhdmlzaXRbaV1baisxXSkKICAgICAgICB7CiAgICAgICAgICAgIHZpc2l0W2ldW2orMV09dHJ1ZTsKICAgICAgICAgICAgcS5wdXNoKG1ha2VfcGFpcihpLGorMSkpOwogICAgICAgIH0KICAgIH0KfQppbnQgbWFpbigpCnsKICAgIHN0ZDo6aW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgaW50IHQ7CiAgICBjaW4+PnQ7CiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgcXVldWU8cGFpcjxpbnQsaW50PiA+IHExczsKICAgICAgICBjaW4+Pm4+Pm07CgogICAgICAgIG1lbXNldChkaXMsMTAwMDAsc2l6ZW9mKGRpcykpOwogICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHN0cmluZyBzOwogICAgICAgICAgICAgICAgY2luPj5zOwogICAgICAgICAgICAgICAgZm9yKGludCBqPTA7ajxtO2orKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZihzW2pdPT0nMScpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBxMXMucHVzaChtYWtlX3BhaXIoaSxqKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGRpc1tpXVtqXT0wOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICB3aGlsZSghcTFzLmVtcHR5KCkpCiAgICAgICAgewogICAgICAgICAgICBiZnMocTFzLmZyb250KCkpOwogICAgICAgICAgICBxMXMucG9wKCk7CiAgICAgICAgfQoKICAgICAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGludCBqPTA7ajxtO2orKykKICAgICAgICAgICAgY291dDw8ZGlzW2ldW2pdPDwiICI7IC8vT25lIHRoaW5nIEkgaGF2ZSBmaWd1cmVkIG91dCBpcyB0aGF0IHdoZW4gSSB0cnkgdG8gcHJpbnQgZGlzIGZvciB2YWx1ZXMgb2YKICAgICAgICAgICAgY291dDw8IlxuIjsgICAgICAgICAgIC8vaSxqIGxhcmdlciB0aGFuIG4gYW5kIG0gaXQgc2hvd3Mgc29tZSB2YWx1ZXMgaW5zdGVhZCBvZiAwCiAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgICAgICAvL2kuZSBkaXMgdGFibGUgaXMgYmVpbmcgZmlsbGVkIGV2ZW4gZm9yIHZhbHVlcyBvZiBpLGogZ3JlYXRlciB0aGFuIG4sbQogICAgfQoKfQovKgoxCjMgNAowMDAxCjAwMTEKMDExMAoqLwo=