#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int map[961][1441], ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int n, m;
struct Pic
{
int x, y;
} r, s, t;
void Bfs(int a, int b)
{
r.x = a; r.y = b;
queue<Pic> Q;
Q.push(r);
while(!Q.empty())
{
s = Q.front(); Q.pop();
for(int i = 0; i < 4; i++)
{
t.x = s.x + ac[i][0];
t.y = s.y + ac[i][1];
if(t.x >= 0 && t.x <= n+1 && t.y >= 0 && t.y <= m+1 && map[t.x][t.y] != 0)
{
map[t.x][t.y] = 0;
Q.push(t);
}
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &m, &n);
memset(map, 1, sizeof(map)); //边框:
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &map[i][j]);
Bfs(0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
printf(j==m? "%d\n":"%d ", map[i][j]);
}
return 0;
}