//广搜
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node{
int x;
int y;
};
int w,h;
int mat[961][1441];
int xx[4]={0,0,-1,1};
int yy[4]={1,-1,0,0};
void bfs(int a,int b){
queue<node>q;
node t1,t2;
t1.x =a;
t1.y =b;
q.push(t1);
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=0;i<4;i++){
t2.x =t1.x +xx[i];
t2.y =t1.y +yy[i];
if(t2.x <0||t2.y<0||t2.x>h+1||t2.y>w+1||mat[t2.x][t2.y]==0)
continue;
mat[t2.x][t2.y]=0;
q.push(t2);
}
}
}
int main()
{
int n;
scanf("%d",&n);
while(n--){
int a;
scanf("%d%d",&w,&h);
memset(mat,1,sizeof(mat));
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++){
scanf("%d",&a);
mat[i][j]=a;
}
bfs(0,0);
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(j==w)
printf("%d\n",mat[i][j]);
else
printf("%d ",mat[i][j]);
}
}
}
return 0;
}
//深搜
# include<stdio.h>
# include<string.h>
int a[1000][1445],w,h;
void dfs(int c1,int c2)
{
if(a[c1][c2]&&c1>=0&&c2>=0&&c1<=h+1&&c2<=w+1)
{
a[c1][c2]=0;
dfs(c1,c2+1);
dfs(c1+1,c2);
dfs(c1-1,c2);
dfs(c1,c2-1);
}
}
int main(){
int t;
scanf("%d",&t);
while(t--)
{ memset(a,1,sizeof(a));//在数组外在
int i,j;
scanf("%d%d",&w,&h);
for(i=1;i<=h;i++)
for(j=1;j<=w;j++)
scanf("%d",&a[i][j]);
dfs(0,0);
for(i=1;i<=h;i++){
for(j=1;j<=w;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
return 0;
}