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

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# cook your dish here\n\'\'\'\nExample Input\n1\n3 3\n010\n000\n001\nExample Output\n1 0 1\n2 1 1\n1 1 0\n\'\'\'\nt=int(input())\nfor i in range(t):\n n,m=map(int,input().split())\n l=[]\n for j in range(n):\n  mat=list(input())\n  for k in range(len(mat)):\n   mat[k]=int(mat[k])\n  l.append(mat)\n\n l1=[]\n \n q1=[]\n q2=[]\n for j in range(n):\n  q1.append(sum(l[j]))\n for j in range(m):\n  s=0\n  for k in range(n):\n   s+=l[k][j]\n  q2.append(s)\n p=[]\n for j in range(n):\n  p1=[]\n  for k in range(m):\n   p1.append([q1[j],q2[k]])\n  p.append(p1)\n check1=0\n check2=0\n for j in q1:\n  if(j!=0):\n   check1=1\n for j in q2:\n  if(j!=0):\n   check2=1\n if(check1==0 and check2==0):\n  for j in range(n):\n   for k in range(m):\n    print("-1",end=\' \')\n   print()\n else:\n  for j in range(n):\n   f=[]\n   for k in range(m):\n    if(p[j][k][0]>0 or p[j][k][1]>0):\n     if(l[j][k]==1):\n      f.append(0)\n     else:\n      f.append(1) \n    elif(p[j][k][0]==0 and p[j][k][1]==0):\n     f.append(2)\n   l1.append(f)\n     \n  for j in range(n):\n   for k in range(m):\n    print(l1[j][k],end=\' \')\n   print()\n    \n  \n '
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 14293