#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;
int scan[185][185];
int ans[185][185];
int nx[185];
int ny[185];
int N,M,test;
//computing distance
int dis(int i,int j,int k ,int l){
int one=i-k;
int two=j-l;
if (one<0)one*=-1;
if (two<0)two*=-1;
return one+two;
}
//my dfs function
void dfs(int n,int m,int i,int j){
if(i>=0&&i<N&&j>=0&&j<M){
if (scan[i][j]==1)return;
int d=dis(n,m,i,j);
if(ans[i][j]<=d)return;
ans[i][j]=d;
dfs(n,m,i,j+1);
dfs(n,m,i,j-1);
dfs(n,m,i-1,j);
dfs(n,m,i+1,j);
}
else return;
}
int main(){
int n;
string s;
//getting number of testcase
scanf("%d",&test);
for(int t=0;t<test;t++){
n=0;
//getting row and column
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)ans[i][j]=32000;
//getting input
for(int i=0;i<N;i++){
cin>>s;
for(int j=0;j<M;j++){
scan[i][j]=s[j]-'0';
if(scan[i][j]==1){
nx[n]=i;ny[n]=j;
n++;
}
}
}
//starts searching
for(int i=0;i<n;i++){
ans[nx[i]][ny[i]]=0;
dfs(nx[i],ny[i],nx[i],ny[i]+1);
dfs(nx[i],ny[i],nx[i],ny[i]-1);
dfs(nx[i],ny[i],nx[i]+1,ny[i]);
dfs(nx[i],ny[i],nx[i]-1,ny[i]);
}
//printing
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
printf("%d ",ans[i][j]);
}
printf("\n");
}
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPGlvc3RyZWFtPgojaW5jbHVkZTxzdHJpbmc+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKaW50IHNjYW5bMTg1XVsxODVdOwppbnQgYW5zWzE4NV1bMTg1XTsKaW50IG54WzE4NV07CmludCBueVsxODVdOwppbnQgTixNLHRlc3Q7Ci8vY29tcHV0aW5nIGRpc3RhbmNlCiBpbnQgZGlzKGludCBpLGludCBqLGludCBrICxpbnQgbCl7CiAgICAgICAgaW50IG9uZT1pLWs7CiAgICAgICAgaW50IHR3bz1qLWw7CiAgICAgICAgaWYgKG9uZTwwKW9uZSo9LTE7CiAgICAgICAgaWYgKHR3bzwwKXR3byo9LTE7CiAgICAgICAgcmV0dXJuIG9uZSt0d287CiAgICB9CiAKIAovL215IGRmcyBmdW5jdGlvbiAKdm9pZCBkZnMoaW50IG4saW50IG0saW50IGksaW50IGopewogICAgICAgIGlmKGk+PTAmJmk8TiYmaj49MCYmajxNKXsKICAgICAgICBpZiAoc2NhbltpXVtqXT09MSlyZXR1cm47CiAgICAgICAgaW50IGQ9ZGlzKG4sbSxpLGopOwogICAgICAgIGlmKGFuc1tpXVtqXTw9ZClyZXR1cm47CiAgICAgICAgYW5zW2ldW2pdPWQ7CiAgICAgICAgZGZzKG4sbSxpLGorMSk7CiAgICAgICAgZGZzKG4sbSxpLGotMSk7CiAgICAgICAgZGZzKG4sbSxpLTEsaik7CiAgICAgICAgZGZzKG4sbSxpKzEsaik7Cgl9CgllbHNlIHJldHVybjsKfQogCiAKIAppbnQgbWFpbigpewoKICAgICAgaW50IG47CiAgICAgIHN0cmluZyBzOwogICAgICAgICAgICAgICAgLy9nZXR0aW5nIG51bWJlciBvZiB0ZXN0Y2FzZQogICAgICBzY2FuZigiJWQiLCZ0ZXN0KTsKICAgICAgIGZvcihpbnQgdD0wO3Q8dGVzdDt0KyspewogICAgICAgICAgIG49MDsKICAgICAgICAgICAgICAgICAgICAgICAgLy9nZXR0aW5nIHJvdyBhbmQgY29sdW1uCiAgICAgICAgICAgc2NhbmYoIiVkICVkIiwmTiwmTSk7CiAgICAgICAgICAgZm9yKGludCBpPTA7aTxOO2krKykKICAgICAgICAgICAgICAgIGZvcihpbnQgaj0wO2o8TTtqKyspYW5zW2ldW2pdPTMyMDAwOwogICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgLy9nZXR0aW5nIGlucHV0CiAgICAgICAgICAgZm9yKGludCBpPTA7aTxOO2krKyl7CiAgICAgICAgICAgICAgIGNpbj4+czsKICAgICAgICAgICAgICAgZm9yKGludCBqPTA7ajxNO2orKyl7ICAgCiAgICAgICAgICAgICAgICAgICBzY2FuW2ldW2pdPXNbal0tJzAnOwogICAgICAgICAgICAgICAgICAgaWYoc2NhbltpXVtqXT09MSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBueFtuXT1pO255W25dPWo7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBuKys7CiAgICAgICAgICAgICAgICAgICB9ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICB9CiAgICAgICAgICAgfQogICAgICAgICAgIC8vc3RhcnRzIHNlYXJjaGluZwogICAgICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgICAgICAgICAgICBhbnNbbnhbaV1dW255W2ldXT0wOwogICAgICAgICAgICAgICBkZnMobnhbaV0sbnlbaV0sbnhbaV0sbnlbaV0rMSk7CiAgICAgICAgICAgICAgIGRmcyhueFtpXSxueVtpXSxueFtpXSxueVtpXS0xKTsKICAgICAgICAgICAgICAgZGZzKG54W2ldLG55W2ldLG54W2ldKzEsbnlbaV0pOwogICAgICAgICAgICAgICBkZnMobnhbaV0sbnlbaV0sbnhbaV0tMSxueVtpXSk7CiAgICAgICAgICAgfQogICAgICAgICAgIAogICAgICAgICAgIC8vcHJpbnRpbmcKICAgICAgICAgICBmb3IoaW50IGk9MDtpPE47aSsrKXsKICAgICAgICAgICAgICAgZm9yKGludCBqPTA7ajxNO2orKyl7CiAgICAgICAgICAgICAgICAgICBwcmludGYoIiVkICIsYW5zW2ldW2pdKTsKICAgICAgICAgICAgICAgIH0gCiAgICAgICAgICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgICAgICB9CgogICAgICAgfQogICAgICAgIHJldHVybiAwOwp9CiAKCg==