You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$.
A move consists of the following steps:
- Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$.
- Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$.
For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $M$.
- $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces.

-----Output-----
For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible.

-----Constraints-----
- $1 \le T \le 100$
- $1 \le N, M \le 1,000$
- $A_{i, j} \in \{0, 1\}$ for each valid $i, j$
- the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$

-----Example Input-----
1
3 3
010
000
001

-----Example Output-----
1 0 1
2 1 1
1 1 0

Difficulty: MEDIUM_HARD

Solutions:
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = []
	for _ in range(n):
		lst = list(input())
		arr.append(lst)
	row = [0] * n
	col = [0] * m
	flag = 0
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '1':
				flag = 1
				row[i] = 1
				col[j] = 1
	if flag:
		ans = [[0] * m for _ in range(n)]
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '0':
					if row[i] == 1 or col[j] == 1:
						ans[i][j] = 1
					else:
						ans[i][j] = 2
	else:
		ans = [[-1] * m for _ in range(n)]
	for item in ans:
		print(*item)


t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	a = [[int(d) for d in input()] for i in range(n)]
	if sum((sum(i) for i in a)):
		sol = [[2] * m] * n
		for i in range(n):
			if 1 in a[i]:
				sol[i] = [1] * m
				for j in range(m):
					sol[i][j] -= a[i][j]
		for j in range(m):
			if 1 in [i[j] for i in a]:
				for i in range(n):
					sol[i][j] = 1 - a[i][j]
	else:
		sol = [[-1] * m] * n
	print('\n'.join((' '.join((str(i) for i in row)) for row in sol)))


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = []
	ans = []
	flag = 0
	for i in range(n):
		k = list(input())
		z = list(map(int, k))
		l.append(z)
		if 1 in z and flag == 0:
			flag = 1
	hash_rows = {}
	hash_columns = {}
	for i in range(n):
		hash_rows[i] = False
	for i in range(m):
		hash_columns[i] = False
	for i in range(n):
		for j in range(m):
			if l[i][j] == 1:
				hash_rows[i] = True
				hash_columns[j] = True
	for i in range(n):
		for j in range(m):
			if l[i][j] == 0:
				if hash_rows[i] == True or hash_columns[j] == True:
					l[i][j] = 1
				elif flag == 1:
					l[i][j] = 2
				else:
					l[i][j] = -1
			else:
				l[i][j] = 0
	for i in range(n):
		print(*l[i])


t = int(input())
while t > 0:
	t -= 1
	(n, m) = [int(x) for x in input().split()]
	row = [0] * n
	col = [0] * m
	arr = []
	for i in range(n):
		temp = [0] * m
		arr.append([0] * m)
		s = str(input())
		for j in range(m):
			p = int(s[j])
			arr[i][j] = p
			row[i] = max(row[i], p)
			col[j] = max(col[j], p)
	if row.count(1) == 0 and col.count(1) == 0:
		for i in range(n):
			print(*[-1] * m)
		continue
	else:
		for i in range(n):
			temp = []
			for j in range(m):
				if arr[i][j] == 1:
					temp.append(0)
				elif row[i] == 1 or col[j] == 1:
					temp.append(1)
				else:
					temp.append(2)
			print(*temp)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	l = [[int(x) for x in input().strip()] for y in range(x)]
	ans = [[2] * y for k in range(x)]
	temp = 0
	rows = set()
	col = set()
	for i in range(x):
		for j in range(y):
			if l[i][j] == 1:
				temp = 1
				rows.add(i)
				col.add(j)
				ans[i][j] = 0
	for ii in rows:
		for var in range(y):
			if ans[ii][var] == 2:
				ans[ii][var] = 1
	for jj in col:
		for var in range(x):
			if ans[var][jj] == 2:
				ans[var][jj] = 1
	if temp == 0:
		ans = [[-1] * y for k in range(x)]
	for li in ans:
		print(*li)


from heapq import heapify, heappush, heappop
from collections import deque
import sys
from math import gcd, sqrt, log10, log2, floor, ceil, sqrt
input = sys.stdin.readline

def il():
	return [int(a) for a in input().strip().split()]

def ip():
	return input().strip()

def ii():
	return int(input())

def ot(in_p, *args, e=' '):
	if type(in_p) == list:
		print(*in_p, end=e)
	if type(in_p) == str or type(in_p) == int:
		print(in_p, end=e)
		for l in args:
			print(l, end=e)
	print()

def a_pwr_n(a, n, mod):
	if n == 1:
		return a % mod
	if n % 2 == 0:
		val = a_pwr_n(a, n // 2, mod)
		return val % mod * val % mod
	else:
		return a % mod * a_pwr_n(a, n - 1, mod) % mod

def rg(*ag):
	if len(ag) == 3:
		return range(ag[0], ag[1], ag[2])
	elif len(ag) == 2:
		return range(ag[0], ag[1])
	elif len(ag) == 1:
		return range(ag[0])
	else:
		raise 'invalid input'

def ispal(i, j, st):
	while i <= j:
		if st[i] == st[j]:
			i = i + 1
			j = j - 1
		else:
			return False
	return True
mod = 1000000000 + 7
dx = [-1, -2, 1, 2, 2, -2, -1, 1]
dy = [2, 1, 2, 1, -1, -1, -2, -2]
for _ in range(ii()):
	(n, m) = il()
	mat = []
	f = True
	for __ in range(n):
		val = list(ip())
		mat.append(val)
		if '1' in val:
			f = False
	if f:
		for i in range(n):
			for j in range(m):
				print(-1, end=' ')
			print()
		continue
	dr = {}
	for i in range(n):
		if '1' in mat[i]:
			dr[i] = True
	dc = {}
	for i in range(m):
		for j in range(n):
			if mat[j][i] == '1':
				dc[i] = True
				break
	res = []
	for i in range(n):
		res.append([-1] * m)
	for i in range(n):
		for j in range(m):
			if mat[i][j] == '1':
				res[i][j] = 0
			elif i in dr or j in dc:
				res[i][j] = 1
			else:
				res[i][j] = 2
	for k in res:
		print(*k)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	mx = []
	r = []
	c = []
	z = 0
	for i in range(n):
		a = list(input())
		z += a.count('1')
		r.append(a.count('1'))
		mx.append(a)
	for i in range(m):
		for j in range(n):
			if mx[j][i] == '1':
				c.append(1)
				break
		else:
			c.append(0)
	if z == 0:
		ans = n * [m * [-1]]
		print('\n'.join((' '.join((str(i) for i in row)) for row in ans)))
	else:
		for i in range(n):
			a = ''
			for j in range(m):
				if mx[i][j] == '1':
					a += '0 '
				elif c[j] != 0 or r[i] != 0:
					a += '1 '
				else:
					a += '2 '
			print(a[:-1])


def findS(s, N, M):
	rows = []
	cols = []
	for i in range(M):
		temp = 0
		for j in range(N):
			temp = temp | s[j][i]
		cols.append(temp)
	for i in s:
		temp = 0
		for j in i:
			temp = temp | j
		rows.append(temp)
	y = []
	d = {0: 1, 1: 0}
	if rows.count(0) == N and cols.count(0) == M:
		return [[-1] * M] * N
	for i in s:
		temp = []
		for j in i:
			temp.append(d[j])
		y.append(temp)
	if 0 in rows and 0 in cols:
		for i in range(N):
			for j in range(M):
				if rows[i] == 0 and cols[j] == 0:
					y[i][j] = 2
	return y
T = int(input())
for i in range(T):
	(N, M) = [int(x) for x in input().split(' ')]
	(A, t) = ([], [])
	for j in range(N):
		t = [int(x) for x in input()]
		A.append(t)
	x = findS(A, N, M)
	for i in x:
		print(' '.join([str(x) for x in i]))


t = int(input())
for _ in range(t):
	flag = 0
	(n, m) = [int(x) for x in input().split()]
	A = [None] * n
	for i in range(n):
		sint = input()
		inp = [None] * m
		for j in range(m):
			inp[j] = int(sint[j])
		A[i] = inp
	B = [[-1 for j in range(m)] for i in range(n)]
	row = {}
	column = {}
	for i in range(n):
		row[i] = 0
	for j in range(m):
		column[j] = 0
	for i in range(n):
		for j in range(m):
			if A[i][j] == 1:
				flag = 1
				row[i] = 1
				column[j] = 1
				B[i][j] = 0
	if flag == 0:
		pass
	else:
		for i in range(n):
			for j in range(m):
				if A[i][j] == 0:
					if row[i] == 1 or column[j] == 1:
						B[i][j] = 1
					else:
						B[i][j] = 2
	for i in range(n):
		print(*B[i])


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	all_zero = True
	cols = [0 for _ in range(M)]
	rows = [0 for _ in range(N)]
	res = [['1' for _ in range(M)] for _ in range(N)]
	for i in range(N):
		A_row = input()
		for j in range(M):
			if A_row[j] == '1':
				all_zero = False
				cols[j] = 1
				rows[i] = 1
				res[i][j] = '0'
	for i in range(N):
		if all_zero:
			print('-1 ' * (M - 1) + '-1')
		else:
			if not rows[i]:
				res[i] = [str(2 - cols[j]) for j in range(M)]
			print(' '.join(res[i]))


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = [list(map(int, input())) for _ in range(n)]
	if all((a[i][j] == 0 for i in range(n) for j in range(m))):
		for i in range(n):
			print(*[-1] * m)
	else:
		b = [[2] * m for _ in range(n)]
		rows = set((i for i in range(n) for j in range(m) if a[i][j]))
		cols = set((j for i in range(n) for j in range(m) if a[i][j]))
		for i in range(n):
			print(*(0 if a[i][j] else 1 if i in rows or j in cols else 2 for j in range(m)))


for _ in range(int(input())):
	(M, N) = map(int, input().split())
	arr = [[1 for i in range(N)] for j in range(M)]
	zero_cols = []
	zero_rows = [0] * N
	arr2 = []
	for i in range(M):
		col = input()
		if '1' not in col:
			zero_cols.append(i)
		temp = []
		for dig in col:
			temp.append(int(dig))
		for k in range(len(temp)):
			zero_rows[k] |= temp[k]
		arr2.append(temp)
	if len(zero_cols) == M:
		for i in range(M):
			print('-1 ' * N)
		continue
	for i in range(M):
		for j in range(N):
			arr[i][j] ^= arr2[i][j]
	for i in zero_cols:
		for j in range(len(zero_rows)):
			if zero_rows[j] == 0:
				arr[i][j] += 1
	for col in arr:
		print(*col, sep=' ')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	q = [[0] * m for i in range(n)]
	a = [input() for i in range(n)]
	r = [0] * n
	c = [0] * m
	for i in range(n):
		for j in range(m):
			if a[i][j] == '1':
				r[i] = 1
				c[j] = 1
	if sum(r) + sum(c) > 0:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '0':
					if r[i] == 0 and c[j] == 0:
						q[i][j] = 2
					else:
						q[i][j] = 1
			print(*q[i])
	else:
		for i in range(n):
			for j in range(m):
				q[i][j] = -1
			print(*q[i])


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = []
	for _ in range(n):
		s = input()
		arr.append(s)
	res = [[-1] * m for i in range(n)]
	r = [0] * n
	col = [0] * m
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '1':
				r[i] = 1
				col[j] = 1
	if sum(r) + sum(col) != 0:
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '1':
					res[i][j] = 0
				elif r[i] == 1 or col[j] == 1:
					res[i][j] = 1
				else:
					res[i][j] = 2
	for i in res:
		print(*i)


from functools import reduce
from operator import or_
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = [list(map(int, input())) for _ in range(n)]
	r = [reduce(or_, x) for x in a]
	c = [reduce(or_, x) for x in zip(*a)]
	if not any(r):
		for i in range(n):
			print(*[-1] * m)
	else:
		for i in range(n):
			print(*(0 if a[i][j] else 1 if r[i] | c[j] else 2 for j in range(m)))


from functools import reduce
from operator import or_
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = [list(map(int, input())) for _ in range(n)]
	r = [reduce(or_, x) for x in a]
	if any(r):
		c = [reduce(or_, x) for x in zip(*a)]
		for (x, row) in zip(r, a):
			print(*((u, x | y, 1).index(1) for (y, u) in zip(c, row)))
	else:
		for _ in r:
			print(*[-1] * m)


T = int(input())
for _ in range(T):
	(N, M) = map(int, input().split())
	R = [False for i in range(N)]
	C = [False for i in range(M)]
	mat = []
	for n in range(N):
		r = list(input().strip())
		for m in range(M):
			if r[m] == '1':
				R[n] = True
				C[m] = True
		mat.append(r)
	if sum(R) or sum(C):
		for n in range(N):
			for m in range(M):
				if mat[n][m] == '1':
					mat[n][m] = '0'
				elif R[n] or C[m]:
					mat[n][m] = '1'
				else:
					mat[n][m] = '2'
		for row in mat:
			print(*row, sep=' ')
	else:
		for i in range(N):
			print(*[-1] * M, sep=' ')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	dp = [[0 for i in range(m)] for i in range(n)]
	f = 0
	mat = []
	for i in range(n):
		l = list(input())
		l = [int(i) for i in l]
		mat.append(l)
		if 1 in l:
			f = 1
	if f == 0:
		for i in range(n):
			z = [-1] * m
			print(*z)
	else:
		bad_row = set()
		bad_col = set()
		for i in range(n):
			for j in range(m):
				if mat[i][j] == 1:
					dp[i][j] = 0
					bad_row.add(i)
					bad_col.add(j)
		for i in bad_row:
			for j in range(m):
				if mat[i][j] != 1:
					dp[i][j] = 1
		for j in bad_col:
			for i in range(n):
				if mat[i][j] != 1:
					dp[i][j] = 1
		for i in range(n):
			for j in range(m):
				if i not in bad_row and j not in bad_col:
					dp[i][j] = 2
		for i in range(n):
			print(*dp[i])


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = []
	r = [0] * n
	c = [0] * m
	for i in range(n):
		a.append(list(map(int, input())))
		for j in range(m):
			r[i] |= a[i][j]
			c[j] |= a[i][j]
	if all((x == 0 for x in r)):
		for i in range(n):
			print(*[-1] * m)
	else:
		for i in range(n):
			print(*(0 if a[i][j] else 1 if r[i] | c[j] else 2 for j in range(m)))


T = int(input())
for i in range(0, T):
	inputs = input().split()
	inputs = list(map(int, inputs))
	N = inputs[0]
	M = inputs[1]
	matrix = []
	output_matrix = [[-1 for q in range(0, M)] for p in range(0, N)]
	impossible = True
	for j in range(0, N):
		row = list(input())
		row = list(map(int, row))
		if 1 in row:
			impossible = False
		matrix.append(row)
	if impossible == True:
		for j in range(0, N):
			print(*output_matrix[j], sep=' ')
	else:
		for p in range(0, N):
			for q in range(0, M):
				if matrix[p][q] == 1:
					output_matrix[p][q] = 0
				elif 1 in matrix[p] or 1 in [row[q] for row in matrix]:
					output_matrix[p][q] = 1
				else:
					output_matrix[p][q] = 2
		for j in range(0, N):
			print(*output_matrix[j], sep=' ')


t = int(input())
while t > 0:
	(n, m) = map(int, input().split(' '))
	matrix = [[0 for i in range(m)] for j in range(n)]
	fin = [[0 for i in range(m)] for j in range(n)]
	check = [[0 for i in range(m)] for j in range(n)]
	go = [[0 for i in range(m)] for j in range(n)]
	res = []
	ans = []
	for i in range(0, n):
		ch = input()
		ch = list(ch)
		for j in range(0, m):
			matrix[i][j] = int(ch[j])
	res.insert(0, [sum(x) for x in matrix])
	res.insert(1, [sum(x) for x in zip(*matrix)])
	for i in range(0, n):
		for j in range(0, m):
			fin[i][j] = res[0][i] + res[1][j]
	ans.insert(0, [sum(x) for x in fin])
	ans.insert(1, [sum(x) for x in zip(*fin)])
	for i in range(0, n):
		for j in range(0, m):
			check[i][j] = ans[0][i] + ans[1][j]
	for i in range(0, n):
		for j in range(0, m):
			if matrix[i][j] == 1:
				go[i][j] = 0
			elif matrix[i][j] == 0:
				if fin[i][j] >= 1:
					go[i][j] = 1
				elif fin[i][j] == 0:
					if check[i][j] >= 1:
						go[i][j] = 2
					elif check[i][j] == 0:
						go[i][j] = -1
	for i in range(0, n):
		print(*go[i])
	t = t - 1


t = int(input())
while t > 0:
	t = t - 1
	(n, m) = map(int, input().split())
	L = []
	for i in range(n):
		L.append([int(j) for j in input()])
	p = []
	q = []
	res = [[-1] * m] * n
	for i in range(n):
		if 1 in L[i]:
			res[i] = [1] * m
	for i in range(m):
		if 1 in [j[i] for j in L]:
			for j in range(n):
				res[j][i] = 1
	if sum((sum(j) for j in L)) == 0:
		for i in range(n):
			print(*[-1] * m)
	else:
		for i in range(n):
			for j in range(m):
				if L[i][j] == 1:
					res[i][j] = 0
				if res[i][j] == -1:
					res[i][j] = 2
		for i in range(n):
			print(*res[i])


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	arr = []
	for _ in range(n):
		s = input()
		arr.append(s)
	ans = [[-1] * m for _ in range(n)]
	row = [0] * n
	col = [0] * m
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '1':
				row[i] = 1
				col[j] = 1
	if sum(row) + sum(col) != 0:
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '1':
					ans[i][j] = 0
				elif row[i] == 1 or col[j] == 1:
					ans[i][j] = 1
				else:
					ans[i][j] = 2
	for item in ans:
		print(*item)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = ['0'] * n
	check = 0
	for t in range(n):
		l1 = list(str(input()))
		l[t] = l1
		hello = set(l1)
		if '1' in hello:
			check = 1
	if check == 0:
		main = [['-1'] * m] * n
		for t in main:
			ans = ' '.join(t)
			print(ans)
	else:
		main = []
		for t in range(n):
			k = []
			for y in range(m):
				k.append('0')
			main.append(k)
		for y in range(n):
			for i in range(m):
				elem = l[y][i]
				if elem == '1':
					main[y][i] = '0'
				else:
					check1 = l[y]
					(c, fu) = (0, 0)
					if '1' in check1:
						main[y][i] = '1'
						c = 1
					if c == 0:
						check2 = [l[x][i] for x in range(n)]
						if '1' in check2:
							main[y][i] = '1'
							fu = 1
					if c == 0 and fu == 0:
						main[y][i] = '2'
		for t in main:
			ans = ' '.join(t)
			print(ans)


mod = 1000000007
read_int = lambda : int(input().strip())
read_str = lambda : input().strip()
read_str_arr = lambda : input().strip().split()
read_int_arr = lambda : [int(x) for x in input().strip().split()]

def solve():
	(N, M) = read_int_arr()
	r = set()
	c = set()
	rc = set()
	for i in range(N):
		row = input()
		for j in range(M):
			if row[j] == '1':
				r.add(i)
				c.add(j)
				rc.add(tuple([i, j]))
	ans = [[None] * M for _ in range(N)]
	for i in range(N):
		for j in range(M):
			if (i, j) in rc:
				ans[i][j] = 0
			elif i in r or j in c:
				ans[i][j] = 1
			elif len(rc) == 0:
				ans[i][j] = -1
			else:
				ans[i][j] = 2
	for row in ans:
		print(*row)
for _ in range(int(input())):
	solve()


t = int(input())
while t > 0:
	t -= 1
	(n, m) = input().split(' ')
	(n, m) = (int(n), int(m))
	a = []
	for i in range(n):
		row = input()
		r = list(row)
		for i in range(m):
			r[i] = int(r[i])
		a.append(r)
	s = 0
	r = []
	c = []
	for i in range(n):
		r.append(sum(a[i]))
		s += r[i]
	for i in range(m):
		v = 0
		for j in range(n):
			v += a[j][i]
		c.append(v)
	f = [[0 for x in range(m)] for y in range(n)]
	for i in range(n):
		for j in range(m):
			if s == 0:
				f[i][j] = '-1'
			elif a[i][j] != 0:
				f[i][j] = '0'
			elif r[i] != 0 or c[j] != 0:
				f[i][j] = '1'
			else:
				f[i][j] = '2'
	for i in range(n):
		print(' '.join(f[i]))


import copy
T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	lt = []
	for i in range(n):
		lt1 = input()
		lt1 = list(lt1)
		lt.append(lt1)
	lt2 = copy.deepcopy(lt)
	lto = n * [m * ['0']]
	if lt != lto:
		indl = []
		for i in range(n):
			if '1' in lt[i]:
				for j in range(m):
					if lt[i][j] == '1':
						indl.append(j)
				for j in range(m):
					if lt[i][j] == '0':
						lt2[i][j] = '1'
					else:
						lt2[i][j] = '0'
		for i in range(n):
			if '1' not in lt[i]:
				for j in indl:
					lt2[i][j] = '1'
				for j in range(m):
					if lt2[i][j] == '0':
						lt2[i][j] = '2'
		for i in lt2:
			print(' '.join(i))
	else:
		lto = n * [m * ['-1']]
		for i in lto:
			print(' '.join(i))


def row_has1(matrix, i):
	if '1' in matrix[i]:
		return True
	return False

def column_has1(matrix, i):
	a = any([row[i] == '1' for row in matrix])
	return a

def main():
	testcases = int(input())
	for i in range(testcases):
		(n, m) = [int(j) for j in input().strip().split()]
		matrix = []
		answer_matrix = []
		has1 = False
		for j in range(n):
			matrix.append(input().strip())
			answer_matrix.append([-1 for k in range(m)])
			if '1' in matrix[-1]:
				has1 = True
		if has1:
			for row_index in range(n):
				for column_index in range(m):
					if matrix[row_index][column_index] == '1':
						answer_matrix[row_index][column_index] = 0
					elif row_has1(matrix, row_index) or column_has1(matrix, column_index):
						answer_matrix[row_index][column_index] = 1
					else:
						answer_matrix[row_index][column_index] = 2
		for l in answer_matrix:
			print(' '.join([str(obj) for obj in l]))
main()


mod = 1000000007

def integer_list():
	return list(map(int, input().split()))
t = int(input())
for _ in range(t):
	(r, c) = integer_list()
	matix = [list(input()) for i in range(r)]
	row_ones = set()
	col_ones = set()
	for i in range(r):
		for j in range(c):
			if matix[i][j] == '1':
				row_ones.add(i)
				col_ones.add(j)
	ans = [[-1 for i in range(c)] for j in range(r)]
	if not row_ones and (not col_ones):
		for i in range(r):
			print(*ans[i])
	else:
		ans = [[2 for i in range(c)] for j in range(r)]
		for i in range(r):
			if i in row_ones:
				for j in range(c):
					if matix[i][j] == '1':
						ans[i][j] = '0'
					else:
						ans[i][j] = '1'
		for i in range(c):
			if i in col_ones:
				for j in range(r):
					if matix[j][i] == '1':
						ans[j][i] = '0'
					else:
						ans[j][i] = '1'
		for i in range(r):
			print(*ans[i])


for _ in range(int(input())):
	arr = []
	(N, M) = list(map(int, input().split()))
	for i in range(N):
		Z = input()
		arr.append(Z)
	ans = [[-1] * M for _ in range(N)]
	r = set()
	c = set()
	for i in range(N):
		for j in range(M):
			if arr[i][j] == '1':
				r.add(i)
				c.add(j)
	if len(c) != 0:
		for i in range(N):
			for j in range(M):
				if arr[i][j] == '1':
					ans[i][j] = 0
				elif i in r or j in c:
					ans[i][j] = 1
				else:
					ans[i][j] = 2
	for i in range(N):
		print(*ans[i])