There is a building with $N$ floors (numbered $1$ through $N$ from bottom to top); on each floor, there are $M$ windows (numbered $1$ through $M$ from left to right). Let's denote the $j$-th window on the $i$-th floor by $(i, j)$.
All windows in the building need to be cleaned one by one in a specific order. You are given this order as a matrix $A$ with $N$ rows and $M$ columns; for each valid $i$ and $j$, the window $(i, j)$ must be cleaned on the $A_{N-i+1,j}$-th turn. 
Whenever we clean a window $(i, j)$, it becomes clean, but the dirty water used for cleaning it flows down to windows $(i-1, j-1)$, $(i-1, j)$ and $(i-1, j+1)$ (more precisely, to all of these windows which exist), so these windows become dirty again. Then, the water flows further down, so the same applies for other windows below: whenever a window $(i, j)$ becomes dirty, windows $(i-1, j-1)$, $(i-1, j)$ and $(i-1, j+1)$ become dirty as well. For example, cleaning a window $(5, 3)$ will make the window $(3, 2)$ dirty. The next window is cleaned only after water flows down completely. Note that each window is cleaned only once and there may be dirty windows at the end.
For each window, determine whether it will be clean after the cleaning process ends.

-----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$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.

-----Output-----
For each test case, print $N$ lines; each of them should contain a string with length $M$. For each valid $i$ and $j$, the $j$-th character on the $i$-th line should be '1' if the window $(N-i+1, j)$ is clean or '0' if it is dirty.

-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le N, M \le 10^3$
- $1 \le A_{i, j} \le N \cdot M$ for each valid $i, j$
- the elements of the matrix $A$ are pairwise distinct
- the sum of $N \cdot M$ over all test cases does not exceed $10^6$

-----Subtasks-----
Subtask #1 (50 points):
- $N, M \le 25$
- the sum of $N \cdot M$ over all test cases does not exceed $625$
Subtask #2 (50 points): original constraints

-----Example Input-----
1
3 4
1 3 7 10
9 2 4 11
8 12 5 6 

-----Example Output-----
1111
1001
0100

Difficulty: interview

Starter code:

Solutions:
try:
    t = int(input())
    while(t > 0):
        t -= 1
        n,m = list(map(int,input().split()))
        a = [list(map(int,input().split())) for _ in range(n)]
        dp = [[0 for _ in range(m)] for _ in range(n)]
        ans = [['0' for _ in range(m)] for _ in range(n)]
        for i in range(n):
            for j in range(m):
                if i-1 < n:
                    if 0 <= j-1 and j+1 < m:
                        dp[i][j] = max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])
                    elif j == 0:
                        dp[i][j] = max(dp[i-1][j],dp[i-1][j+1])
                    elif j == m-1:
                        dp[i][j] = max(dp[i-1][j-1],dp[i-1][j])
    
                if dp[i][j] > a[i][j]:
                    ans[i][j] = '0'
                else:
                    ans[i][j] = '1'
                    dp[i][j] = a[i][j]
    
        for i in ans:
            print(''.join(i))
except:
    pass

try:
    t = int(input())
    while(t > 0):
        t -= 1
        n,m = list(map(int,input().split()))
        a = [list(map(int,input().split())) for _ in range(n)]
        dp = [[0 for _ in range(m)] for _ in range(n)]
        ans = [['0' for _ in range(m)] for _ in range(n)]
        for i in range(n):
            for j in range(m):
                if i-1 < n:
                    if 0 <= j-1 and j+1 < m:
                        dp[i][j] = max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])
                    elif j == 0:
                        dp[i][j] = max(dp[i-1][j],dp[i-1][j+1])
                    elif j == m-1:
                        dp[i][j] = max(dp[i-1][j-1],dp[i-1][j])
    
                if dp[i][j] > a[i][j]:
                    ans[i][j] = '0'
                else:
                    ans[i][j] = '1'
                    dp[i][j] = a[i][j]
    
        for i in ans:
            print(''.join(i))
except:
    pass

# cook your dish here
for _ in range(int(input())):
    N,M=list(map(int,input().split()))
    matrix=[]
    for i in range(N):
        l=list(map(int,input().split()))
        matrix.append(l)
    for i in range(1,N):
        for j in range(M):
            if(j==0):
                if(max(matrix[i-1][j],matrix[i-1][j+1])>matrix[i][j]):
                    matrix[i][j]=max(matrix[i-1][j],matrix[i-1][j+1])
            elif(j==M-1):
                if(max(matrix[i-1][j],matrix[i-1][j-1])>matrix[i][j]):
                    matrix[i][j]=max(matrix[i-1][j],matrix[i-1][j-1])
            else:
                if(max(matrix[i-1][j-1],matrix[i-1][j],matrix[i-1][j+1])>matrix[i][j]):
                    matrix[i][j]=max(matrix[i-1][j-1],matrix[i-1][j],matrix[i-1][j+1])
    # print(matrix)
    ans=[[1]*M for i in range(N)]
    for i in range(1,N):
        for j in range(M):
            if(j==0):
                if(max(matrix[i-1][j],matrix[i-1][j+1])>=matrix[i][j]):
                    ans[i][j]=0
            elif(j==M-1):
                if(max(matrix[i-1][j],matrix[i-1][j-1])>=matrix[i][j]):
                    ans[i][j]=0
            else:
                if(max(matrix[i-1][j-1],matrix[i-1][j],matrix[i-1][j+1])>=matrix[i][j]):
                    ans[i][j]=0
    for i in range(N):
        for j in range(M):
            print(ans[i][j],end="")
        print()
                

# cook your dish here
from sys import stdin,stdout
import sys
from bisect import bisect_left,bisect_right

# stdin = open("input.txt", "r");
# stdout = open("output.txt", "w");

t= int(stdin.readline().strip())

for _ in range(t):
	n,m=stdin.readline().strip().split(' ')
	n,m=int(n),int(m)
	arr=[]
	for i in range(n):
		arr.append(list(map(int,stdin.readline().strip().split(' '))))
	ctr=0
	visited=[[False for i in range(m)] for j in range(n)]
	sol=[[1 for i in range(m)] for j in range(n)]
	pq=[]
	for i in range(n):
		for j in range(m):
			pq.append((arr[i][j],i,j))
	pq=sorted(pq,key=lambda e:(e[0]),reverse=True)
	# print(pq)
	for i in pq:
		cv,yo,xo=i
		if visited[yo][xo]:
			continue
		else:
			stack=[xo,yo]
			while len(stack):
				y=stack.pop(-1)
				x=stack.pop(-1)
				visited[y][x]=True
				ctr+=1
				sol[y][x]=0
				if x-1>=0 and x-1<m and y+1>=0 and y+1<n:
					if not visited[y+1][x-1]:
						stack.append(x-1)
						stack.append(y+1)
				if x>=0 and x<m and y+1>=0 and y+1<n:
					if not visited[y+1][x]:
						stack.append(x)
						stack.append(y+1)
				if x+1>=0 and x+1<m and y+1>=0 and y+1<n:
					if not visited[y+1][x+1]:
						stack.append(x+1)
						stack.append(y+1)
			sol[yo][xo]=1

	for i in sol:
		for j in i:
			stdout.write(str(j))
		stdout.write("\n")

for _ in range(int(input())):
	n,m = map(int,input().split())
	print('1'*m)
	prev  = tuple(map(int,input().split()))
	for i in range(1,n):
		s = ''
		now = tuple(map(int,input().split()))
		prev_ = []
		for j in range(m):
			temp = prev[j]
			if j+1<m and prev[j+1]>temp:
				temp = prev[j+1]
			if j>0 and prev[j-1]>temp:
				temp = prev[j-1]
			if temp<now[j]:
				s += '1'
				temp = now[j]
			else:
				s += '0'
			prev_.append(temp)
		prev = prev_
		print(s)


import numpy as np
for _ in range(int(input())):
	n,m = map(int,input().split())
	order = np.zeros((n*m,2),dtype=np.uint8)
	state = np.ones((n,m),dtype=np.uint8)*2
	for i in range(n):
		j = 0
		for e in map(int,input().split()):
			order[e-1] = i,j
			j+=1

	for i0 in range(n*m-1,-1,-1):
		i,j = order[i0]
		# print(i,j)
		if state[i,j]==2:#same as init or not changed till now
			# print(i,j)
			j_start = j_end = j
			state[i,j]=1#clean at the end
			while i<n-1:
				i+=1
				j_start = max(0,j_start-1)
				j_end = min(m,j_end+1)
				state[i,j_start:j_end+1] &= 1# if will be cleaned later not affected
				#if init or dirty marked as dirty set to 0
				# print(i,j_start,j_end)
	# print(order)
	for i in range(n):
		print(''.join(map(str,state[i])))

for _ in range(int(input())):
    N, M = list(map(int, input().split()))
    A = []
    B = [['1' for x in range(M)] for x in range(N)]
    C = [['1' for x in range(M)] for x in range(N)]

    for __ in range(N):
        A.append(list(map(int, input().split())))

    B[0] = A[0].copy()

    for i in range(1, N):
        p = i - 1
        for j in range(0, M):
            B[i][j] = A[p][j]
            B[i][j] = max(B[i][j], A[i][j])

            if j >= 1:
                B[i][j] = max(B[i][j], B[p][j - 1])

            if j <= M - 2:
                B[i][j] = max(B[i][j], B[p][j + 1])

    C[0] = [1] * M
    for i in range(1, N):
        for j in range(0, M):
            if B[i][j] > A[i][j]:
                C[i][j] = 0

    for i in range(0, N):
        for j in range(0, M):
            print(C[i][j], end='')
        print()


for _ in range(int(input())):
    N, M = list(map(int, input().split()))
    A = []
    B = [['0' for x in range(M)] for x in range(N)]
    for __ in range(N):
        A.append(input().split())

    D = {}
    for i in range(N):
        for j in range(M):
            D[int(A[i][j])] = (i, j)

    for k in range(1, N * M + 1):
        i, j = D[k]
        B[i][j] = '1'
        for p in range(i + 1, N):
            diff = p - i
            for q in range(j - diff, j + diff + 1):
                if q >= 0 and q < M:
                    B[p][q] = '0'

    for i in B:
        for j in i:
            print(j, end='')
        print()


def arr_str(arr):
	strr=""
	for i in arr:
		strr+=str(i)
	return(strr)
for _ in range(int(input())):
	n,m=map(int,input().split())
	A=[0]*n
	for i in range(n):
		temp=[0]
		B=[int(i) for i in input().split()]
		temp.extend(B)
		temp.append(0)
		A[i]=temp

	# print(A)
	ans=[[0 for i in range(m)] for j in range(n)]
	dp=[[0 for i in range(m+2)] for j in range(n)]
	for i in range(m):
		dp[0][i+1]=A[0][i+1]
	for i in range(1,n):
		for j in range(1,m+1):
			temp=max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])
			# print(temp)
			if(temp>A[i][j]):
				ans[i-1][j-1]=0
			else:
				ans[i-1][j-1]=1
			temp=max(temp,A[i][j])
			dp[i][j]=temp
	tem=[1]*m
	
	print(arr_str(tem),end="\n")
	for i in range(n-1):
		print(arr_str(ans[i]),end="\n")

	

t=int(input())
for i in range(0,t):
    n,m = map(int, input().split())
    arr = []
    ans = []
    for i in range (0,n):
        in_arr = list(map(int , input().split()))
        if i == 0:
            
            x= '1'*m
        else :
            x = ""
            for j in range ( 0, m):
                arr2 = [0]*m
                if j==0 :
                    maxv = max(ans[i-1][0],ans[i-1][1])
                    
                elif j== m-1 :
                    maxv = max(ans[i-1][m-2],ans[i-1][m-1])
                    
                else :
                    maxv = max(ans[i-1][j-1] , ans[i-1][j] , ans[i-1][j+1])
                if (maxv > in_arr[j]):
                        in_arr[j] = maxv 
                        x=x+'0'
                else:
                        x=x+'1'
        ans.append(in_arr)
        arr.append(x)
    for i in range (0,n):
        print(arr[i])
            
        
            



t=int(input())
for _ in range(t):
    n,m=map(int,input().split())
    l=[]
    for i in range(n):
        l.append(list(map(int,input().split())))
    k=[]
    for i in range(n):
        for j in range(m):
            k.append([l[i][j],i,j])
    k.sort()
    s=[]
    for i in range(n):
        s.append([0]*m)
    for i in k:
        x=i[1]
        y=i[2]
        s[x][y]=1
        c=1
        #print(x,y)
        for j in range(x+1,n):
            s[j][y]=0
            for b in range(y-1,max(y-c-1,-1),-1):
                s[j][b]=0
            for b in range(y+1,min(m,y+c+1)):
                s[j][b]=0
            c+=1
        
    for i in s:
        print("".join(map(str,i)))
    
                


T = int(input())
while(T>0):
    N,M = [int(x) for x in input().split()]
    arr = [[int(x) for x in input().split()] for y in range(N)]
    ans = [['1' for x in range(M)] for y in range(N)]
    for i in range(1,N):
        for j in range(M):
            m = max(arr[i][j],arr[i-1][j])
            if(j-1>=0):
                m = max(m,arr[i-1][j-1])
            if(j+1<M):
                m = max(m,arr[i-1][j+1])
            if(arr[i][j]!=m):
                ans[i][j]='0'
            arr[i][j]=m
    for i in range(N):
        print(''.join(ans[i]))
    T-=1

#code
#code
t=int(input())
for i in range(t):
    #n,k=list(map(int,input().split()))
    arr=[]
    n,m=map(int,input().split())
    z=['1' for i in range(m)]
    arr.append(list(map(int,input().split())))
    print("".join(z))
    for i in range(1,n):
        x=''
        arr.append(list(map(int,input().split())))
        if arr[i][0]>arr[i-1][0] and arr[i][0]>arr[i-1][1]:
            x+='1'
        else:
            x+='0'
        arr[i][0]=max(arr[i-1][0],arr[i][0],arr[i-1][1])
        for j in range(1,m-1):
            if arr[i][j]>arr[i-1][j] and arr[i][j]>arr[i-1][j+1] and arr[i][j]>arr[i-1][j-1]:
                x+='1'
            else:
                x+='0'
            arr[i][j]=max(arr[i-1][j],arr[i][j],arr[i-1][j+1],arr[i-1][j-1])
        if arr[i][m-1]>arr[i-1][m-1] and arr[i][m-1]>arr[i-1][m-2]:
            x+='1'
        else:
            x+='0'
        arr[i][m-1]=max(arr[i-1][m-2],arr[i][m-1],arr[i-1][m-1])
        print(x)
        

from sys import stdin    
t=int(stdin.readline())
while t:
    n,m=map(int,stdin.readline().split())
    a=[]
    k=[[1 for _ in range(m)]for __ in range(n)]
    for _ in range(n):
        a.append(list(map(int,stdin.readline().split())))
    dp=[[a[i][_] for _ in range(m)]for i in range(n)]
    for i in range(1,n):
        for j in range(m):
            if j==m-1:
                dp[i][j]=max(0,max(dp[i-1][j-1],dp[i-1][j]))
            elif j==0:
                dp[i][j]=max(0,max(dp[i-1][j],dp[i-1][j+1]))
                
            else:
                dp[i][j]=max(0,max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1]))
                
            if dp[i][j]>a[i][j]:
                k[i][j]=0
            else:
                k[i][j]=1
                dp[i][j]=a[i][j]
            
    for i in range(n):
        for j in range(m):
            print(k[i][j],end='')
        print()
            
        
    t-=1

    
    


R = lambda : map(int, input().split())

t, = R()
for _ in range(t):
  n, m = R()
  b = [0] * m 
  for _ in range(n):
    a = list(R())
    b = list(map(max, a, [0] + b, b, b[1:] + [0]))
    print(''.join('10'[x < y] for x, y in zip(a, b)))


R = lambda : map(int, input().split())

t, = R()
for _ in range(t):
  n, m = R()
  b = (0,) * m 
  for _ in range(n):
    a = *R(),
    b = *map(max, a, (0,) + b, b, b[1:] + (0,)),
    print(''.join('10'[x < y] for x, y in zip(a, b)))


R = lambda : map(int, input().split())

t, = R()
for _ in range(t):
  n, m = R()
  b = (0,) * m 
  for _ in range(n):
    a = tuple(R())
    b = *map(max, a, (0,) + b, b, b[1:] + (0,)),
    print(''.join('10'[x < y] for x, y in zip(a, b)))


R = lambda : map(int, input().split())

t, = R()
for _ in range(t):
  n, m = R()
  b = (0,) * m 
  for _ in range(n):
    a = *R(),
    b = *map(max, a, (0,) + b, b, b[1:] + (0,)),
    print(''.join('10'[x < y] for x, y in zip(a, b)))


R = lambda : map(int, input().split())

t, = R()
for _ in range(t):
  n, m = R()
  b = [0] * m 
  for _ in range(n):
    a = *R(),
    b = *map(max, a, (0, *b), b, (*b[1:], 0)),
    print(''.join('10'[x < y] for x, y in zip(a, b)))


R = lambda : map(int, input().split())

t, = R()
for _ in range(t):
  n, m = R()
  b = [0] * m 
  for _ in range(n):
    a = list(R())
    b = list(map(max, a, [0] + b, b, b[1:] + [0]))
    print(''.join('10'[x < y] for x, y in zip(a, b)))


t = int(input())
for i in range(t):
	n, m = map(int, input().split())
	arr = []
	for j in range(n):
		arr += [[int(j) for j in input().split()]]
	# print(arr)

	dp = [[0 for i in range(m)] for j in range(n)]
	ans = [[0 for i in range(m)] for j in range(n)]

	for i in range(m):
		dp[0][i] = arr[0][i]
		ans[0][i] = 1

	if m == 1:
		for i in range(1, n):
			dp[i][0] = max(dp[i-1][0], arr[i][0])
			if dp[i][0] == arr[i][0]:
				ans[i][0] = 1
		# print(dp)
		for x in ans:
			print(*x, sep='')
		continue

	for i in range(1, n):
		dp[i][0] = max(dp[i-1][0], dp[i-1][1], arr[i][0])
		if dp[i][0] == arr[i][0]:
			ans[i][0] = 1

		for j in range(1, m-1):
			dp[i][j] = max(dp[i-1][j], dp[i-1][j-1], dp[i-1][j+1], arr[i][j])
			if dp[i][j] == arr[i][j]:
				ans[i][j] = 1

		dp[i][m-1] = max(dp[i-1][m-1], dp[i-1][m-2], arr[i][m-1])
		if dp[i][m-1] == arr[i][m-1]:
			ans[i][m-1] = 1

	for x in ans:
		print(*x, sep='')


# cook your dish here
t = int(input())

for _ in range(t):
	n, m = map(int, input().split())

	get_ups = lambda r, c: [(r-1, c + i) for i in [1, 0, -1] if 0 <= c+i < m]
	grid = [list(map(int, input().split())) for _ in range(n)]
	res = [[0]*m for _ in range(n)]
	res[0] = ['1']*m 
	for i in range(1, n):
		for j in range(m):
			t = 1
			for r, c in get_ups(i, j):
				if grid[r][c] > grid[i][j]:
					grid[i][j] = grid[r][c]
					t = 0
			res[i][j] = '1' if t else '0'
	for r in res:
		print(''.join(r))

R = lambda : map(int, input().split())
f = lambda x: ([0] + x, x, x[1:] + [0])

t, = R()
for _ in range(t):
  n, m = R()
  a = b = [0] * m 
  for _ in range(n):
    a = list(map(max, *f(a), *f(b)))
    b = list(R())
    print(''.join('01'[x < y] for x, y in zip(a, b)))