Polygon is not only the best platform for developing problems but also a square matrix with side $n$, initially filled with the character 0.

On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $2n$ cannons were placed. [Image] Initial polygon for $n=4$. 

Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.

More formally:   if a cannon stands in the row $i$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($i, 1$) and ends in some cell ($i, j$);  if a cannon stands in the column $j$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($1, j$) and ends in some cell ($i, j$). 

For example, consider the following sequence of shots: [Image] 

 1. Shoot the cannon in the row $2$.                         2. Shoot the cannon in the row $2$.                         3. Shoot the cannon in column $3$. 

You have a report from the military training on your desk. This report is a square matrix with side length $n$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?

Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow.

Each test case starts with a line containing an integer $n$ ($1 \le n \le 50$) — the size of the polygon.

This is followed by $n$ lines of length $n$, consisting of 0 and 1 — the polygon matrix after the training.

The total area of the matrices in all test cases in one test does not exceed $10^5$.


-----Output-----

For each test case print:  YES if there is a sequence of shots leading to a given matrix;  NO if such a sequence does not exist. 

The letters in the words YES and NO can be printed in any case.


-----Example-----
Input
5
4
0010
0011
0000
0000
2
10
01
2
00
00
4
0101
1111
0101
0111
4
0100
1110
0101
0111

Output
YES
NO
YES
YES
NO



-----Note-----

The first test case was explained in the statement.

The answer to the second test case is NO, since a 1 in a cell ($1, 1$) flying out of any cannon would continue its flight further.

Difficulty: EASY
Tags: Dynamic programming

Graph algorithms

Implementation

Shortest paths
Skill types: Dynamic programming

Starter code:

Solutions:
t = int(input())
while t > 0:
	n = int(input())
	l = []
	for i in range(n):
		ls = input()
		l.append(ls)
	flag = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if (i != n - 1 and l[i + 1][j] != '1') and (j != n - 1 and l[i][j + 1] != '1'):
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = []
	flag = 0
	for i in range(n):
		a.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				flag = 1
	if flag:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	grid = [list(map(int, list(input()))) + [1] for _ in range(n)]
	grid.append([1] * n)
	possible = True
	for i in range(2 * (n - 1), -1, -1):
		for x in range(n):
			y = i - x
			if y >= n or y < 0:
				continue
			if grid[y][x] == 1 and grid[y + 1][x] == 0 and (grid[y][x + 1] == 0):
				possible = False
	if possible:
		print('YES')
	else:
		print('NO')


k = int(input())
for f in range(k):
	n = int(input())
	a = [[int(i) for i in input()] for j in range(n)]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if not (a[i][j] == 1 and (a[i + 1][j] == 1 or a[i][j + 1] == 1) or a[i][j] == 0):
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


def fun(a, n):
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1:
					pass
				elif a[i][j + 1] == '1' or a[i + 1][j] == '1':
					pass
				else:
					return False
	return True
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = []
	for i in range(n):
		b = input()
		a.append(list(b))
	if fun(a, n):
		print('YES')
	else:
		print('NO')


import sys
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right
from collections import deque
from collections import Counter
from itertools import accumulate, combinations, permutations
from heapq import heapify, heappop, heappush
from functools import lru_cache

def input():
	return sys.stdin.readline()[:-1]

def printl(li):
	_ = print(*li, sep='\n') if li else None

def argsort(s, return_sorted=False):
	inds = sorted(range(len(s)), key=lambda k: s[k])
	if return_sorted:
		return (inds, [s[i] for i in inds])
	return inds

def alp2num(c, cap=False):
	return ord(c) - 97 if not cap else ord(c) - 65

def num2alp(i, cap=False):
	return chr(i + 97) if not cap else chr(i + 65)

def matmat(A, B):
	(K, N, M) = (len(B), len(A), len(B[0]))
	return [[sum([A[i][k] * B[k][j] for k in range(K)]) for j in range(M)] for i in range(N)]

def matvec(M, v):
	(N, size) = (len(v), len(M))
	return [sum([M[i][j] * v[j] for j in range(N)]) for i in range(size)]

def T(M):
	(n, m) = (len(M), len(M[0]))
	return [[M[j][i] for j in range(n)] for i in range(m)]

def main():
	mod = 1000000007
	N = int(input())
	S = tuple((tuple(map(int, input())) for i in range(N)))
	n = N - 1
	for n in range(N - 2, -1, -1):
		for i in reversed(range(n + 1)):
			x = S[n][i]
			if x == 0:
				continue
			if x == 1:
				if S[n + 1][i] or S[n][i + 1]:
					pass
				else:
					print('NO')
					return
		for i in reversed(range(n + 1)):
			x = S[i][n]
			if x == 0:
				continue
			if x == 1:
				if S[i + 1][n] or S[i][n + 1]:
					pass
				else:
					print('NO')
					return
	print('YES')
q = int(input())
for _ in range(q):
	main()


for _ in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		row = input()
		matrix.append(row)
	pos = True
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif matrix[i + 1][j] == '1' or matrix[i][j + 1] == '1':
					continue
				else:
					pos = False
					break
	if pos:
		print('YES')
	else:
		print('NO')


import sys
from collections import Counter

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))
sys.setrecursionlimit(20000)
T = int(input())
for _ in range(T):
	n = int(input())
	A = []
	for i in range(n):
		a = list(map(int, sys.stdin.readline().strip()))
		A.append(a)
	for i in range(n - 1):
		break_outer = False
		for j in range(n - 1):
			if A[i][j] == 1 and A[i + 1][j] == 0 and (A[i][j + 1] == 0):
				print('NO')
				break_outer = True
				break
		if break_outer:
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = []
	u = 0
	for i in range(n):
		s = input()
		a.append(s)
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1 or a[i + 1][j] == '1' or (a[i][j + 1] == '1'):
					continue
				else:
					u = 1
					break
	if u == 1:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	p = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1:
					pass
				elif j == n - 1:
					pass
				elif a[i][j + 1] == '1' or a[i + 1][j] == '1':
					pass
				else:
					p = False
					break
	if p:
		print('YES')
	else:
		print('NO')


import sys
from collections import defaultdict
input = sys.stdin.readline

def print_result(l):
	return print(' '.join(map(str, l)))

def timer(function):
	import time

	def wrapper(*args, **kwargs):
		t1 = time.time()
		val = function()
		t2 = time.time()
		print('Executed \\{}\\ in {:.2f} seconds.'.format(function.__name__, t2 - t1))
		return val
	return wrapper

def solve():
	n = int(input())
	mat = [input().split('\n')[0] for i in range(n)]
	flag = True
	for i in range(n):
		if not flag:
			break
		for j in range(n):
			if mat[i][j] == '1':
				if i == n - 1 or j == n - 1 or mat[i + 1][j] == '1' or (mat[i][j + 1] == '1'):
					continue
				else:
					flag = False
					break
	print('YES' if flag else 'NO')
t = int(input())
for _ in range(t):
	solve()


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, grid):
	for r in range(N):
		for c in range(N):
			if grid[r][c]:
				if r == N - 1 or c == N - 1:
					continue
				if grid[r + 1][c] == 1 or grid[r][c + 1] == 1:
					continue
				return 'NO'
	return 'YES'
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	grid = [[int(x) for x in input().decode().rstrip()] for i in range(N)]
	ans = solve(N, grid)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	(polygon, toReturn) = ([], True)
	for _ in range(n):
		polygon.append([var for var in input()])
	for root in reversed(range(n - 1)):
		delta = 0
		while root - delta >= 0:
			if polygon[root][root - delta] == '1':
				if not (polygon[root + 1][root - delta] == '1' or polygon[root][root - delta + 1] == '1'):
					toReturn = False
					break
			if polygon[root - delta][root] == '1':
				if not (polygon[root - delta][root + 1] == '1' or polygon[root - delta + 1][root] == '1'):
					toReturn = False
					break
			delta += 1
	print('YES' if toReturn is True else 'NO')


import math
T = int(input())
for _ in range(0, T):
	n = int(input())
	L = []
	for i in range(0, n):
		s = input() + '1'
		L.append(s)
	tt = '1' * (n + 1)
	L.append(tt)
	temp = 'YES'
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if L[i][j] == '1':
				if L[i + 1][j] == '0' and L[i][j + 1] == '0':
					temp = 'NO'
					break
		if temp == 'NO':
			break
	print(temp)


for _ in range(int(input())):
	n = int(input())
	grid = [list(map(lambda x: x == '1', input())) for _ in range(n)]
	bad = False
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j]:
				if not (grid[i + 1][j] or grid[i][j + 1]):
					bad = True
					break
		if bad:
			break
	print('NO' if bad else 'YES')


for k in range(int(input())):
	a = int(input())
	t = []
	for k in range(a):
		t.append(list(input()))
	for i in range(a):
		h = 0
		for j in range(a):
			if t[i][j] == '1':
				if j == a - 1 or t[i][j + 1] == '1':
					pass
				elif i == a - 1 or t[i + 1][j] == '1':
					pass
				else:
					print('NO')
					h += 1
					break
		if h > 0:
			break
	if h == 0:
		print('YES')


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial, sqrt
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, permutations
from bisect import *
from functools import reduce
from operator import mul
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().strip('\n')
grid = lambda r: [lint() for i in range(r)]
localsys = 0
start_time = time.time()
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)

def ceill(n, x):
	return (n + x - 1) // x
T = 1

def solve():
	n = int(input())
	(arr, ans) = ([S() for _ in range(n)], 'YES')
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1' and (not (arr[i + 1][j] == '1' or arr[i][j + 1] == '1')):
				ans = 'NO'
	print(ans)

def run():
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
run()
T = I() if T else 1
for _ in range(T):
	solve()
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


for _ in range(int(input())):
	f = 1
	n = int(input())
	a1 = [''] * n
	for i in range(n):
		a1[i] = input()
	for i in range(n - 1):
		for j in range(n - 1):
			if a1[i][j] == '1':
				if a1[i][j + 1] == '0' and a1[i + 1][j] == '0':
					f = 0
					break
		if not f:
			break
	if f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	board = [input() for _ in range(n)]
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			if board[i][j] != '1':
				continue
			if board[i + 1][j] != '1' and board[i][j + 1] != '1':
				flag = True
				break
		if flag:
			break
	if flag:
		print('NO')
		continue
	print('YES')


for _ in range(int(input())):
	n = int(input())
	s = []
	for _ in range(n):
		c = input()
		s.append([])
		for x in c:
			s[-1].append(int(x))
	gone = set()
	q = []
	for i in range(n):
		q.append((n - 1, i))
		q.append((i, n - 1))
	while len(q) != 0:
		(a, b) = q.pop()
		if (a, b) in gone or a < 0 or b < 0 or (s[a][b] == 0):
			continue
		gone.add((a, b))
		q.append((a - 1, b))
		q.append((a, b - 1))
	SUM = 0
	for x in s:
		SUM += sum(x)
	if SUM == len(gone):
		print('YES')
	else:
		print('NO')


def solve(n, rows):
	for i in range(n - 1):
		for j in range(n - 1):
			cell = rows[i][j]
			if cell == '0':
				continue
			elif rows[i + 1][j] == '0' and rows[i][j + 1] == '0':
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	rows = []
	for i in range(n):
		rows.append(input())
	print(solve(n, rows))


def codefun():
	n = int(input())
	temp = [0] * n
	matrix = []
	for i in range(n):
		s = input()
		for i in range(n):
			temp[i] = int(s[i])
		matrix.append(temp[:])
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == 1:
				if matrix[i + 1][j] == 0 and matrix[i][j + 1] == 0:
					return 'NO'
	return 'YES'
for _ in range(int(input())):
	print(codefun())


from math import sqrt
from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for t in range(test):
		n = int(input())
		matrix = []
		for i in range(n):
			s = list(input().strip())
			matrix.append(s)
		flag = True
		for i in range(n - 1):
			for j in range(n - 1):
				if matrix[i][j] == '1':
					if not (matrix[i + 1][j] == '1' or matrix[i][j + 1] == '1'):
						flag = False
						print('NO')
						break
			if not flag:
				break
		if flag:
			print('YES')
main()


t = int(input())
for k in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(map(int, input())))
	bool = True
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if mat[i][j] == 1:
				if mat[i + 1][j] == 1 or mat[i][j + 1] == 1:
					continue
				else:
					bool = False
					break
	if bool == False:
		print('NO')
	else:
		print('YES')


T = int(input())
for _ in range(T):
	N = int(input())
	cell = [list(map(int, list(input()))) for _ in range(N)]

	def dfs(x, y):
		if x < 0 or y < 0:
			return
		if cell[y][x] == 0:
			return
		cell[y][x] = 0
		dfs(x, y - 1)
		dfs(x - 1, y)
	for x in range(N):
		dfs(x, N - 1)
	for y in range(N):
		dfs(N - 1, y)
	for c in cell:
		if max(c) == 1:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for o in range(t):
	f = list()
	n = int(input())
	for i in range(n):
		s = [int(i) for i in input()]
		f += [s + [1]]
	f += [[1] * (n + 1)]
	g = 1
	for i in range(n):
		for j in range(n):
			if f[i][j] == 1 and f[i][j + 1] + f[i + 1][j] == 0:
				g = 0
	if g:
		print('yes')
	else:
		print('no')


import sys
import math
import itertools
import random
from collections import deque

def solve():
	n = int(input())
	a = [input() for _ in range(n)]
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				x = 0
				y = 0
				if i == n - 1:
					x = 1
				elif a[i + 1][j] == '1':
					x = 1
				if j == n - 1:
					y = 1
				elif a[i][j + 1] == '1':
					y = 1
				if x == 0 and y == 0:
					print('NO')
					return
	print('YES')
t = int(input())
while t > 0:
	solve()
	t -= 1


from __future__ import division, print_function
from collections import *
from math import *
from itertools import *
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1':
				if arr[i][j + 1] == '0' and arr[i + 1][j] == '0':
					print('NO')
					return
	print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	main()
	t -= 1


import sys

def readLn():
	return sys.stdin.readline().strip()

def write(a):
	sys.stdout.write(a)

def solve(a, n):
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if i + 1 < n:
					if a[i + 1][j] == 1:
						a[i][j] = 0
				else:
					a[i][j] = 0
				if j + 1 < n:
					if a[i][j + 1] == 1:
						a[i][j] = 0
				else:
					a[i][j] = 0
		if 1 in a[i]:
			return False
	return True

def main():
	t = int(readLn())
	ans = [0] * t
	for test in range(t):
		n = int(readLn())
		a = [[int(j) for j in readLn()] for i in range(n)]
		if solve(a, n):
			ans[test] = 'YES'
		else:
			ans[test] = 'NO'
	write('\n'.join(ans))
main()


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def is_prime(n):
	if n == 1:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def solve():
	n = int(inp())
	grid = []
	for i in range(0, n):
		a = list(inp())
		for i in range(0, n):
			a[i] = int(a[i])
		grid.append(a)
	hori = []
	veti = []
	for i in range(0, n):
		a = [0] * n
		hori.append(a)
		a = [0] * n
		veti.append(a)
	for j in range(0, n):
		for i in range(n - 1, -1, -1):
			if i == n - 1:
				veti[i][j] = grid[i][j]
			else:
				veti[i][j] = grid[i][j] + veti[i + 1][j]
	for i in range(0, n):
		for j in range(n - 1, -1, -1):
			if j == n - 1:
				hori[i][j] = grid[i][j]
			else:
				hori[i][j] = grid[i][j] + hori[i][j + 1]
	f = True
	for i in range(0, n - 1):
		if f == False:
			break
		for j in range(0, n - 1):
			if grid[i][j] == 1:
				if veti[i][j] > veti[i + 1][j] and grid[i + 1][j] != 0 or (hori[i][j] > hori[i][j + 1] and grid[i][j + 1] != 0):
					continue
				else:
					f = False
					break
	if f:
		print('YES')
	else:
		print('NO')
testcase(int(inp()))


t = int(input())
for _ in range(t):
	n = int(input())
	sl = []
	for i in range(n):
		sl.append(input())
	polm = []
	for i in range(n):
		polm.append([])
		for j in range(n):
			polm[i].append(int(sl[i][j]))
	p = 0
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if polm[i][j] == 1:
				if i == n - 1 or j == n - 1 or polm[i + 1][j] == 1 or (polm[i][j + 1] == 1):
					continue
				else:
					p = 1
					print('NO')
					break
		if p == 1:
			break
	if p == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	d = []
	a = []
	for i in range(n - 1):
		x = list(map(int, input()))
		for j in range(n - 1):
			if x[j] == 1 and x[j + 1] == 0:
				d.append((i, j))
		a.append(x)
	x = list(map(int, input()))
	a.append(x)
	res = 'YES'
	for (i, j) in d:
		if a[i + 1][j] == 0:
			res = 'NO'
			break
	print(res)


for _ in range(int(input())):
	n = int(input())
	matrix = [input() for r in range(n)]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1':
				if matrix[i + 1][j] == '0' and matrix[i][j + 1] == '0':
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	ok = True
	mask = [0] * n
	for r in range(n):
		row = list(map(int, input()))
		for (i, (s, f, m)) in enumerate(zip(row[1:], row[:-1], mask)):
			if f - m < 0:
				ok = False
			if f > s:
				mask[i] = 1
			else:
				mask[i] = 0
		if r == n - 1:
			break
	if ok:
		print('YES')
	else:
		print('NO')


import math
import time

def is_prime(n):
	if n <= 1:
		return False
	if n == 2:
		return True
	if n > 2 and n % 2 == 0:
		return False
	max_div = math.floor(math.sqrt(n))
	for i in range(3, 1 + max_div, 2):
		if n % i == 0:
			return False
	return True
t = int(input())
for i in range(0, t):
	n = int(input())
	l = []
	for j in range(n):
		s = input()
		s += '0'
		l.append(s)
	f = 0
	for j in range(0, n):
		for k in range(0, n):
			if l[j][k] == '1' and (k + 1 == n or l[j][k + 1] == '1' or j + 1 == n or (l[j + 1][k] == '1')):
				continue
			elif l[j][k] == '1':
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('Yes')
	else:
		print('No')


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		s = list(input())
		l.append(s)
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					pass
				else:
					f = 1
					break
		if f == 1:
			break
	if f == 1:
		print('NO')
	else:
		print('YES')


def main():
	n = int(input())
	q = [[int(k) for k in input()] for o in range(n)]
	for i in range(len(q) - 1):
		for o in range(len(q[0]) - 1):
			if q[i][o] and (not q[i + 1][o]) and (not q[i][o + 1]):
				print('NO')
				return
	print('YES')
for i in range(int(input())):
	main()


T = int(input())
for _ in range(T):
	n = int(input())
	s = []
	for _ in range(n):
		t = input()
		s.append(t)
	valid = True
	for i in range(n):
		for j in range(n):
			if s[i][j] == '1':
				if not ((j + 1 == n or s[i][j + 1] == '1') or (i + 1 == n or s[i + 1][j] == '1')):
					valid = False
					break
		if not valid:
			break
	ans = 'YES' if valid else 'NO'
	print(ans)


t = int(input())
while t:
	mat = []
	n = int(input())
	for i in range(n):
		mat.append([])
		mat[i].extend(input())
	poss = True
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1' and mat[i + 1][j] == '0' and (mat[i][j + 1] == '0'):
				poss = False
	if poss:
		print('YES')
	else:
		print('NO')
	t -= 1


N = int(input())
answers = []
for i in range(N):
	n = int(input())
	arr = []
	for k in range(n):
		arr1 = list(map(int, list(input())))
		arr.append(arr1)
	flag = True
	for y in range(n - 1):
		for x in range(n - 1):
			if arr[y][x]:
				if arr[y + 1][x] or arr[y][x + 1]:
					continue
				else:
					flag = False
					break
	if flag:
		answers.append('YES')
	else:
		answers.append('NO')
for i in answers:
	print(i)


for _ in range(int(input())):
	n = int(input())
	m = [input() for _ in range(n)]
	mark = 0
	mark1 = 0
	count = 0
	count1 = 0
	for i in range(n):
		for j in range(n):
			if m[i][j] == '1':
				if i == n - 1 or j == n - 1:
					pass
				elif m[i][j + 1] == '0' and m[i + 1][j] == '0':
					print('NO')
					mark = 1
					break
		if mark == 1:
			break
	if mark == 0:
		print('YES')


import math

def answer(n, A):
	for i in range(n):
		for j in range(n):
			if i != n - 1 and j != n - 1:
				if A[i][j] == '1' and A[i + 1][j] != '1' and (A[i][j + 1] != '1'):
					return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	print(answer(n, l))


def five(n, pole):
	for i in range(n):
		for j in range(n):
			if pole[i][j] == '1':
				if i + 1 < n and j + 1 < n:
					right = pole[i][j + 1] == '0'
					left = pole[i + 1][j] == '0'
					if right and left:
						return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	pole = [input() for _ in range(n)]
	print(five(n, pole))


import math
import random
mod = 1000000007
r1 = lambda : int(input())
rm = lambda : map(int, input().split())
rls = lambda : list(rm())
test = r1()
while test:
	test -= 1
	n = r1()
	ls = []
	for i in range(n):
		ls.append(list(input()))
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if ls[i][j] == '1' and ls[i + 1][j] == '0' and (ls[i][j + 1] == '0'):
				flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import math
import sys

def checkmap(map):
	n = len(map)
	for i in range(n):
		for j in range(n):
			if map[i][j] == '1':
				if i < n - 1 and map[i + 1][j] == '0' and (j < n - 1) and (map[i][j + 1] == '0'):
					return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	map = [input() for i in range(n)]
	print(checkmap(map))


from __future__ import division, print_function
mod = int(1000000000.0 + 7)
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
from collections import Counter
import collections
import math
import heapq
import re

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split('')))

def sin():
	return input()

def inin():
	return int(input())

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def most_frequent(list):
	return max(set(list), key=list.count)

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def Convert(string):
	li = list(string.split(''))
	return li

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f[-1]
prime = []
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j

def nextPerfectSquare(N):
	nextN = math.floor(math.sqrt(N)) + 1
	return nextN * nextN

def main():
	for _ in range(inin()):
		n = inin()
		flag = 0
		arr = []
		for _ in range(n):
			arr.append(list(str(sin())))
		for i in range(n):
			for j in range(n):
				if arr[i][j] == '1':
					if i == n - 1 or j == n - 1:
						continue
					elif arr[i + 1][j] == '1' or arr[i][j + 1] == '1':
						continue
					else:
						flag = 1
		if flag == 1:
			print('NO')
		else:
			print('YES')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for j in range(n):
		matrix.append(input())
	flag = 0
	for j in range(n):
		for k in range(n):
			if matrix[j][k] == '1' and j < n - 1 and (k < n - 1):
				if j < n - 1 and matrix[j + 1][k] == '1' and (k < n - 1) or (matrix[j][k + 1] == '1' and j < n - 1 and (k < n - 1)):
					continue
				else:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


n = int(input())
for _ in range(n):
	dim = int(input())
	lis = []
	for i in range(dim):
		row = input()
		lis.append(row)

	def check(lis, dim):
		for j in range(dim):
			for k in range(dim):
				if lis[j][k] == '1':
					if j == dim - 1 or k == dim - 1:
						continue
					if lis[j][k + 1] == '1' or lis[j + 1][k] == '1':
						continue
					print('NO')
					return
		print('YES')
	check(lis, dim)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(input())):
	n = int(input())
	A = [input() for i in range(n)]
	valid = 1
	for i in range(n):
		for j in range(n):
			if A[i][j] == '1':
				if not ((i + 1 == n or A[i + 1][j] == '1') or (j + 1 == n or A[i][j + 1] == '1')):
					valid = 0
					break
		if valid == 0:
			break
	if valid:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	polygon = defaultdict(list)
	for i in range(n):
		polygon[i] = list(input())
	done = False
	for i in range(n - 1):
		for j in range(n - 1):
			if polygon[i][j] == '1':
				if polygon[i][j + 1] == '0' and polygon[i + 1][j] == '0':
					done = True
					break
		if done:
			break
	if done:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	t -= 1
	ok = 1
	n = int(input())
	g = [input() for _ in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			if g[i][j] == '1' and g[i + 1][j] == '0' and (g[i][j + 1] == '0'):
				ok = 0
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lis = []
	for i in range(n):
		lis.append(list(input()))
	z = 0
	for i in range(n):
		for j in range(n):
			if lis[i][j] == '1':
				f1 = 0
				f2 = 0
				if j < n - 1 and lis[i][j + 1] == '0':
					f1 = 1
				if i < n - 1 and lis[i + 1][j] == '0':
					f2 = 1
				if f1 == 1 and f2 == 1:
					z = 1
					break
		if z == 1:
			break
	if z == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	grid = []
	ok = True
	for i in range(n):
		temp = list(str(input()))
		grid.append(temp)
	for i in range(n - 1):
		if grid[0][i] == '1':
			if not (grid[0][i + 1] == '1' or grid[1][i] == '1'):
				ok = False
				break
	if ok:
		for i in range(n - 1):
			if grid[i][0] == '1':
				if not (grid[i + 1][0] == '1' or grid[i][1] == '1'):
					ok = False
					break
	if ok:
		for r in range(1, n - 1):
			for c in range(1, n - 1):
				if grid[r][c] == '1':
					if not (grid[r][c + 1] == '1' or grid[r + 1][c] == '1'):
						ok = False
						break
	if ok:
		print('YES')
	elif ok == False:
		print('NO')


from sys import stdin
import math

def readline():
	return stdin.readline()
tests = int(readline())

def solve(n, m):
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if m[i][j] == 1 and m[i + 1][j] != 1 and (m[i][j + 1] != 1):
				return 'NO'
	return 'YES'
for t in range(0, tests):
	n = int(readline())
	m = []
	for i in range(0, n):
		m.append(list(map(int, readline().rstrip('\n'))))
	print(solve(n, m))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	matrix = [list(input()) for j in range(n)]
	adj = [[0, 1], [1, 0]]
	answer = True
	for i in range(len(matrix)):
		for j in range(n):
			found = 0
			for (dx, dy) in adj:
				(nx, ny) = (j + dx, dy + i)
				if 0 <= ny < n and 0 <= nx < n and (matrix[i][j] == '1'):
					if matrix[ny][nx] == '0':
						found += 1
			if found == 2:
				answer = False
	if not answer:
		print('NO')
	else:
		print('YES')


tests = int(input())
for i in range(tests):
	n = int(input())
	lines = []
	for x in range(n):
		lines.append(input())
	result = 1
	for x in range(n - 1):
		if result != 1:
			break
		for y in range(n - 1):
			if lines[x][y] == '1':
				if lines[x + 1][y] == '0' and lines[x][y + 1] == '0':
					result = 0
					break
	if result == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for i in range(n):
		s = input()
		l = list(s)
		a.append(l)
	ans = 0
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					pass
				else:
					ans = 1
					print('NO')
					break
		if ans == 1:
			break
	if ans == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	p = [[int(i) for i in input()] for j in range(n)]
	foundWrong = False
	for i in range(n - 1):
		for j in range(n - 1):
			if p[i][j] == 1 and p[i + 1][j] == 0 and (p[i][j + 1] == 0):
				foundWrong = True
				break
		if foundWrong:
			break
	if foundWrong:
		print('NO')
	else:
		print('YES')


from typing import *

def is_valid_board(board: List[str]) -> bool:
	for (line_no, line) in enumerate(board):
		for (col_no, x) in enumerate(line):
			if x == '0':
				continue
			if line_no < len(board) - 1:
				if board[line_no + 1][col_no] == '1':
					continue
			else:
				continue
			if col_no < len(line) - 1:
				if line[col_no + 1] == '1':
					continue
			else:
				continue
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	board = []
	for _ in range(n):
		board.append(input())
	if is_valid_board(board):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	ls = []
	for i in range(n):
		ls.append(list(input()))
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if ls[i][j] == '1':
				if ls[i][j + 1] != '1' and ls[i + 1][j] != '1':
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from collections import Counter as C, defaultdict as D, deque as Q
from operator import itemgetter as I
from itertools import product as P
from bisect import bisect_left as BL, bisect_right as BR
from heapq import heappush as HPUSH, heappop as HPOP
from math import floor as MF, ceil as MC, gcd as MG, factorial as F, sqrt as SQRT, inf as INFINITY, log as LOG

def player1():
	print('')

def player2():
	print('')

def isPrime(n):
	for i in range(2, MC(SQRT(n)) + 1):
		if n % i == 0:
			return False
	return True

def main():
	for _ in range(int(input())):
		n = int(input())
		a = []
		for i in range(n):
			a.append(input())
		ans = 'YES'
		for (i, j) in P(range(n - 1, -1, -1), range(n - 1, -1, -1)):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				ans = 'no'
				break
		print(ans)
main()


t = int(input())
for case1 in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		x = list(input())
		arr.append(x)
	flag = True
	for i in range(n):
		for j in range(n):
			if i + 1 == n:
				flag = True
			elif j + 1 == n:
				flag = True
			elif arr[i][j] == '1':
				if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
					flag = False
					break
		if flag == False:
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l1 = [input() for i in range(n)]
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if l1[i][j] == '1' and l1[i + 1][j] != '1' and (l1[i][j + 1] != '1'):
				ans = 'NO'
		if ans == 'NO':
			break
	print(ans)


for _ in range(int(input())):
	x = int(input())
	myList = [list(input()) for i in range(x)]
	flag = True
	for i in range(len(myList)):
		for j in range(len(myList[0])):
			if i + 1 == x or j + 1 == x or myList[i][j] == 0:
				continue
			elif myList[i][j] == '1' and int(myList[i + 1][j]) + int(myList[i][j + 1]) < 1:
				flag = False
				break
	print('YES' if flag == True else 'NO')


n = int(input())
count = 0
lista = []
while count != n:
	x = int(input())
	for i in range(x):
		string = input()
		lista.append(string)
	count += 1
	flag = False
	for i in range(0, x - 1):
		for j in range(0, x - 1):
			if lista[i][j + 1] != '1' and lista[i + 1][j] != '1' and (lista[i][j] == '1'):
				flag = True
				break
	if flag == False:
		print('YES')
	else:
		print('NO')
	lista = []


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	M = [stdin.readline().strip() for _ in range(n)]
	ans = True
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if M[i][j] == '1':
				if i < n - 1 and M[i + 1][j] == '0' and (j < n - 1) and (M[i][j + 1] == '0'):
					ans = False
					break
		if not ans:
			break
	if ans:
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *

class Scanner:

	@staticmethod
	def int():
		return int(sys.stdin.readline().rstrip())

	@staticmethod
	def string():
		return sys.stdin.readline().rstrip()

	@staticmethod
	def map_int():
		return [int(x) for x in Scanner.string().split()]

	@staticmethod
	def string_list(n):
		return [Scanner.string() for i in range(n)]

	@staticmethod
	def int_list_list(n):
		return [Scanner.map_int() for i in range(n)]

	@staticmethod
	def int_cols_list(n):
		return [Scanner.int() for i in range(n)]

class Math:

	@staticmethod
	def gcd(a, b):
		if b == 0:
			return a
		return Math.gcd(b, a % b)

	@staticmethod
	def lcm(a, b):
		return a * b // Math.gcd(a, b)

	@staticmethod
	def divisor(n):
		res = []
		i = 1
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				res.append(i)
				if i != n // i:
					res.append(n // i)
		return res

	@staticmethod
	def round_up(a, b):
		return -(-a // b)

	@staticmethod
	def is_prime(n):
		if n < 2:
			return False
		if n == 2:
			return True
		if n % 2 == 0:
			return False
		d = int(n ** 0.5) + 1
		for i in range(3, d + 1, 2):
			if n % i == 0:
				return False
		return True

def pop_count(x):
	x = x - (x >> 1 & 6148914691236517205)
	x = (x & 3689348814741910323) + (x >> 2 & 3689348814741910323)
	x = x + (x >> 4) & 1085102592571150095
	x = x + (x >> 8)
	x = x + (x >> 16)
	x = x + (x >> 32)
	return x & 127
MOD = int(1000000000.0) + 7
INF = int(1000000000000000.0)

def solve():
	N = Scanner.int()
	S = Scanner.string_list(N)
	for i in range(N):
		for j in range(N):
			ok = False
			if S[i][j] == '0':
				continue
			if i + 1 == N:
				ok = True
			if j + 1 == N:
				ok = True
			if i + 1 < N and S[i + 1][j] == '1':
				ok = True
			if j + 1 < N and S[i][j + 1] == '1':
				ok = True
			if ok:
				continue
			return False
	return True

def main():
	T = Scanner.int()
	for _ in range(T):
		print('YNEOS'[not solve()::2])
main()


from sys import stdin, exit
input = stdin.readline

def i():
	return input()

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

def iis():
	return map(int, input().split())

def liis():
	return list(map(int, input().split()))

def print_array(a):
	print(' '.join(map(str, a)))

def is_valid(pol, i, j, valids):
	valid1 = True
	for k in range(j, len(pol)):
		if valids[i][k]:
			break
		if pol[i][k] != 1:
			valid1 = False
			break
	valid2 = True
	for k in range(i, len(pol)):
		if valids[k][j]:
			break
		if pol[k][j] != 1:
			valid2 = False
			break
	return valid1 or valid2
t = ii()
for _ in range(t):
	n = ii()
	pol = []
	for i in range(n):
		a = input()
		pol.append([])
		for j in str(a):
			try:
				pol[i].append(int(j))
			except:
				continue
	valids = [[False for i in range(n)] for i in range(n)]
	can = True
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if pol[i][j] == 1:
				if not is_valid(pol, i, j, valids):
					can = False
					break
				else:
					valids[i][j] = True
		if not can:
			break
	if can:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return list(map(int, input().split()))

def solve(matrix):
	r = len(matrix)
	c = len(matrix[0])
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == '1':
				if i + 1 == r or j + 1 == c:
					continue
				elif i + 1 < r and matrix[i + 1][j] == '1' or (j + 1 < c and matrix[i][j + 1] == '1'):
					continue
				else:
					return False
	return True
t = inp()
for i in range(t):
	n = inp()
	matrix = []
	for j in range(n):
		matrix.append(insr())
	print('YES' if solve(matrix) else 'NO')


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		line = list(str(input()))
		numbers = [int(n) for n in line]
		matrix.append(numbers)
	y = 0
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if matrix[i][j] == 1:
				if matrix[i + 1][j] == 0 and matrix[i][j + 1] == 0:
					y = 1
					break
		if y == 1:
			break
	if y == 0:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
input = stdin.readline
for t in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input().strip())
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				flag = 0
				if a[i + 1][j] == '1':
					flag = 1
				if flag == 0:
					if a[i][j + 1] == '1':
						flag = 1
				if flag == 0:
					ans = 'NO'
					break
		if ans == 'NO':
			break
	stdout.write(ans + '\n')


T = int(input())
for t in range(T):
	n = int(input())
	poly = []
	possible = True
	for i in range(n):
		poly.append(input())
	for i in range(n):
		for j in range(n):
			if poly[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				if poly[i + 1][j] == '1' or poly[i][j + 1] == '1':
					continue
				possible = False
				break
	if possible:
		print('YES')
	else:
		print('NO')


import sys
from collections import deque
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

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

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)) + '\n')

def list_input():
	return list(map(int, input().split()))

def sep_input():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	l = []
	visited = [[False for i in range(n)] for j in range(n)]
	for i in range(n):
		temp = [int(i) for i in input()]
		l.append(temp[:])
	q = deque()
	for i in range(n):
		if l[i][n - 1] == 1:
			q.append((i, n - 1))
		if l[n - 1][i] == 1:
			q.append((n - 1, i))
	while len(q) != 0:
		a = q.popleft()
		r = a[0]
		c = a[1]
		if visited[r][c]:
			continue
		else:
			visited[r][c] = True
			if r - 1 >= 0:
				if l[r - 1][c] == 1:
					q.append((r - 1, c))
			if c - 1 >= 0:
				if l[r][c - 1] == 1:
					q.append((r, c - 1))
	f = True
	for i in range(n):
		for j in range(n):
			if l[i][j] == 1 and visited[i][j] == False:
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


from math import sqrt as sq
for _ in range(int(input())):
	n = int(input())
	lis = []
	for i in range(n):
		s = input() + '1'
		lis.append(s)
	tmp = '1' * (n + 1)
	lis.append(tmp)
	c = 0
	for i in range(n):
		for j in range(n):
			if lis[i][j] == '1':
				if lis[i + 1][j] != '1' and lis[i][j + 1] != '1':
					print('NO')
					c = 1
					break
		if c:
			break
	if c == 0:
		print('YES')


import sys
import math
from sys import stdin, stdout

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def main():
	for t in range(int(input())):
		n = int(input())
		l = []
		for i in range(n):
			h = input()
			l.append(h)
		c = 0
		for i in range(n - 1):
			for j in range(n - 1):
				if l[i][j] == '1':
					if l[i + 1][j] == '1' or l[i][j + 1] == '1':
						pass
					else:
						c = 1
						break
		if c == 1:
			print('NO')
		else:
			print('YES')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import deque
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	s = []
	for _ in range(n):
		s.append(input())
	seen = [[False] * n for _ in range(n)]
	for i in range(n):
		for j in range(n):
			if s[i][j] == '0':
				seen[i][j] = True
				continue
			if seen[i][j]:
				continue
			if i == n - 1 or j == n - 1:
				seen[i][j] = True
				continue
			q = deque([(i, j)])
			while q:
				(y, x) = q.popleft()
				flg = False
				for (dy, dx) in dxdy:
					if s[y + dy][x + dx] == '0':
						continue
					flg = True
					if seen[y + dy][x + dx]:
						continue
					seen[y + dy][x + dx] = True
					if y + dy == n - 1 or x + dx == n - 1:
						continue
					q.append((y + dy, x + dx))
				if not flg:
					return 'NO'
	return 'YES'
t = int(input())
dxdy = [(0, 1), (1, 0)]
for _ in range(t):
	print(solve())


class Main:

	def main(self):
		t = int(input())
		for t1 in range(0, t):
			n = int(input())
			p = list(list())
			for i in range(0, n):
				s = input()
				p.append(list(map(lambda j: s[j], range(0, len(s)))))
			result = True
			for i in range(0, n):
				for j in range(0, n):
					if p[i][j] == '1' and i + 1 < n and (p[i + 1][j] == '0') and (j + 1 < n) and (p[i][j + 1] == '0'):
						result = False
			print('YES' if result else 'NO')
Main().main()


t = int(input())
s1 = ''
a = []
b = []
flag = 0
while t:
	n = int(input())
	for i in range(n):
		s1 = input()
		b.append(s1)
		a.append(b)
		b = []
	for i in range(n):
		for j in range(n):
			if a[i][0][j] == '1':
				if (i != n - 1 and a[i + 1][0][j] == '0') and (j != n - 1 and a[i][0][j + 1] == '0'):
					flag = 1
	if flag == 1:
		print('NO')
	else:
		print('YES')
	flag = 0
	a = []
	b = []
	s = ''
	t -= 1


def polygon(L):
	n = len(L)
	if n == 1:
		return 'YES'
	else:
		for i in range(0, n - 1):
			for j in range(0, n - 1):
				if L[i][j] == '1' and max(L[i + 1][j], L[i][j + 1]) != '1':
					return 'NO'
		return 'YES'
t = int(input())
for i in range(0, t):
	L = []
	n = int(input())
	for i in range(0, n):
		M = list(str(input()))
		L.append(M)
	print(polygon(L))


T = int(input())
for t in range(T):
	N = int(input())
	M = []
	for i in range(N):
		row = input()
		M.append(row)
	for i in range(len(M) - 1):
		for j in range(len(M[i]) - 1):
			if M[i][j] == '1' and M[i + 1][j] == '0' and (M[i][j + 1] == '0'):
				print('NO')
				break
		else:
			continue
		break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	ans = 'YES'
	for i in range(n - 1):
		for j in range(len(l[i]) - 1):
			if l[i][j] == '1' and l[i + 1][j] == '0' and (l[i][j + 1] == '0'):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)


def is_valid_matrix(matrix):
	n = len(matrix)
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == '1' and (not (i == n - 1 or matrix[i + 1][j] == '1' or j == n - 1 or (matrix[i][j + 1] == '1'))):
				return False
	return True

def solve():
	n = int(input())
	matrix = []
	for _ in range(n):
		matrix.append(input())
	if is_valid_matrix(matrix):
		print('YES')
	else:
		print('NO')
t = int(input())
for _ in range(t):
	solve()


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
	n = read_int()
	mat = []
	for i in range(n):
		mat.append(input())
	ok = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '0':
				continue
			cok = j == n - 1 or i == n - 1
			if not cok:
				cok = mat[i][j + 1] == '1' or mat[i + 1][j] == '1'
			if not cok:
				ok = False
				break
		if not ok:
			break
	print('YES' if ok else 'NO')


t = int(input())
out = []
for _ in range(t):
	n = int(input())
	m = []
	for x in range(n):
		m.append(list(input()))
	exist = True
	n2 = len(m[0])
	for i in range(n):
		for j in range(n2):
			if m[i][j] == '1':
				if j + 1 < n2 and m[i][j + 1] == '0':
					if i + 1 < n and m[i + 1][j] == '0':
						exist = False
						break
		if not exist:
			break
	out.append(exist)
for x in out:
	if x:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = ''
	for _ in range(n):
		s += input()
	ans = 'YES'
	for i in range(n * n - n):
		if s[i] == '1':
			if (i + 1) % n != 0:
				if s[i + n] == '0' and s[i + 1] == '0':
					ans = 'NO'
					break
	print(ans)


from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect
import math
import heapq
import sys
import io, os

def check(i, j, n, arr):
	if j == n - 1 or i == n - 1:
		return True
	if arr[i][j + 1] == 0 and arr[i + 1][j] == 0:
		return False
	return True
for ii in range(int(input())):
	n = int(input())
	arr = [list(map(int, list(input()))) for i in range(n)]
	f = 1
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1:
				if not check(i, j, n, arr):
					print('NO')
					f = 0
					break
		if not f:
			break
	if f:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for j in range(n):
		matrix.append(list(input()))
	stop = 0
	for row in range(n):
		if not stop:
			for column in range(n):
				if matrix[row][column] == '1':
					if row == n - 1 or column == n - 1:
						continue
					else:
						if matrix[row + 1][column] == '1':
							continue
						if matrix[row][column + 1] == '1':
							continue
					print('NO')
					stop = 1
					break
	if not stop:
		print('YES')


from sys import stdin, stdout
import math
n = int(stdin.readline())
for i in range(n):
	t = int(stdin.readline())
	matrix = []
	for i in range(t):
		arr = [x for x in stdin.readline()]
		matrix.append(arr)
	yes = 1
	for i in range(t):
		for j in range(t - 1):
			if matrix[i][j] == '1':
				if j == t - 1 or i == t - 1:
					continue
				elif matrix[i + 1][j] == '1' or matrix[i][j + 1] == '1':
					continue
				else:
					yes = 0
	if yes:
		stdout.write('YES' + '\n')
	else:
		stdout.write('NO' + '\n')


def r(L, n):
	for i in range(n - 1):
		for t in range(n - 1):
			if int(L[i][n - t - 1]) == 0:
				if int(L[i][n - t - 2]) == 1:
					if int(L[i + 1][n - t - 2]) == 0:
						return 'NO'
	return 'YES'
t = int(input())
while t > 0:
	L = []
	n = int(input())
	for s in range(n):
		a = input()
		w = list(a)
		L.append(w)
	print(r(L, n))
	t -= 1


import math
t = int(input())
for i in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		arr = list(map(int, input().strip()))
		mat.append(arr)
		arr = []
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			t = mat[i][j]
			if t == 1:
				if mat[i + 1][j] == 1 or mat[i][j + 1] == 1:
					continue
				else:
					print('NO')
					flag = 1
					break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


def pol(l1):
	for i in range(len(l1) - 2, -1, -1):
		for j in range(len(l1) - 2, -1, -1):
			if l1[i][j] == '1':
				if l1[i + 1][j] == '1' or l1[i][j + 1] == '1':
					continue
				else:
					return 'NO'
	return 'YES'
t = int(input())
l = []
for i in range(t):
	l1 = []
	n = int(input())
	for j in range(n):
		s = input()
		l1.append(s)
	l.append(l1)
for l1 in l:
	print(pol(l1))


def isConnected(array, length):
	for i in range(length - 1):
		for j in range(length - 1):
			if array[i][j] == '1':
				if not (array[i][j + 1] == '1' or array[i + 1][j] == '1'):
					return False
	return True
for _ in [0] * int(input()):
	n = int(input())
	arr = []
	for _ in range(n):
		arr += [input()]
	if isConnected(arr, n):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	is_pos = True
	if n == 1:
		print('YES')
	else:
		for i in range(n - 1):
			for j in range(n - 1):
				if arr[i][j] == '1':
					if arr[i + 1][j] != '1' and arr[i][j + 1] != '1':
						is_pos = False
						break
		if is_pos:
			print('YES')
		else:
			print('NO')


t = int(input())
while t:
	n = int(input())
	mat = []
	for i in range(n):
		temp = list(input())
		mat.append([int(j) for j in temp])
	flg = 0
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i != n - 1 and j != n - 1:
					if mat[i + 1][j] != 1 and mat[i][j + 1] != 1:
						flg = 1
	if flg == 0:
		print('YES')
	else:
		print('NO')
	t -= 1


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = []
	for i in range(n):
		a.append([int(i) for i in input()])
	f = 1
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if a[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				elif a[i + 1][j] == 0 and a[i][j + 1] == 0:
					f = 0
					break
		if f == 0:
			break
	if f:
		print('YES')
	else:
		print('NO')


import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def printspx(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')

def printchk(*args):
	return print(*args, end=' \\ ')
MODPRIME = int(1000000000.0 + 7)
BABYMOD = 998244353
for _testcases_ in range(int(input())):
	n = int(input())
	matr = []
	for i in range(n):
		li = input()
		matr.append(li)
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matr[i][j] == '1':
				if matr[i][j + 1] == '0' and matr[i + 1][j] == '0':
					flag = False
					break
	if flag:
		print('YES')
	else:
		print('NO')


def test(a, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] != '1' and a[i][j + 1] != '1':
					print('NO')
					return
	print('YES')
	return
for i in range(int(input())):
	n = int(input())
	a = [input() for i in range(n)]
	test(a, n)


t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input()))
	f = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] != '1' and l[i][j + 1] != '1':
					f = 0
					break
	if f == 1:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_right(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = []
		for x in range(n):
			l.append(list(input()))
		flag = 0
		for i in range(n - 1):
			for j in range(n - 1):
				if l[i][j] == '1':
					if l[i + 1][j] != '1' and l[i][j + 1] != '1':
						flag = 1
						break
		if flag:
			no()
		else:
			yes()
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for _ in range(int(input())):
	n = int(input())
	arr = []
	for __ in range(n):
		arr.append(input())
	flag = 0
	for i in range(n - 1):
		if flag == 1:
			break
		for j in range(n - 1):
			if arr[i][j] == '1' and arr[i + 1][j] == '0' and (arr[i][j + 1] == '0'):
				print('NO')
				flag = 1
				break
	if flag == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	grid = list()
	for i in range(n):
		grid.append(list(input()))
	valid = True
	for i in range(n - 1):
		if not valid:
			break
		for j in range(n - 1):
			if grid[i][j] == '1' and grid[i + 1][j] == '0' and (grid[i][j + 1] == '0'):
				valid = False
				break
	if valid:
		print('YES')
	else:
		print('NO')


import sys

def answer(n, m):
	for row in range(n):
		for col in range(n):
			if m[row][col] == '1':
				if col == n - 1:
					pass
				elif row == n - 1:
					pass
				elif m[row][col + 1] == '1':
					pass
				elif m[row + 1][col] == '1':
					pass
				else:
					return 'NO'
	return 'YES'

def main():
	t = int(input())
	while t:
		n = int(sys.stdin.readline())
		m = [0] * n
		for row in range(n):
			m[row] = sys.stdin.readline().rstrip()
		print(answer(n, m))
		t -= 1
	return
main()


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
ans = ['NO', 'YES']
for _ in range(II()):
	n = II()
	a = [SI() for i in range(n)]
	ok = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] != '1' and (a[i][j + 1] != '1'):
				ok = 0
	print(ans[ok])


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = []
	for i in range(n):
		m = input()
		a.append(m)
	k = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					k = 1
				else:
					k = 2
					break
		if k == 2:
			break
	if k == 1:
		print('YES')
	elif k == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	p = [list(map(int, list(input()))) for _ in range(n)]
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			if p[i][j] == 1 and p[i + 1][j] == 0 and (p[i][j + 1] == 0):
				print('NO')
				flag = True
				break
		if flag:
			break
	if not flag:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	mat = []
	for j in range(n):
		mat.append(str(input()))
	bp = False
	for j in range(n - 1):
		for k in range(n - 1):
			if mat[j][k] == '1':
				if mat[j + 1][k] == '0' and mat[j][k + 1] == '0':
					bp = True
					break
		if bp == True:
			print('NO')
			break
	if bp == False:
		print('YES')


for i in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	check = True
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if i < n and j < n:
				if mat[i][j] == '1':
					if mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
						check = True
					else:
						check = False
						flag = 1
						break
				else:
					pass
		if flag == 1 and check == False:
			break
	if flag != 1 and check == True:
		print('YES')
	else:
		print('NO')


t = int(input())
rez = []
for i in range(t):
	n = int(input())
	a = []
	nn = 0
	for j in range(n):
		a.append(input())
	for j in range(n - 1):
		for jj in range(n - 1):
			if a[j][jj] == '1' and a[j][jj + 1] != '1' and (a[j + 1][jj] != '1'):
				nn = 1
				break
		if nn == 1:
			break
	if nn == 1:
		rez.append('NO')
	else:
		rez.append('YES')
for i in range(t):
	print(rez[i])


from sys import stdin, stdout

def dfs(arr, i, j, N):
	if not (0 <= i < N and 0 <= j < N):
		return
	if arr[i][j] != 1:
		return
	arr[i][j] = -1
	dfs(arr, i, j - 1, N)
	dfs(arr, i - 1, j, N)

def find(arr, N):
	for i in range(N)[::-1]:
		dfs(arr, i, N - 1, N)
	for i in range(N)[::-1]:
		dfs(arr, N - 1, i, N)
	for i in range(N):
		for j in range(N):
			if arr[i][j] == 1:
				return 'NO'
	return 'YES'

def main():
	for _ in range(int(stdin.readline())):
		N = int(stdin.readline())
		arr = []
		for __ in range(N):
			arr.append(list(map(int, stdin.readline().strip())))
		print(find(arr, N))
main()


import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
	n = int(input())
	grid = []
	for i in range(n):
		grid.append(input())
	flag = 0
	for i in range(n):
		for j in range(n):
			if grid[i][j] == '1':
				if i == n - 1:
					continue
				elif j == n - 1:
					continue
				elif grid[i][j + 1] == '1':
					continue
				elif grid[i + 1][j] == '1':
					continue
				else:
					flag = 1
					print('NO')
					break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(0, n):
		a += [input()]
	b = 0
	for i in range(0, n):
		for j in range(0, n):
			if a[i][j] == '1' and i < n - 1 and (j < n - 1) and (a[i][j + 1] == '0') and (a[i + 1][j] == '0'):
				b = 1
				break
		if b == 1:
			break
	if b == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i1 in range(t):
	n = int(input())
	matr = []
	for i in range(n):
		a = [int(x) for x in input()]
		matr.append(a)
	bool = []
	for i in range(len(matr[0])):
		bool.append([False] * len(matr[0]))
	for k in range(len(matr[0])):
		if matr[k][n - 1] == 1:
			bool[k][n - 1] = True
		if matr[n - 1][k] == 1:
			bool[n - 1][k] = True
	for i in range(n - 1, -1, -1):
		for t in range(i, -1, -1):
			if matr[i][t] == 1 and (t + 1 < n and bool[i][t + 1] == True or (i + 1 < n and bool[i + 1][t] == True)):
				bool[i][t] = True
		for t in range(i, -1, -1):
			if matr[t][i] == 1 and (t + 1 < n and bool[t + 1][i] == True or (i + 1 < n and bool[t][i + 1] == True)):
				bool[t][i] = True
	flag = True
	for i in range(n):
		for j in range(n):
			if matr[i][j] == 1 and (not bool[i][j]):
				flag = False
	if not flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [[1] * (n + 1)]
	for i in range(n):
		t = input()
		tmp = [int(x) for x in t]
		a.append([1] + tmp + [1])
	a.append([1] * (n + 2))
	c = 1
	for i in range(n, 0, -1):
		for j in range(n, 0, -1):
			if a[i][j] == 1:
				if a[i + 1][j] != 1 and a[i][j + 1] != 1:
					c = 0
					break
		if c == 0:
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	m = []
	for i in range(n):
		m.append(list(input()))
	flg = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1':
				if m[i + 1][j] == '0' and m[i][j + 1] == '0':
					flg = 1
					break
		if flg:
			break
	if flg:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = [list(input()) for i in range(n)]
	boo = True
	for i in range(n):
		for j in range(n):
			if i == n - 1 or j == n - 1:
				continue
			elif a[i][j] == '1' and (a[i + 1][j] == '0' and a[i][j + 1] == '0'):
				boo = False
				break
		if not boo:
			break
	ans = 'YES' if boo else 'NO'
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	a = []
	for i in range(n):
		b = []
		b = [int(x) for x in list(input())]
		a.append(b)
	count = 0
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1 and i != n - 1 and (j != n - 1):
				if a[i + 1][j] == 0 and a[i][j + 1] == 0:
					count = count + 1
					print('NO')
					break
		if count == 1:
			break
	if count == 0:
		print('YES')
	t = t - 1


from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left
from itertools import product
T = int(input())
for _ in range(T):
	n = int(input())
	ls = [input() for _ in range(n)]
	ok = 1
	for (i, j) in product(range(n), range(n)):
		if ls[i][j] == '0' or i == n - 1 or j == n - 1 or (ls[i + 1][j] == '1') or (ls[i][j + 1] == '1'):
			continue
		ok = 0
		break
	print('YES' if ok else 'NO')


import sys
import math
import collections

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin

def dfs(grid, i, j):
	if not 0 <= i < n or not 0 <= j < n or grid[i][j] == '0' or ((i, j) in visit):
		return
	visit.add((i, j))
	dfs(grid, i - 1, j)
	dfs(grid, i, j - 1)
t = int(input())
for ti in range(1, t + 1):
	n = int(input())
	grid = []
	for i in range(n):
		grid.append(input())
	count = 0
	start = []
	for i in range(n):
		for j in range(n):
			if grid[i][j] == '1':
				count += 1
				if i == n - 1 or j == n - 1:
					start.append((i, j))
	visit = set()
	for (x, y) in start:
		dfs(grid, x, y)
	if len(visit) == count:
		print('YES')
	else:
		print('NO')


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
for _ in range(iinput()):
	n = iinput()
	mat = []
	for i in range(n):
		mat.append(input())
	flg = False
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i + 1][j] == '0' and mat[i][j + 1] == '0':
					flg = True
					break
		if flg:
			break
	print(['NO', 'YES'][not flg])


import collections
T = int(input())

def topological_sort(graph):
	result = []
	rev_deps = {n: 0 for n in graph.keys()}
	for (k, v) in graph.items():
		for i in v:
			rev_deps[i] += 1
	queue = collections.deque()
	for (k, v) in rev_deps.items():
		if v == 0:
			queue.append(k)
	while queue:
		item = queue.popleft()
		result.append(item)
		for n in graph[item]:
			rev_deps[n] -= 1
			if rev_deps[n] == 0:
				queue.append(n)
	for (k, v) in rev_deps.items():
		if v > 0:
			return []
	return result
for t_itr in range(T):
	N = int(input())
	A = []
	for _ in range(N):
		A.append(list(map(int, input())))
	graph = {x: set() for x in range(N * N)}
	result = True
	for i in range(N - 1):
		for j in range(N - 1):
			connected = False
			if A[i][j] == 1:
				connected = False
				if A[i][j + 1] == 1:
					graph[i + N * j].add(i + N * (j + 1))
					connected = True
				if A[i + 1][j] == 1:
					graph[i + N * j].add(i + 1 + N * j)
					connected = True
				if not connected:
					result = False
					break
		if not result:
			break
	for j in range(N - 1):
		if A[-1][j] == 1 and A[-1][j + 1] == 1:
			graph[N - 1 + N * j].add(N - 1 + N * (j + 1))
	for i in range(N - 1):
		if A[i][-1] == 1 and A[i + 1][-1] == 1:
			graph[i + N * N - N].add(i + 1 + N * N - N)
	if not result:
		print('NO')
	elif topological_sort(graph):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = []
	for i in range(n):
		s.append(input())
	f = 1
	for i in range(n - 1, -1, -1):
		if f:
			for j in range(n - 1, -1, -1):
				if s[i][j] == '1':
					if i != n - 1 and j != n - 1:
						if s[i + 1][j] == '0' and s[i][j + 1] == '0':
							f = 0
							print('NO')
							break
	if f == 1:
		print('YES')


from sys import stdin
from collections import deque

def r():
	return stdin.readline().strip()

def r_t(tp):
	return map(tp, r().strip().split())

def r_a(tp):
	return list(r_t(tp))

def solve(n, M):
	for i in range(n):
		for j in range(n):
			if M[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif i < n and M[i + 1][j] == '1' or (j < n and M[i][j + 1] == '1'):
					continue
				else:
					return False
	return True

def main():
	cases = int(r())
	for case in range(cases):
		n = int(r())
		M = list()
		for i in range(n):
			M.append(r())
		print('YES' if solve(n, M) else 'NO')
main()


T = int(input())
for test in range(T):
	n = int(input())
	l = []
	for i in range(n):
		t = input()
		l.append(t)
	c = True
	for i in range(n - 1):
		for j in range(n - 1):
			if (l[i][j] == '1' and l[i][j + 1] == '0') and (l[i][j] == '1' and l[i + 1][j] == '0'):
				c = False
				break
	if c:
		print('YES')
	else:
		print('NO')


def check(i, j, board, n):
	if j != n - 1:
		if board[i][j + 1] == '1':
			return True
	else:
		return True
	if i != n - 1:
		if board[i + 1][j] == '1':
			return True
	else:
		return True
	return False
for t in range(int(input())):
	n = int(input())
	board = []
	for i in range(n):
		board.append(input())
	isTrue = True
	for i in range(n):
		for j in range(n):
			if board[i][j] == '1':
				isTrue = check(i, j, board, n)
				if isTrue == False:
					print('NO')
					break
		if isTrue == False:
			break
	if isTrue:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	(a, flag) = ([], 0)
	for i in range(n):
		s = input()
		a += [s]
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif a[i][j + 1] == '1' or a[i + 1][j] == '1':
					continue
				else:
					flag = 1
					break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


import sys

def result():
	global list_map, N
	for y in range(N - 1):
		for x in range(N - 1):
			if list_map[y][x] == '1' and list_map[y][x + 1] == '0' and (list_map[y + 1][x] == '0'):
				return 'NO'
	return 'YES'
T = int(sys.stdin.readline())
for _ in range(T):
	N = int(sys.stdin.readline())
	list_map = [list(sys.stdin.readline().strip()) for _ in range(N)]
	print(result())


import os, sys
from math import *
t = int(input())
for nc in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input().strip())
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			flag = flag or (a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'))
	print('NO' if flag else 'YES')


YES = 'YES'
NO = 'NO'

def reader():
	tests = []
	tests_count = int(input())
	for i in range(0, tests_count):
		tests.append([])
		rows_count = int(input())
		for __ in range(0, rows_count):
			tests[i].append([int(i) for i in input()])
	return tests

def checker(test):
	rows_count = len(test)
	for i in range(0, rows_count):
		for j in range(0, rows_count):
			if test[i][j] == 1 and (i < rows_count - 1 and test[i + 1][j] == 0) and (j < rows_count - 1 and test[i][j + 1] == 0):
				return False
	return True
for test in reader():
	print(YES) if checker(test) else print(NO)


def didTrainingHappen(sideLengthOfReport, report):
	for iRow in range(sideLengthOfReport):
		if not possibleRow(sideLengthOfReport, report, iRow):
			return 'NO'
	return 'YES'

def possibleRow(sideLengthOfReport, report, iRow):
	iMax = sideLengthOfReport - 1
	for iColumn in range(sideLengthOfReport):
		if report[iRow][iColumn] == '1' and iColumn != iMax and (iRow != iMax) and (report[iRow][iColumn + 1] == '0') and (report[iRow + 1][iColumn] == '0'):
			return False
	return True

def testCase_1360e():
	sideLengthOfReport = int(input())
	return (sideLengthOfReport, tuple((input() for row in range(sideLengthOfReport))))
nTestCases = int(input())
testCases = tuple((testCase_1360e() for testCase in range(nTestCases)))
tuple((print(didTrainingHappen(*testCase)) for testCase in testCases))


for _ in range(int(input())):
	n = int(input())
	a = [input() for i in range(n)]
	was = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				print('NO')
				was = 1
				break
		if was:
			break
	if not was:
		print('YES')


from sys import stdin
import math
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	ar = []
	flag = False
	for i in range(n):
		ar.append(inp())
	for i in range(n):
		if flag:
			break
		for j in range(n):
			if ar[i][j] == '1':
				if i == n - 1 or j == n - 1 or ar[i + 1][j] == '1' or (ar[i][j + 1] == '1'):
					continue
				else:
					print('NO')
					flag = True
					break
	if flag:
		continue
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for _ in range(n):
		mat.append(list(input()))
	dp = []
	for i in range(n):
		inner = []
		for j in range(n):
			if mat[i][j] == '1':
				inner.append(False)
			else:
				inner.append(True)
		dp.append(inner)
	for i in range(n):
		if mat[i][n - 1] == '1':
			dp[i][n - 1] = True
	for i in range(n):
		if mat[n - 1][i] == '1':
			dp[n - 1][i] = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if mat[i][j] == '1':
				if mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
					dp[i][j] = True
	flag = True
	for i in range(n):
		for j in range(n):
			if not dp[i][j]:
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	grid = []
	for i in range(n):
		s = str(input())
		l = list(s)
		grid.append(l)
	c = 0
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if grid[i][j] == str(1):
				if grid[i + 1][j] == str(1) or grid[i][j + 1] == str(1):
					pass
				else:
					c = 1
					break
	if c == 1:
		print('NO')
	else:
		print('YES')


import sys
t = int(input())
for tc in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		s = list(map(int, list(input())))
		mat.append(s)
	flag = 0
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i + 1 < n and j + 1 < n:
					if mat[i + 1][j] == 0 and mat[i][j + 1] == 0:
						flag = 1
		if flag == 1:
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


from math import sqrt
from collections import defaultdict as dd, deque
import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	ll = []
	lol = 0
	for i in range(n):
		s = input().split()[0]
		ll.append(s)
	for i in range(n):
		for j in range(n):
			if ll[i][j] == '1':
				if (i + 1 < n and ll[i + 1][j] == '0') and (j + 1 < n and ll[i][j + 1] == '0'):
					lol = 1
					break
		if lol:
			break
	if lol:
		print('NO')
	else:
		print('YES')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = [list(map(int, input())) for _ in range(n)]
	s = True
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == 1 and l[i][j + 1] != 1 and (l[i + 1][j] != 1):
				s = False
				break
		if not s:
			break
	if s:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for _ in range(n):
		s = input()
		temp = list(s)
		l.append(temp)
	flag = 0
	for i in range(n - 1):
		for j in range(len(l[i]) - 1):
			if (l[i][j] == '1' and l[i + 1][j] != '1') and (l[i][j] == '1' and l[i][j + 1] != '1'):
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for m in range(n):
		s1 = str(input())
		s = list(map(int, s1))
		arr.append(s)
	f_error = False
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1:
				if i != n - 1 and j != n - 1:
					if arr[i + 1][j] != 1 and arr[i][j + 1] != 1:
						f_error = True
	if f_error:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	can = []
	f = 0
	for i in range(n):
		can.append(list(input()))
	for i in range(n - 1):
		for j in range(n - 1):
			if can[i][j] == '1' and (can[i + 1][j] != '1' and can[i][j + 1] != '1'):
				f = 1
				break
		if f:
			break
	if f:
		print('NO')
	else:
		print('YES')


def solve(n, a):
	hay = False
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if a[i][j] == 1 and a[i + 1][j] == 0 and (a[i][j + 1] == 0):
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append([int(j) for j in input()])
	print(solve(n, matrix))


import sys
t = int(sys.stdin.readline())
ans_arr = []
for i in range(t):
	n = int(sys.stdin.readline())
	arr = []
	for x in range(n):
		tmp = sys.stdin.readline().strip()
		b = []
		for h in range(n):
			b.append(int(tmp[h]))
		arr.append(b)
	ans = 'YES'
	for a in range(n - 1):
		for b in range(n - 1):
			if arr[a][b] == 1 and arr[a][b + 1] == 0 and (arr[a + 1][b] == 0):
				ans = 'NO'
	ans_arr.append(ans)
print('\n'.join(ans_arr))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools
from collections import deque, defaultdict, OrderedDict
import collections

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = [[0] * n for i in range(n)]
		mat = [[j for j in input()] for i in range(n)]
		ans = 'YES'
		for i in range(n):
			for j in range(n):
				if mat[i][j] == '1':
					if i + 1 < n and j + 1 < n and (mat[i + 1][j] == '0') and (mat[i][j + 1] == '0'):
						ans = 'NO'
						break
			if ans == 'NO':
				break
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def solve(m, f):
	for i in range(f):
		for j in range(f):
			if m[i][j] == '1':
				if i == f - 1 or j == f - 1:
					continue
				elif m[i][j + 1] == '1' or m[i + 1][j] == '1':
					continue
				else:
					return False
	return True
g = int(input())
while g:
	g -= 1
	f = int(input())
	m = []
	for i in range(f):
		r = input()
		m.append(list(r))
	if solve(m, f):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	board = []
	for _ in range(n):
		board.append(list(input()))
	ans = True
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if board[i][j] == '1' and (not (board[i + 1][j] == '1' or board[i][j + 1] == '1')):
				ans = False
	if ans:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	bok = int(input())
	pole = [[] for _ in range(bok + 1)]
	pole[bok] = ['1' for _ in range(bok + 1)]
	przerw = False
	for x in range(bok):
		pole[x] = list(input()) + [1]
	for y in range(bok):
		for x in range(bok):
			if pole[y][x] == '1':
				if pole[y][x + 1] == '0' and pole[y + 1][x] == '0':
					print('NO')
					przerw = True
					break
		if przerw:
			break
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		x = input()
		y = []
		for j in range(len(x)):
			y.append(int(x[j]))
		arr.append(y)
	flag = 0
	for i in range(len(arr) - 1):
		for j in range(len(arr) - 1):
			if arr[i][j] == 1 and arr[i + 1][j] == 0 and (arr[i][j + 1] == 0):
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())

def solve(n, grid):
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if grid[i][j] == '1' and (grid[i + 1][j] != '1' and grid[i][j + 1] != '1'):
				return False
	return True
for _ in range(t):
	n = int(input())
	grid = []
	for _ in range(n):
		grid.append(list(input()))
	a = solve(n, grid)
	if a == True:
		print('YES')
	else:
		print('NO')


def solution(a):
	for (idx_row, row) in enumerate(a):
		for (idx_item, item) in enumerate(row):
			if item == '1':
				if idx_item == len(row) - 1 or row[idx_item + 1] == '1' or idx_row == len(a) - 1 or (a[idx_row + 1][idx_item] == '1'):
					continue
				else:
					print('NO')
					return
	print('YES')
inp = int(input())
for _ in range(inp):
	cnt = int(input())
	res = []
	for i in range(cnt):
		res.append(input())
	solution(res)


import math
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = [list(input()) for i in range(n)]
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] == '0' and a[i + 1][j] == '0':
					ans = False
					break
		if ans == False:
			break
	if ans:
		print('YES')
	else:
		print('NO')


import io, os, math

def ri():
	return int(input().strip())

def rl():
	return list(map(int, input().strip().split()))

def Solve():
	n = ri()
	mp = []
	for i in range(n):
		mp.append(input().strip())
	for j in range(n - 1, -1, -1):
		tag = 0
		for i in range(n - 1, -1, -1):
			if mp[i][j] == '0':
				tag = 1
			if mp[i][j] == '1':
				if tag and j + 1 < n and (mp[i][j + 1] == '0'):
					print('NO')
					return
				tag = 0
	print('YES')
T = ri()
while T:
	T -= 1
	Solve()


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque

def solution(a, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] == '0' and a[i + 1][j] == '0':
					return 'NO'
	return 'YES'

def main():
	T = read_int()
	for i in range(T):
		n = read_int()
		a = []
		for j in range(n):
			a.append(read_string())
		x = solution(a, n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


def solve():
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] != 1 and arr[i][j + 1] != 1:
					return 'NO'
	return 'YES'
testcases = int(input())
for i in range(testcases):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(map(int, input())))
	print(solve())


from collections import defaultdict, Counter
from functools import lru_cache
from heapq import heappush, heappop
from math import gcd, floor, ceil
from sys import stdin, stdout

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

def istr():
	return stdin.readline().strip()

def solve(a):
	n = len(a)
	for i in range(n):
		for j in range(n - 1, -1, -1):
			if a[i][j] == '0':
				break
			a[i][j] = '#'
	for j in range(n):
		for i in range(n - 1, -1, -1):
			if a[i][j] == '0':
				break
			a[i][j] = '#'
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if a[i][j] == '1':
				if j + 1 < n and a[i][j + 1] == '#':
					a[i][j] = '#'
				if i + 1 < n and a[i + 1][j] == '#':
					a[i][j] = '#'
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				return 'NO'
	return 'YES'
t = iint()
for _ in range(t):
	n = iint()
	a = []
	for _ in range(n):
		a.append(list(istr()))
	print(solve(a))


def panduan(list1):
	for i in range(n - 1):
		for y in range(n - 1):
			if int(list1[i][y]):
				if list1[i][y + 1] != '1' and list1[i + 1][y] != '1':
					return 'NO'
	return 'YES'
list_ans = []
for _ in range(int(input())):
	n = int(input())
	list1 = []
	for i in range(n):
		list1.append(input())
	list_ans.append(panduan(list1))
for item in list_ans:
	print(item)


def solve():
	n = int(input())
	a = [list(map(int, list(input()))) for i in range(n)]
	exists = [[0] * n for i in range(n)]
	wrong = False
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if i == n - 1 or j == n - 1:
				if a[i][j] == 1:
					exists[i][j] = True
			elif a[i][j] == 1:
				if exists[i][j + 1] == True or exists[i + 1][j] == True:
					exists[i][j] = True
				else:
					wrong = True
					break
	if wrong:
		print('NO')
	else:
		print('YES')
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


from functools import reduce
import collections
import math
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(delimiter=' '):
		return input().split(delimiter)

	@staticmethod
	def list_int(delimiter=' '):
		return list(map(int, input().split(delimiter)))

def main():
	t = Read.int()
	for _ in range(t):
		n = Read.int()
		a = list()
		for i in range(n):
			a.append(list(Read.string()))
		flag = True
		for i in range(n):
			if not flag:
				break
			for j in range(n):
				if a[i][j] == '1':
					if j != n - 1 and i != n - 1:
						if a[i][j + 1] == '0' and a[i + 1][j] == '0':
							flag = False
							break
		print('yes') if flag else print('no')
main()


import math
for _ in range(int(input())):
	n = int(input())
	x = []
	flag = 0
	for i in range(n):
		x.append([int(w) for w in input()])
	for i in range(n - 1):
		for j in range(n - 1):
			if x[i][j]:
				if x[i + 1][j] + x[i][j + 1] == 0:
					flag = 1
					break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s = []
	for i in range(n):
		s.append(list(input()))
	ans = True
	for i in range(n):
		for j in range(n):
			if s[i][j] == '1' and i != n - 1 and (j != n - 1):
				if s[i + 1][j] == s[i][j + 1] == '0':
					ans = False
					break
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		s = input()
		tmp = []
		for x in s:
			tmp.append(int(x))
		a.append(tmp)
	bool_value = True
	for x in range(n):
		for y in range(n):
			if a[x][y] == 1:
				if x != n - 1 and y != n - 1:
					if a[x + 1][y] != 1 and a[x][y + 1] != 1:
						bool_value = False
						break
		if not bool_value:
			break
	if bool_value:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	n = int(input())
	n1 = n
	a = []
	while n1:
		b = input().rstrip()
		a.append(b)
		n1 -= 1
	T = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if n == i + 1 or n == j + 1:
					continue
				elif a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				else:
					T = False
					break
		if T:
			continue
		else:
			break
	if T:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]
t = int(input())
for q in range(t):
	n = inputnum()
	matrix = [0] * n
	for i in range(n):
		matrix[i] = inputstring()
	good = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1' and matrix[i + 1][j] == '0' and (matrix[i][j + 1] == '0'):
				good = False
				break
		if not good:
			break
	if good:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	k = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if j != n - 1 and l[i][j + 1] == '0' and (i != n - 1) and (l[i + 1][j] == '0'):
					k = 1
	if k == 0:
		print('YES')
	else:
		print('NO')
	t -= 1


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		mat = []
		for i in range(n):
			mat.append([char for char in input()])
		done = False
		for i in range(n - 1):
			if done:
				break
			for j in range(n - 1):
				if mat[i][j] == '1':
					if mat[i + 1][j] != '1' and mat[i][j + 1] != '1':
						print('NO')
						done = True
						break
		if not done:
			print('YES')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list()
	for row in range(n):
		temp = list(map(int, list(input())))
		arr.append(temp)
	no = 0
	for r in range(n):
		for c in range(n):
			if arr[r][c] == 1:
				if r < n - 1 and c < n - 1:
					if arr[r + 1][c] == 1 or arr[r][c + 1] == 1:
						continue
					else:
						no = 1
						break
				elif r == n - 1 and c < n - 1:
					if arr[r][c + 1] == 1:
						continue
					else:
						break
				elif c == n - 1 and r < n - 1:
					if arr[r + 1][c] == 1:
						continue
					else:
						break
				elif r == n - 1 and c == n - 1:
					continue
	if no == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = [[*map(int, input())] for i in range(n)]
	flag = True
	for i in range(n):
		for j in range(n):
			if l[i][j] == 1:
				if i + 1 == n or j + 1 == n or l[i + 1][j] == 1 or (l[i][j + 1] == 1):
					continue
				else:
					flag = False
					break
	print('YES' if flag else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for _ in range(n):
		arr.append([int(i) for i in input()])
	out = 'YES'
	for i in range(n):
		for j in range(n):
			val = arr[i][j]
			if val == 1:
				if i == n - 1 or j == n - 1:
					pass
				elif arr[i + 1][j] == 1:
					pass
				elif arr[i][j + 1] == 1:
					pass
				else:
					out = 'NO'
					break
	print(out)


for _ in range(int(input())):
	n = int(input())
	l = []
	xx = 0
	for i in range(n):
		s = list(str(input()))
		l.append(s)
	for i in range(n):
		for j in range(n):
			c = 0
			k = 0
			if l[i][j] == '1' and i != n - 1 and (j != n - 1):
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				else:
					xx = 1
					break
		if xx == 1:
			break
	if xx == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for we in range(t):
	n = int(input())
	l = []
	for i in range(n):
		d = []
		h = input()
		for i in range(n):
			d.append(int(h[i]))
		d.append(1)
		l.append(d)
	d = [1] * (n + 1)
	l.append(d)
	res = 1
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if l[i][j] == 1 and l[i][j + 1] == 0 and (l[i + 1][j] == 0):
				res = 0
				break
	if res == 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	l = [list(input()) for i in range(n)]
	c = a = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and l[i + 1][j] == '0' and (l[i][j + 1] == '0'):
				c = 1
			if c == 1:
				break
	print('YNEOS'[c::2])


import sys

def input():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(int(input())):
		n = int(input())
		G = [input() + '1' for k in range(n)]
		G.append('1' * (n + 1))
		f = 1
		for i in range(n):
			for j in range(n):
				if G[i][j] == '1':
					if G[i][j + 1] == '0' and G[i + 1][j] == '0':
						f = 0
						break
		print('YES' if f == 1 else 'NO')
main()


def polygon_training():
	t = int(input())
	for _ in range(t):
		n = int(input())
		matriks = [list(input()) for _ in range(n)]
		positive = True
		for i in range(n - 1):
			for (count, number) in enumerate(matriks[i]):
				if count < n - 1:
					if number == '1':
						if matriks[i][count + 1] == '0' and matriks[i + 1][count] == '0':
							positive = False
		print('YES') if positive else print('NO')
polygon_training()


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = []
	for x in range(n):
		s = input()
		s = list(s)
		arr.append(s)
	ans = 'YES'
	for x in range(n):
		for y in range(n):
			if arr[x][y] == '0':
				pass
			elif arr[x][y] == '1':
				if x + 1 >= n or y + 1 >= n:
					pass
				elif x + 1 < n and arr[x + 1][y] == '1':
					pass
				elif y + 1 < n and arr[x][y + 1] == '1':
					pass
				else:
					ans = 'NO'
					break
	print(ans)
	i = i + 1


for i in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(map(int, list(input()))))
	f = 'YES'
	for i in range(n - 1):
		for g in range(n - 1):
			if arr[i][g]:
				if not arr[i][g + 1] and (not arr[i + 1][g]):
					f = 'NO'
					break
		else:
			continue
		break
	print(f)


tent = int(input())
for i in range(tent):
	n = int(input())
	pol = []
	for j in range(n):
		l = list(input())
		pol.append(l)
	resp = 0
	for x in range(n):
		for y in range(n):
			if pol[x][y] == '1':
				if x != n - 1 and y != n - 1:
					if pol[x][y + 1] == '0' and pol[x + 1][y] == '0':
						resp = 1
						break
		if resp == 1:
			break
	if resp == 1:
		print('NO')
	else:
		print('YES')


def solve():
	n = int(input())
	a = [input() for _ in ' ' * n]
	if all((a[i] == '0' * n for i in range(n))):
		print('YES')
		return
	w = False
	for i in range(n):
		w |= a[i][n - 1] == '1' or a[n - 1][i] == '1'
	if not w:
		print('NO')
		return
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					print('NO')
					return
	print('YES')
for _ in ' ' * int(input()):
	solve()


t = int(input())
for q in range(0, t):
	n = int(input())
	a = []
	for i in range(0, n):
		a.append(input())
	flag = 0
	for i in range(0, n):
		for j in range(0, n):
			if a[i][j] == '1':
				if j + 1 < n and i + 1 < n and (a[i + 1][j] == '0') and (a[i][j + 1] == '0'):
					flag = 1
					break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
input = sys.stdin.readline
from bisect import *
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = []
	for i in range(0, n):
		x = input().strip()
		x = list(x)
		x = list(map(int, x))
		a.append(x)
	f = 1
	for i in range(0, n):
		for j in range(0, n):
			if a[i][j] == 1:
				if i + 1 == n or j + 1 == n or a[i + 1][j] == 1 or (a[i][j + 1] == 1):
					continue
				else:
					f = 0
					break
	if f:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for x in range(n):
		p = input()
		l.append(list(map(int, list(p))))
	chk = 0
	for m in range(n):
		for z in range(n):
			if l[m][z] == 1:
				if z == n - 1 or m == n - 1:
					continue
				elif l[m + 1][z] != 1 and l[m][z + 1] != 1:
					chk = 1
		if chk == 1:
			break
	if chk == 1:
		print('NO')
	else:
		print('YES')


from math import sqrt
t = int(input())
for _ in range(t):
	n = int(input())
	m = [input() for _ in range(n)]
	ok = True
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1':
				if m[i + 1][j] == '0' and m[i][j + 1] == '0':
					ok = False
	print('YES' if ok else 'NO')


for _ in range(int(input())):
	n = int(input())
	matrix = []
	flag = True
	for i in range(n):
		matrix.append(list(map(int, list(input()))))
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if i == n - 1 or j == n - 1:
				continue
			elif matrix[i][j] == 1:
				if matrix[i + 1][j] == 0 and matrix[i][j + 1] == 0:
					flag = False
					break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)
for _ in range(int(input())):
	n = int(input())
	t = []
	for i in range(n):
		ls = list(input())
		t.append(ls[:n])
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if t[i][j] == '1':
				if t[i + 1][j] == '0' and t[i][j + 1] == '0':
					f = 1
					break
	if f == 0:
		print('YES')
	else:
		print('NO')


def traverse(a, n, val, i, j, arr):
	if a[i][j] == 1 and arr[i][j] == 0:
		arr[i][j] = val
	else:
		return
	if i - 1 >= 0:
		traverse(a, n, val + 1, i - 1, j, arr)
	if j - 1 >= 0:
		traverse(a, n, val + 1, i, j - 1, arr)
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		tmp = []
		s = input()
		for i in s:
			tmp.append(int(i))
		a.append(tmp)
	arr = []
	for i in range(n):
		tmp = []
		for j in range(n):
			tmp.append(0)
		arr.append(tmp)
	for i in range(n - 1, -1, -1):
		if a[i][n - 1] == 1:
			if arr[i][n - 1] != 0:
				traverse(a, n, arr[i][n - 1], i, n - 1, arr)
			else:
				traverse(a, n, 1, i, n - 1, arr)
	for i in range(n - 1, -1, -1):
		if a[n - 1][i] == 1:
			if arr[n - 1][i] != 0:
				traverse(a, n, arr[n - 1][i], n - 1, i, arr)
			else:
				traverse(a, n, 1, n - 1, i, arr)
	ver = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1 and arr[i][j] == 0:
				ver = False
				break
		if ver == False:
			break
	if ver == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(map(int, input())))
	ans = True
	for i in range(n):
		for j in range(n):
			if arr[j][i] == 1:
				if i < n - 1 and arr[j][i + 1] == 0 and (j < n - 1) and (arr[j + 1][i] == 0):
					ans = False
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for test in range(1, t + 1):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] != '1':
					if a[i + 1][j] != '1':
						ans = False
		if not ans:
			break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for z in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.extend(list(map(int, input())))
	c = 0
	for i in range(n ** 2):
		if i < n ** 2 - n and (i - n + 1) % n != 0:
			if a[i] == 1 and a[i + 1] != 1 and (a[i + n] != 1):
				c = 1
				break
	if c == 1:
		print('NO')
	else:
		print('YES')


import math
t = int(input())
for g in range(0, t):
	n = int(input())
	a = []
	for i in range(0, n):
		s = input()
		a.append(s)
	ans = 1
	breaks = 0
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				else:
					breaks = 1
					break
		if breaks == 1:
			break
	if breaks == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for test in range(0, t):
	n = int(input())
	flag = False
	polygon = []
	for i in range(0, n):
		polygon.append(list(input()))
	for i in range(0, n):
		for j in range(0, n):
			if polygon[i][j] != '0':
				if i == n - 1 or j == n - 1:
					break
				if polygon[i + 1][j] == '0' and polygon[i][j + 1] == '0':
					flag = True
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


from collections import Counter
import sys
sys.setrecursionlimit(10 ** 6)
mod = 1000000007
inf = int(1e+18)
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

def inverse(a):
	return pow(a, mod - 2, mod)

def usearch(x, a):
	lft = 0
	rgt = len(a) + 1
	while rgt - lft > 1:
		mid = (rgt + lft) // 2
		if a[mid] <= x:
			lft = mid
		else:
			rgt = mid
	return lft

def solve():
	n = int(input())
	s = [[int(j) for j in input()] for _ in range(n)]
	d = [[0] * n for _ in range(n)]
	for y in range(n - 1, -1, -1):
		for x in range(n - 1, -1, -1):
			if s[y][x] == 0:
				continue
			if y == n - 1 or x == n - 1:
				d[y][x] = 1
				continue
			if d[y + 1][x] == 1 or d[y][x + 1] == 1:
				d[y][x] = 1
				continue
			print('NO')
			return
	print('YES')

def main():
	n = int(input())
	for i in range(n):
		solve()
main()


def main():
	k = int(input())
	for i in range(k):
		n = int(input())
		mtr = [[0] * n for i in range(n)]
		for j in range(n):
			mtr[j] = list(input())
		tmp = True
		for j in range(n - 1):
			for p in range(n - 1):
				if mtr[j][p] == '1' and mtr[j + 1][p] == '0' and (mtr[j][p + 1] == '0'):
					tmp = False
		print('YES' if tmp else 'NO')
main()


T = int(input())
II = 0
while II < T:
	n = int(input())
	i = 0
	m = []
	while i < n:
		m.append([x for x in input()])
		i += 1
	f = True
	i = 0
	while i < n and f:
		j = 0
		while j < n and f:
			x = m[i][j]
			if x == '1':
				if not ((i == n - 1 or m[i + 1][j] == '1') or (j == n - 1 or m[i][j + 1] == '1')):
					f = False
			j += 1
		i += 1
	if f:
		print('YES')
	else:
		print('NO')
	II += 1


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
while t:
	t -= 1
	n = int(inp().strip())
	a = [inp().strip() for k in range(n)]
	possible = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if not (a[i][j + 1] == '1' or a[i + 1][j] == '1'):
					possible = False
					break
		if not possible:
			break
	if possible:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	s = []
	for i in range(x):
		s.append(input())
	f = 0
	for i in range(x - 1):
		for j in range(x - 1):
			if s[i][j] == '1' and (s[i + 1][j] != '1' and s[i][j + 1] != '1'):
				f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	field = []
	flag = True
	for i in range(n):
		f = list(str(input()))
		field.append(f)
	for i in range(n):
		for j in range(n):
			m = 2
			if field[i][j] == '1':
				if i != n - 1:
					if field[i + 1][j] != '1':
						m -= 1
				if j != n - 1:
					if field[i][j + 1] != '1':
						m -= 1
			if m == 0:
				flag = False
	if flag:
		print('YES')
	else:
		print('NO')


def solve(matrix, n):
	res = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == 1:
				if matrix[i][j + 1] != 1 and matrix[i + 1][j] != 1:
					res = 'NO'
					return res
	return res
for _ in range(int(input())):
	n = int(input())
	matrix = [[int(i) for i in input()] for _ in range(n)]
	res = solve(matrix, n)
	print(res)


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(0, n):
		p = []
		k = input()
		for j in k:
			p.append(int(j))
		arr.append(p)
	flag = 0
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] == 1 or arr[i][j + 1] == 1:
					pass
				else:
					flag = 1
			if flag == 1:
				break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(0, t):
	n = int(input())
	arr = []
	for i in range(0, n):
		arr.append(str(input()))
	c = 0
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] != '1' and arr[i][j + 1] != '1':
					c = 1
					break
	if c == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	s = []
	for o in range(n):
		a = input()
		s.append(a)
	flag = True
	for p in range(n - 1):
		for w in range(n - 1):
			if s[p][w] == '1':
				if s[p][w + 1] == '0' and s[p + 1][w] == '0':
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(0, int(input())):
	n = int(input())
	grid = []
	for i in range(0, n):
		grid.append(input())
	post = 'YES'
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if grid[i][j] == '1' and grid[i + 1][j] == '0':
				if grid[i][j + 1] == '0':
					post = 'NO'
					break
	print(post)


import sys
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	n = int(sys.stdin.readline())
	m = []
	for _n in range(n):
		m.append(input())
	need_to_break = False
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and m[i + 1][j] != '1' and (m[i][j + 1] != '1'):
				print('NO')
				need_to_break = True
				break
		if need_to_break:
			break
	else:
		print('YES')


from sys import stdin
input = stdin.readline

def printM(a):
	for e in a:
		print(*e)
for _ in range(int(input())):
	n = int(input())
	a = []
	for _ in range(n):
		a.append([[c, False] for c in input().strip()])
	f = True
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if a[i][j][0] == '1':
				if i == n - 1 or a[i + 1][j][1] == True or j == n - 1 or (a[i][j + 1][1] == True):
					a[i][j][1] = True
				else:
					print('NO')
					f = False
					break
		if not f:
			break
	else:
		print('YES')


import sys
from math import ceil
from collections import deque
for _ in range(int(input())):
	n = int(input())
	s = []
	for i in range(n):
		s.append([int(i) for i in input() + '1'])
	s.append([1 for i in range(n + 1)])
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if s[i][j] == 1 and s[i + 1][j] == 0 and (s[i][j + 1] == 0):
				ans = 'NO'
	print(ans)


def solve(A, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if A[i][j] == '1':
				if A[i][j + 1] != '1' and A[i + 1][j] != '1':
					print('NO')
					return
	print('YES')
t = int(input())
for _ in range(t):
	n = int(input())
	A = []
	for i in range(n):
		A.append(input())
	if n == 1:
		print('YES')
		continue
	solve(A, n)


t = int(input())
for i in range(t):
	n = int(input())
	data = list()
	for j in range(n):
		temp = list()
		s = input()
		for i in s:
			temp.append(int(i))
		data.append(temp)
	flag = 1
	for j in range(n):
		for k in range(n):
			ele = data[j][k]
			if ele == 1:
				if k == n - 1 or j == n - 1:
					continue
				elif data[j][k + 1] == 1 or data[j + 1][k] == 1:
					continue
				else:
					flag = 0
					break
		if flag == 0:
			break
	if flag == 0:
		print('NO')
	else:
		print('YES')


from sys import stdin

def check(grid):
	for row in range(n - 1):
		for col in range(n - 1):
			if grid[row][col] == '1':
				if grid[row + 1][col] == '0' and grid[row][col + 1] == '0':
					return False
	return True
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	grid = []
	for i in range(n):
		grid.append(stdin.readline().strip())
	if check(grid):
		print('YES')
	else:
		print('NO')


import math
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	x = []
	for i in range(n):
		s = input()
		x.append(s)
	for i in range(n - 1):
		for j in range(n - 1):
			if x[i][j] == '1':
				v = x[i + 1][j]
				v1 = x[i][j + 1]
				if v != '1' and v1 != '1':
					print('NO')
					return
	print('YES')
for _ in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	mt = []
	n = int(input())
	for j in range(n):
		mt.append(list(input()))
	check = True
	for x in range(n - 1):
		for y in range(n - 1):
			if mt[x][y] == '1' and mt[x + 1][y] == '0' and (mt[x][y + 1] == '0'):
				print('NO')
				check = False
			else:
				continue
			if check == False:
				break
		if check == False:
			break
	if check:
		print('YES')


t = int(input())
count = 0
while count < t:
	count += 1
	l = []
	n = int(input())
	for i in range(n):
		l.append(input())
	ud = 0
	lr = 0
	ans = True
	while ud < n - 1:
		lr = 0
		while lr < n - 1:
			if l[ud][lr] == '1':
				if l[ud + 1][lr] != '1' and l[ud][lr + 1] != '1':
					ans = False
					break
			lr += 1
		ud += 1
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for tc in range(t):
	n = int(input())
	T = []
	for i in range(n):
		T.append([True if c == '1' else False for c in input()])
	impossible = False
	for i in range(n - 1, -1, -1):
		if impossible:
			break
		for j in range(n - 1, -1, -1):
			if impossible:
				break
			if T[i][j]:
				if j == n - 1 or i == n - 1:
					continue
				else:
					T[i][j] = True if T[i + 1][j] or T[i][j + 1] else False
				if not T[i][j]:
					impossible = True
	print('YES' if not impossible else 'NO')


for _1 in range(int(input())):
	n = int(input())
	l = [[int(x) for x in input()] for y in range(n)]
	res = 'YES'
	for x in range(n - 1):
		for y in range(n - 1):
			if l[x][y] == 1 and l[x][y + 1] == 0 and (l[x + 1][y] == 0):
				res = 'NO'
		if res == 'NO':
			break
	print(res)


from collections import deque

def solve(n, A):
	dq = deque([])
	for i in range(n):
		if A[i][n - 1] == 1:
			dq.append((n - 1, i))
		if i == n - 1:
			for j in range(n):
				if A[i][j] == 1:
					dq.append((j, i))
	dx = [-1, 0]
	dy = [0, -1]
	while dq:
		(x, y) = dq.pop()
		for i in range(2):
			nx = x + dx[i]
			ny = y + dy[i]
			if 0 <= nx <= n - 1 and 0 <= ny <= n - 1 and (A[ny][nx] == 1):
				dq.append((nx, ny))
		A[y][x] = 0
	for i in range(n):
		if 1 in A[i]:
			return 'NO'
	else:
		return 'YES'
t = int(input())
for k in range(t):
	n = int(input())
	Array = []
	for l in range(n):
		Array.append(list(map(int, input())))
	print(solve(n, Array))


t = int(input())
for _ in range(t):
	l = []
	n = int(input())
	a = 1
	for k in range(n):
		l.append(list(input()))
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and (l[i + 1][j] == '1' or l[i][j + 1] == '1'):
				continue
			elif l[i][j] == '1':
				a = 0
				print('NO')
				break
		if not a:
			break
	if a:
		print('YES')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	matrice = [[char for char in sys.stdin.readline()] for i in range(n)]
	out = True
	for l in range(n):
		if not out:
			break
		for c in range(n):
			if not out:
				break
			if matrice[l][c] == '1' and l != n - 1 and (c != n - 1):
				if matrice[l + 1][c] == '0' and matrice[l][c + 1] == '0':
					out = False
	if out:
		print('YES')
	else:
		print('NO')


import sys

def process():
	n = int(sys.stdin.readline().strip())
	w = []
	for i in range(n):
		w.append([int(s) for s in list(sys.stdin.readline().strip())])
	for i in range(n - 1):
		for j in range(n - 1):
			if w[i][j] == 1:
				if w[i + 1][j] == 0 and w[i][j + 1] == 0:
					print('NO')
					return
	print('YES')
T = int(sys.stdin.readline())
for x in range(T):
	process()


from sys import stdin, stdout
import math

def check(a, n):
	even = odd = 0
	for i in range(n):
		if a[i] % 2 == 0:
			even += 1
		else:
			odd += 1
	return odd % 2 == 0 and even % 2 == 0
t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for i in range(n):
		row = [int(x) for x in input()]
		a.append(row)
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] == 0 and a[i][j + 1] == 0:
					ans = 'NO'
	print(ans)


for T in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		lis = list(input())
		arr.append(lis)
	if n == 1:
		print('YES')
	else:
		res = 'YES'
		for i in range(n - 2, -1, -1):
			for j in range(n - 1):
				if arr[i][j] == '1':
					if arr[i][j + 1] != '1' and arr[i + 1][j] != '1':
						res = 'NO'
						break
		print(res)


from sys import stdin, stdout
for query in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [[int(x) for x in list(stdin.readline().strip())] for y in range(n)]
	mybool = True
	for x in range(n):
		for y in range(n):
			if a[x][y] == 1:
				if not ((x == n - 1 or a[x + 1][y] == 1) or (y == n - 1 or a[x][y + 1] == 1)):
					mybool = False
	if mybool:
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


def check(mat):
	for i in range(len(mat) - 1):
		for j in range(len(mat) - 1):
			if mat[i][j] + mat[i + 1][j] + mat[i][j + 1] == '100':
				return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for _ in range(n):
		mat.append(input())
	print(check(mat))


def main(n, m):
	for i in range(n):
		for j in range(n):
			if m[i][j] == '1':
				if i != n - 1 and j != n - 1 and (m[i][j + 1] != '1') and (m[i + 1][j] != '1'):
					return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	m = []
	for i in range(n):
		m.append(input())
	print(main(n, m))


t = int(input(''))
while t > 0:
	n = int(input(''))
	major_list = []
	for i in range(n):
		l1 = list(input(''))
		major_list.append(l1)
	flag = 0
	for j in range(n - 1):
		ele = major_list[j]
		nexele = major_list[j + 1]
		for x in range(n - 1):
			if ele[x] == '1':
				if ele[x + 1] != '1' and nexele[x] != '1':
					flag = -1
					break
	if flag == -1:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys
t = int(input())
for tc in range(t):
	n = int(input())
	board = []
	for i in range(n):
		board.append(input())
	impossible = False
	for i in range(n):
		for j in range(n):
			if board[i][j] == '1':
				if (i + 1 != n and board[i + 1][j] == '0') and (j + 1 != n and board[i][j + 1] == '0'):
					impossible = True
	if impossible:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	a = int(input())
	v = []
	for i in range(a):
		v.append(list(input()))
	ans = 0
	for i in range(a - 1):
		for j in range(a - 1):
			if v[i][j] == '1' and v[i + 1][j] == v[i][j + 1] == '0':
				ans = 1
				break
		if ans:
			break
	print(['YES', 'NO'][ans])


for _ in range(int(input())):
	x = int(input())
	l = []
	for i in range(x):
		l.append(input())
	p = 'YES'
	b = 0
	for i in range(x):
		for j in range(x):
			if l[i][j] == '1':
				if i != x - 1 and j != x - 1:
					if l[i][j + 1] != '1' and l[i + 1][j] != '1':
						p = 'NO'
						b = 1
						break
		if b == 1:
			break
	print(p)


import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest
import re

def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])

def sortDictWithVal(passedDic):
	temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
	toret = {}
	for tup in temp:
		toret[tup[0]] = tup[1]
	return toret

def sortDictWithKey(passedDic):
	return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
t = INT()
while t != 0:
	n = INT()
	graph = []
	for _ in range(n):
		graph.append(list(input()))
	flag = 0
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if graph[i][j] == '1':
				if graph[i + 1][j] == '1' or graph[i][j + 1] == '1':
					continue
				else:
					flag = 1
					break
			if flag == 1:
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7

def build_grid(H, W, intv, _type, space=True, padding=False):
	if space:
		_input = lambda : input().split()
	else:
		_input = lambda : input()
	_list = lambda : list(map(_type, _input()))
	if padding:
		offset = 1
	else:
		offset = 0
	grid = list2d(H + offset * 2, W + offset * 2, intv)
	for i in range(offset, H + offset):
		row = _list()
		for j in range(offset, W + offset):
			grid[i][j] = row[j - offset]
	return grid

def check(N, grid):
	for i in range(N - 1):
		for j in range(N - 1):
			if grid[i][j]:
				if grid[i + 1][j] != 1 and grid[i][j + 1] != 1:
					return False
	return True
for _ in range(INT()):
	N = INT()
	grid = build_grid(N, N, 0, int, space=0)
	if check(N, grid):
		YES()
	else:
		NO()


def main():
	tests = int(input())
	for _ in range(tests):
		n = int(input())
		matrix = []
		for i in range(n):
			matrix.append([int(i) for i in list(input())])
		yes = True
		for i in range(n - 1):
			for j in range(n - 1):
				if matrix[i][j] == 1:
					if matrix[i + 1][j] == 0 and matrix[i][j + 1] == 0:
						yes = False
						break
			if yes == False:
				break
		if yes:
			print('YES')
		else:
			print('NO')
main()


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

def ri():
	return int(input())

def ria():
	return list(map(int, input().split()))

def ia_to_s(a):
	return ' '.join([str(s) for s in a])

def solve(n, a):
	ok = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if not (i == n - 1 or (i < n - 1 and a[i + 1][j] == 1) or j == n - 1 or (j < n - 1 and a[i][j + 1] == 1)):
					ok = False
					break
	return ok

def main():
	for _ in range(ri()):
		n = ri()
		a = []
		for __ in range(n):
			a.append([int(x) for x in rs()])
		print('YES' if solve(n, a) else 'NO')
main()


import sys
import heapq

def main():
	t = int(input())
	for _ in range(t):
		n = int(sys.stdin.readline().strip())
		table = []
		for i in range(n):
			table.append(sys.stdin.readline().strip())
		visited = set()
		flag = True
		for i in range(n):
			for j in range(n):
				if not flag:
					break
				if table[i][j] == '0' or (i, j) in visited:
					continue
				stack = [(i, j)]
				while stack:
					temp = stack.pop()
					if temp in visited:
						continue
					visited.add(temp)
					tempFlag = False
					if temp[0] < n - 1:
						if table[temp[0] + 1][temp[1]] == '1':
							tempFlag = True
							stack.append((temp[0] + 1, temp[1]))
					if temp[1] < n - 1:
						if table[temp[0]][temp[1] + 1] == '1':
							tempFlag = True
							stack.append((temp[0], temp[1] + 1))
					if temp[0] == n - 1 or temp[1] == n - 1:
						tempFlag = True
					if not tempFlag:
						flag = False
						break
			if not flag:
				break
		if flag:
			print('YES')
		else:
			print('NO')
	return
main()


def jrbefndo():
	n = int(input())
	matrix = []
	for i in range(n):
		s = input()
		row = []
		for c in s:
			row.append(int(c))
		row.append(1)
		matrix.append(row)
	row = [1] * (n + 1)
	matrix.append(row)
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == 1 and (matrix[i + 1][j] == 0 and matrix[i][j + 1] == 0):
				print('NO')
				return
	print('YES')
	return

def main():
	t = int(input())
	for _ in range(t):
		jrbefndo()
	return 0
main()


def validpos(i, j, a):
	n = len(a)
	if i == n - 1 or j == n - 1:
		return True
	elif a[i][j + 1] == '1' or a[i + 1][j] == '1':
		return True
	return False
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = []
	while n:
		n -= 1
		a.append(input())
	valid = True
	for (i, string) in enumerate(a):
		for (j, digit) in enumerate(string):
			if digit == '1':
				valid = valid and validpos(i, j, a)
			if valid is False:
				break
		if valid is False:
			break
	if valid:
		print('YES')
	else:
		print('NO')


def main():
	for _ in range(int(input())):
		n = int(input())
		l = [list(map(int, list(input()))) for i in range(n)]
		(x, y) = (True, True)
		for i in range(n - 1, -1, -1):
			(x, y) = (True, True)
			for j in range(n - 1, -1, -1):
				(x, y) = (True, True)
				if l[i][j] == 1:
					if i + 1 < n:
						if l[i + 1][j] != 1:
							x = False
					if j + 1 < n:
						if l[i][j + 1] != 1:
							y = False
				if not (x or y):
					break
			if not (x or y):
				break
		if not (x or y):
			print('NO')
		else:
			print('YES')
main()


t = int(input())
for test in range(0, t):
	n = int(input())
	s = []
	for i in range(0, n):
		a = input()
		s.append(a)
	f = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if s[i][j] == '1':
				if s[i][j + 1] == '0' and s[i + 1][j] == '0':
					f = False
					break
		if not f:
			break
	if f:
		print('YES')
	else:
		print('NO')


import math

def floodFill(arr, i, j):
	if i < 0 or j < 0 or arr[i][j] != 1:
		return
	arr[i][j] = 2
	floodFill(arr, i - 1, j)
	floodFill(arr, i, j - 1)
T = int(input())
for t in range(T):
	N = int(input())
	arr = [list(map(int, input())) for i in range(N)]
	impossible = False
	for k in range(N):
		floodFill(arr, k, N - 1)
		floodFill(arr, N - 1, k)
	for i in range(N):
		if 1 in arr[i]:
			impossible = True
			break
	if impossible:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = [list(input()) for i in range(n)]
	x = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				print('NO')
				x = 1
				break
		if x == 1:
			break
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = [input() for i in range(n)]
	r = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				r = 'NO'
				break
	print(r)


cases = int(input())
for t in range(cases):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(input()))
	f = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				f = 'NO'
				break
		if f == 'NO':
			break
	print(f)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	flg = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and (l[i + 1][j] != '1' and l[i][j + 1] != '1'):
				flg = 1
				break
	if flg == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
from math import *

def primes(n):
	k = []
	for tz in range(1, ceil(sqrt(n)) + 1):
		if n % tz == 0:
			if n // tz == tz:
				k.append(tz)
			else:
				k.append(tz)
				k.append(n // tz)
	return k
for xy in range(t):
	n = int(input())
	arr = []
	for aq in range(n):
		arr.append(list(input()))
	al = False
	for ty in range(n - 1):
		for tz in range(n - 1):
			if arr[ty][tz] == '1' and arr[ty + 1][tz] != '1' and (arr[ty][tz + 1] != '1'):
				al = True
				break
		if al:
			break
	if al:
		print('NO')
	else:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	s = []
	for i in range(n):
		s.append(input() + '1')
	s.append('1' * len(s[0]))
	f = False
	for i in range(n):
		for j in range(len(s[0]) - 1):
			if s[i][j] == '1' and s[i][j + 1] == '0' and (s[i + 1][j] == '0'):
				f = True
				break
		if f:
			break
	if f:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	correct = True
	for i in range(n):
		l.append(list(map(int, list(input()))))
	if n == 1:
		print('YES')
	else:
		for i in range(n - 1):
			for j in range(n - 1):
				if l[i][j] == 1:
					if l[i + 1][j] == 1 or l[i][j + 1] == 1:
						continue
					else:
						correct = False
						break
		if correct == True:
			print('YES')
		else:
			print('NO')


from sys import stdin
t = int(input())
for _ in range(t):
	n = int(input())
	matrix = []
	for _ in range(n):
		matrix.append(stdin.readline().rstrip())
	answer = 'YES'
	cont = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1' and matrix[i + 1][j] != '1' and (matrix[i][j + 1] != '1'):
				answer = 'NO'
				cont = False
				break
		if not cont:
			break
	print(answer)


t = int(input())
for i in range(0, t):
	n = int(input())
	m = [input() for p in range(n)]
	event = True
	for j in range(0, n - 1):
		for k in range(0, n - 1):
			if m[j][k] == '1' and m[j][k + 1] == '0' and (m[j + 1][k] == '0'):
				event = False
				break
		if not event:
			break
	if event:
		print('YES')
	else:
		print('NO')


def solve(m):
	n = len(m)
	valid = True
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if m[i][j] == 0:
				continue
			else:
				if j == n - 1:
					continue
				if i == n - 1:
					continue
				if m[i + 1][j] == 0 and m[i][j + 1] == 0:
					valid = False
					break
	if valid:
		print('YES')
	else:
		print('NO')
t = int(input())
for _ in range(t):
	n = int(input())
	m = []
	for j in range(n):
		m.append(list(map(int, list(input()))))
	solve(m)


T = int(input())
from collections import Counter
for _ in range(T):
	n = int(input())
	a = [[] for _ in range(n)]
	for i in range(n):
		a[i] = input()
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i + 1][j] == '0' and a[i][j + 1] == '0' and (a[i][j] == '1'):
				ans = 'NO'
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for j in range(n):
		l.append(list(map(int, input())))
	ans = 'YES'
	for j in range(n - 1):
		for k in range(n - 1):
			if l[j][k] == 1:
				if l[j][k + 1] == 0 and l[j + 1][k] == 0:
					ans = 'NO'
					break
	print(ans)


t = int(input())
for tes in range(t):
	arr = []
	n = int(input())
	for i in range(n):
		t = input()
		arr.append(t)
	flag = True
	for i in range(len(arr) - 1):
		for j in range(len(arr[0]) - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
					flag = False
					break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	rows = int(input())
	arr = []
	for i in range(rows):
		arr.append([int(i) for i in input()[:-1]])
	gFlag = True
	for i in range(rows - 2, -1, -1):
		for j in range(rows - 2, -1, -1):
			if arr[i][j] == 1 and (not (arr[i + 1][j] == 1 or arr[i][j + 1] == 1)):
				gFlag = False
				break
		if not gFlag:
			break
	print('YES' if gFlag else 'NO')


t = int(input())
for i in range(t):
	matrix = []
	rows = int(input())
	for t in range(rows):
		matrix.append(list(input()))
	flag = True
	for row in range(rows):
		for col in range(rows):
			if matrix[row][col] == '1':
				if row == rows - 1 or col == rows - 1:
					continue
				if matrix[row + 1][col] == '1':
					continue
				if matrix[row][col + 1] == '1':
					continue
				flag = False
				break
		if flag == False:
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [list(map(int, list(input()))) for i in range(n)]
	a = True
	i = 0
	while a and i + 1 != n:
		for j in range(n - 1):
			if l[i][j] == 0:
				continue
			if l[i][j + 1] == 0 and l[i + 1][j] == 0:
				a = False
				print('NO')
				break
		i += 1
	if a:
		print('YES')


from collections import *
from bisect import *
from math import *
from heapq import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [[1 for i in range(n + 2)] for j in range(n + 2)]
	for i in range(n):
		b = input().rstrip('\n')
		for j in range(len(b)):
			a[i + 1][j + 1] = int(b[j])
	flag = 0
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if a[i][j] == 0:
				continue
			if a[i + 1][j] == a[i][j + 1] == 0:
				flag = 1
				break
	if flag:
		print('NO')
	else:
		print('YES')


import math
tc = int(input())
for _ in range(tc):
	num = int(input())
	arr = []
	for _ in range(num):
		arr.append(input().strip())
	flag = 0
	for i in range(num - 1):
		for j in range(num - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] != '1' and arr[i][j + 1] != '1':
					flag = 1
	if flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = [list(input()) for _ in range(n)]
	temp = 1
	for j in range(n - 1):
		if temp == 0:
			break
		for i in range(n - 1):
			if A[i][j] == '1':
				if A[i + 1][j] == A[i][j + 1] == '0':
					print('NO')
					temp = 0
					break
		if temp == 0:
			break
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = [list(input()) for i in range(n)]
	check = True
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if i == n - 1:
					continue
				if j == n - 1:
					continue
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				check = False
				break
	if check:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	li = []
	posj = []
	posi = []
	st = True
	for i in range(n):
		tmp = list(map(int, input()))
		li.append(tmp)
	for i in range(n - 1):
		for j in range(n - 1):
			if li[i][j] == 0:
				continue
			if li[i + 1][j] == 0 and li[i][j + 1] == 0:
				st = False
				break
	if st == True:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(list(input()))
	ok = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if ar[i][j] == '1':
				if ar[i + 1][j] == '1' or ar[i][j + 1] == '1':
					continue
				else:
					ok = 1
					break
	if ok == 0:
		print('YES')
	else:
		print('NO')
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	board = []
	for i in range(n):
		box = []
		get = input()
		for j in get:
			box.append(int(j))
		board.append(box)
	nocount = 0
	for i in range(n):
		if nocount == 0:
			for j in range(n):
				if board[i][j] == 1:
					if j + 1 < n:
						if board[i][j + 1] == 0:
							if i + 1 < n:
								if board[i + 1][j] == 0:
									print('NO')
									nocount = 1
									break
	if nocount == 0:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		s = str(input())
		lt = list(s)
		l.append(lt)
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '0' and l[i][j + 1] == '0':
					f = 1
					break
	if f == 1:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	m = []
	f = 0
	for i in range(n):
		m.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and (m[i][j + 1] == '0' and m[i + 1][j] == '0'):
				print('NO')
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		r = input()
		r = list(r)
		l = l + r
	for i in range(len(l)):
		l[i] = int(l[i])
	b = True
	for i in range(n * n - 1):
		if l[i] == 1:
			if i % n == n - 1:
				pass
			elif i >= n * (n - 1):
				pass
			elif l[i + 1] != 1 and l[i + n] != 1:
				b = b and False
	if b == False:
		print('NO')
	else:
		print('YES')


import math

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		ar = []
		for k in range(n):
			l = list(map(int, input()))
			ar.append(l)
		flag = 'YES'
		for i in range(n - 1):
			for j in range(n - 1):
				if ar[i][j] == 1:
					if ar[i + 1][j] == 1 or ar[i][j + 1] == 1:
						continue
					else:
						flag = 'NO'
						break
			if flag == 'NO':
				break
		print(flag)
main()


def solve(n, M):
	for i in range(n - 1):
		for j in range(n - 1):
			if M[i][j] == 1 and M[i][j + 1] == 0 and (M[i + 1][j] == 0):
				return 'NO'
	return 'YES'

def main():
	T = int(input())
	for _ in range(T):
		n = int(input())
		M = [[int(c) for c in input().strip()] for _ in range(n)]
		print(solve(n, M))
main()


t = int(input())
for _ in range(t):
	n = int(input())
	grid = []
	for _ in range(n):
		grid.append([int(x) for x in iter(input())])
	impossible = False
	for i in range(n):
		for j in range(n):
			if grid[i][j]:
				if i < n - 1 and grid[i + 1][j] == 0 and (j < n - 1) and (grid[i][j + 1] == 0):
					impossible = True
	print('NO' if impossible else 'YES')


import sys, heapq
from collections import *
from functools import lru_cache
sys.setrecursionlimit(10 ** 6)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		dp = []
		for i in range(n):
			s = input()
			dp.append(s)
		flag = True
		for i in range(n - 1)[::-1]:
			for j in range(n - 1)[::-1]:
				if dp[i][j] == '1' and dp[i + 1][j] == '0' and (dp[i][j + 1] == '0'):
					flag = False
					break
		if flag:
			print('YES')
		else:
			print('NO')
main()


import sys
input = sys.stdin.readline
from math import *

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = []
	for __ in range(n):
		a.append(list(input().strip()))
	f = 1
	for i in range(n - 1):
		if not f:
			break
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] != '1' and a[i + 1][j] != '1':
					f = 0
					break
	if f:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase

def solution(grid, n):
	if n == 1:
		write('YES')
		return
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == 1:
				if grid[i + 1][j] == 0 and grid[i][j + 1] == 0:
					write('NO')
					return
	write('YES')

def main():
	for _ in range(r_int()):
		n = r_int()
		grid = []
		for _ in range(n):
			row = [int(x) for x in input()]
			grid.append(row)
		solution(grid, n)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


for _ in range(int(input())):
	n = int(input())
	a = [input().strip() for i in range(n)]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				flag = False
				print('NO')
				break
		if not flag:
			break
	if flag:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	m = []
	l = []
	for _ in range(n):
		l = input()
		m.append(l)
	c = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1':
				if m[i][j + 1] == '0' and m[i + 1][j] == '0':
					print('NO')
					c = -1
					break
		if c == -1:
			break
	if c == 0:
		print('YES')


def solve(arr, n):
	flag = False
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 0:
				continue
			if i == n - 1 or j == n - 1:
				continue
			if arr[i + 1][j] == 1 or arr[i][j + 1] == 1:
				continue
			else:
				flag = True
				break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	n = int(input())
	lis = []
	for i in range(n):
		arr = list(map(int, list(input())))
		lis.append(arr)
	solve(lis, n)


from sys import stdin
input = stdin.readline
for Ti in range(int(input().strip())):
	n = int(input().strip())
	m = []
	for ri in range(n):
		ci = [int(x) for x in list(input().strip())]
		ci.append(1)
		m.append(ci)
	m.append([1] * (n + 1))
	stack = []
	for cj in range(n + 1):
		stack.append([n, cj])
	for ri in range(n):
		stack.append([ri, n])
	while stack:
		(ci, cj) = stack.pop()
		if m[ci][cj] == 2:
			continue
		m[ci][cj] = 2
		if 0 <= ci - 1 and m[ci - 1][cj] == 1:
			stack.append([ci - 1, cj])
		if 0 <= cj - 1 and m[ci][cj - 1] == 1:
			stack.append([ci, cj - 1])
	ans = True
	for i in range(n):
		for j in range(n):
			if m[i][j] == 1:
				ans = False
	print('YES') if ans else print('NO')


t = int(input())
for qwe in range(t):
	n = int(input())
	matr = [0] * n
	for i in range(n):
		matr[i] = list(map(int, input()))
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			if matr[i][j] == 1 and (matr[i + 1][j] == 1 or matr[i][j + 1] == 1):
				pass
			elif matr[i][j] == 1:
				flag = True
				break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for __ in range(n):
		a.append(input())
	check = 1
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					check = 0
					break
		if check == 0:
			break
	if check == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	lens = int(input())
	matrix = [input() for _ in range(lens)]
	answer = 'Yes'
	for i in range(lens - 1):
		for j in range(lens - 1):
			if matrix[i][j] == '1' and matrix[i + 1][j] == '0' and (matrix[i][j + 1] == '0'):
				answer = 'NO'
				break
		if answer == 'NO':
			break
	print(answer)


for _ in range(int(input())):
	n = int(input())
	(ls, notequal) = ([], 123232232)
	for i in range(n):
		m = list(input())
		ls.append(list(map(int, m)))
	for i in range(len(ls)):
		for j in range(len(ls[i])):
			if ls[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				elif ls[i + 1][j] == 1 or ls[i][j + 1] == 1:
					continue
				else:
					notequal = -1
	if notequal != -1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	h = int(input())
	flag = True
	arr = []
	for i in range(h):
		st = input()
		temp_lis = list(map(int, list(st)))
		arr.append(temp_lis)
	for i in range(h):
		for j in range(h):
			if i == h - 1 or j == h - 1:
				pass
			elif arr[i][j] == 1:
				if arr[i][j + 1] != 1 and arr[i + 1][j] != 1:
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(list((int(x) for x in input())))
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == 1:
				if matrix[i + 1][j] == 1 or matrix[i][j + 1] == 1:
					pass
				else:
					flag = 1
					break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	polygon = []
	for j in range(n):
		polygon.append(input())
	ans = 'YES'
	for j in range(n - 1):
		for k in range(n - 1):
			if polygon[j][k] == '1':
				if j + 1 < n and k + 1 < n:
					if polygon[j + 1][k] == '0' and polygon[j][k + 1] == '0':
						ans = 'NO'
						break
		if ans == 'NO':
			break
	print(ans)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	dp = [list(map(int, list(input().rstrip()))) for _ in range(n)]
	cnt = 0
	for i in range(n):
		for j in range(n):
			if dp[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				elif dp[i + 1][j] == 1 or dp[i][j + 1] == 1:
					continue
				else:
					print('NO')
					return
			else:
				continue
	print('YES')
T = int(input())
for i in range(T):
	solve()


from sys import stdin, stdout
t = int(stdin.readline())
for tt in range(t):
	n = int(stdin.readline())
	a = [[None for i in range(n)] for j in range(n)]
	for i in range(n):
		s = str(stdin.readline())
		for j in range(n):
			a[i][j] = s[j]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					flag = False
					break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


def checkBorder(r, i):
	global n
	ans = [False, False]
	if r == n - 1:
		ans[0] = True
	if i == n - 1:
		ans[1] = True
	return ans
for t in range(int(input())):
	n = int(input())
	a = list()
	for _ in range(n):
		s = input()
		tempL = list()
		for c in s:
			tempL.append(int(c))
		a.append(tempL)
	for r in range(n):
		for i in range(n):
			found = False
			imp = 0
			if a[r][i] == 1:
				bCheck = checkBorder(r, i)
				if not bCheck[0]:
					if a[r + 1][i] != 1:
						imp += 1
					else:
						found = True
				if not bCheck[1]:
					if found:
						continue
					elif a[r][i + 1] != 1:
						imp += 1
			if imp == 2:
				break
		if imp == 2:
			break
	if imp == 2:
		print('NO')
	else:
		print('YES')


def ans(a, n):
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if (j != n - 1 and a[i][j + 1] != '1') and (i != n - 1 and a[i + 1][j] != '1'):
					return False
	return True
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	if ans(a, n):
		print('YES')
	else:
		print('NO')


def empty(*a):
	pass
debug = empty
t = int(input())
pri = []
for zzz in range(t):
	n = int(input())
	arr = [None] * n
	for i in range(n):
		arr[i] = input()
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1':
				debug('YO')
				if arr[i][j + 1] != '1' and arr[i + 1][j] != '1':
					ans = False
					break
	if ans:
		pri.append('YES')
	else:
		pri.append('NO')
print('\n'.join(pri))


def main():
	t = int(input())
	for i in range(t):
		solve()

def solve():
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(input()))
	layer = n - 2
	while layer >= 0:
		if arr[layer][layer] == '1' and arr[layer + 1][layer] == '0' and (arr[layer][layer + 1] == '0'):
			print('NO')
			return
		for j in range(layer - 1, -1, -1):
			if arr[layer][j] == '1' and arr[layer][j + 1] == '0' and (arr[layer + 1][j] == '0') or (arr[j][layer] == '1' and arr[j + 1][layer] == '0' and (arr[j][layer + 1] == '0')):
				print('NO')
				return
		layer -= 1
	print('YES')
main()


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, input().split()))

def In():
	return map(int, input().split())

def I():
	return int(input())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		arr = []
		for i in range(n):
			s = input()
			arr.append(s)
		flag = 1
		for i in range(n):
			for j in range(n):
				c = 0
				if arr[i][j] == '1' and i != n - 1 and (j != n - 1):
					if j + 1 != n and arr[i][j + 1] == '1':
						c += 1
					if i + 1 != n and arr[i + 1][j] == '1':
						c += 1
					if i + 1 != n and j + 1 != n and (c == 0):
						flag = 0
						break
		if flag:
			print('YES')
		else:
			print('NO')
main()


import sys
t = int(input())
for i in range(0, t):
	n = int(input())
	matrix = []
	for j in range(0, n):
		x = input()
		x = list(x)
		x.append('1')
		matrix.append(x)
	r = ['1'] * (n + 1)
	matrix.append(r)
	wrong_found = False
	j = n - 1
	while j >= 0:
		if wrong_found == False:
			for k in range(0, n):
				if wrong_found == False:
					if matrix[j][k] == '1':
						if matrix[j + 1][k] == '1' or matrix[j][k + 1] == '1':
							pass
						else:
							print('NO')
							wrong_found = True
		j -= 1
	if wrong_found == False:
		print('YES')


for i in range(int(input())):
	n = int(input())
	l = []
	for j in range(n):
		l.append(input())
	f = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == '0':
				continue
			if j + 1 < n:
				if l[i][j + 1] == '0':
					if i + 1 < n:
						if l[i + 1][j] == '0':
							f = 1
		if f:
			break
	if f:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	A = [0] * (n + 2)
	A[0] = [0] * (n + 2)
	A[n + 1] = [0] * (n + 2)
	for i in range(1, n + 1):
		A[i] = [int(x) for x in input()]
		x = [0]
		x.extend(A[i])
		x.extend([0])
		A[i] = x.copy()
	poss = True
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if A[i][j] == 1:
				if i != n and j != n and (A[i + 1][j] == 0) and (A[i][j + 1] == 0):
					poss = False
	if poss:
		print('YES')
	else:
		print('NO')


import sys

def arr():
	return list(map(int, input().split()))
input = sys.stdin.readline
for _ in range(int(input())):
	N = int(input())
	mat = []
	for i in range(N):
		mat.append(list(input()))
		mat[i].pop()
	f1 = True
	f2 = True
	for i in range(N):
		if f1 == False and f2 == False:
			break
		f1 = True
		f2 = True
		for j in range(N):
			if f1 == False and f2 == False:
				break
			f1 = True
			f2 = True
			if mat[i][j] == '1':
				for k in range(i + 1, N):
					if mat[k][j] == '1':
						break
					if mat[k][j] == '0':
						f1 = False
						break
				for t in range(j + 1, N):
					if mat[i][t] == '1':
						break
					if mat[i][t] == '0':
						f2 = False
	if f1 == False and f2 == False:
		print('NO')
	else:
		print('YES')


import math
t = int(input())
for z in range(t):
	n = int(input())
	a = []
	d = []
	for i in range(n):
		a.append(input())
		d.append(['o'] * n)
	if a[n - 1][n - 1] == '0':
		d[n - 1][n - 1] = 'n'
	else:
		d[n - 1][n - 1] = 'y'
	f = False
	for i in range(n):
		if f:
			break
		for j in range(n):
			r = False
			if a[n - i - 1][n - j - 1] == '0':
				d[n - i - 1][n - j - 1] = 'n'
			elif a[n - i - 1][n - j - 1] == '1':
				if n - i < n:
					if d[n - i][n - j - 1] == 'n':
						r = True
				else:
					r = False
				if n - j < n:
					if d[n - i - 1][n - j] != 'n':
						r = False
				else:
					r = False
				if r:
					print('NO')
					f = True
					break
				else:
					d[n - i - 1][n - j - 1] = 'y'
	if not f:
		print('YES')


from math import sqrt

def detlen(A, i, j, t=0):
	if A[i][j] == 0:
		return 0
	if i == n - 1 or j == n - 1:
		return t + 1
	if A[i + 1][j] == 1:
		return detlen(A, i + 1, j, t + 1)
	if A[i][j + 1] == 1:
		return detlen(A, i, j + 1, t + 1)
	return -1
t = int(input())
for i in range(t):
	n = int(input())
	A = [None] * n
	for i in range(n):
		A[i] = [None] * n
	for i in range(n):
		w = input()
		for j in range(n):
			A[i][j] = int(w[j])
	B = [None] * n
	for i in range(n):
		B[i] = [None] * n
	for i in range(n):
		for j in range(n):
			B[i][j] = detlen(A, i, j)
	C = []
	for i in range(n):
		C.append(min(B[i]))
	r = min(C)
	if r == -1:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = []
	for i in range(n):
		a = input()
		l.append(a)
	ok = True
	for i in range(n - 1):
		for j in range(len(l[i]) - 1):
			if l[i][j] == '1':
				if l[i + 1][j] != '1' and l[i][j + 1] != '1':
					ok = False
					break
	if ok:
		print('YES')
	else:
		print('NO')


test = int(input())
for i in range(test):
	n = int(input())
	arr = [0 for i in range(n)]
	for i in range(n):
		arr[i] = [int(i) for i in input()]
	flag = 1
	for i in range(n):
		for j in range(n):
			if arr[i][j]:
				if i == n - 1 or j == n - 1:
					continue
				elif arr[i + 1][j] or arr[i][j + 1]:
					continue
				else:
					flag = 0
					break
		if not flag:
			break
	if not flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	m = []
	n = int(input())
	for __ in range(n):
		m.append(list(map(int, list(input().replace(' ', '')))))
	print_yes = True
	for i in range(n - 1):
		no = False
		for j in range(n - 1):
			if m[i][j] == 1 and i + 1 < n and (m[i + 1][j] == 0) and (j + 1 < n) and (m[i][j + 1] == 0):
				print('NO')
				no = True
				break
		if no:
			print_yes = False
			break
	if print_yes:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for _ in range(n):
		mat = input()
		a.append(mat)
	nf = 0
	for i in range(n - 1):
		flag = 0
		for j in range(n - 1):
			flag = 0
			if a[i][j] == '1':
				if a[i][j + 1] == '1' or a[i + 1][j] == '1':
					flag = 0
				else:
					print('NO')
					flag = 1
					nf = 1
			if flag == 1:
				nf = 1
				break
		if flag == 1:
			nf = 1
			break
	if nf == 0:
		print('YES')


t = int(input())
l = []
for i in range(t):
	n = int(input())
	m = []
	for j in range(n):
		m.append(list(map(int, list(input()))))
	r = 1
	for j in range(n):
		for k in range(n):
			if m[j][k] == 1:
				if j != n - 1 and k != n - 1 and (m[j + 1][k] == 0) and (m[j][k + 1] == 0):
					r = 0
					break
		if r == 0:
			break
	if r == 0:
		l.append('NO')
	else:
		l.append('YES')
for i in l:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	mat = [list(input()) for i in range(n)]
	f = True
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i + 1][j] != '1' and mat[i][j + 1] != '1':
					f = False
	print('YES' if f else 'NO')


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
for _ in range(int(cin())):
	n = int(cin())
	lst = []
	for _ in range(n):
		lst.append(cin()[:-1])
	f = 0
	for i in range(n):
		for j in range(n):
			if lst[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif lst[i + 1][j] == '0' and lst[i][j + 1] == '0':
					f = 1
					break
		if f == 1:
			break
	if f:
		cout('No\n')
	else:
		cout('Yes\n')


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(n, matrix):
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == 0 or i + 1 == n or j + 1 == n:
				continue
			if matrix[i + 1][j] == 1:
				continue
			if matrix[i][j + 1] == 1:
				continue
			return False
	return True
for i in tests():
	n = int_in()
	matrix = [[int(x) for x in str_in()] for y in range(n)]
	bool_out(solve(n, matrix))


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = []
	for i in range(n):
		s = input()
		l = [int(x) for x in s]
		a += [l]
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] == 0 and a[i][j + 1] == 0:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


cases = int(input())

def check_matrix(w):
	n = len(w)
	for i in range(n - 1):
		for j in range(n - 1):
			if w[i][j] == 1 and w[i + 1][j] == 0 and (w[i][j + 1] == 0):
				return 'NO'
	return 'YES'
for _ in range(cases):
	n = int(input())
	w = []
	for i in range(n):
		x = list(map(int, input()))
		w.append(x)
	print(check_matrix(w))


for ii in range(int(input())):
	n = int(input())
	li = []
	for i in range(n):
		a = str(input())
		b = [int(i) for i in a]
		li.append(b)
	flag = True
	if li == [[0 for i in range(n)] for j in range(n)]:
		print('YES')
	else:
		count = 0
		for i in range(n - 1):
			if li[n - 1][i] == 1:
				count += 1
			if li[i][n - 1] == 1:
				count += 1
		if li[n - 1][n - 1] == 1:
			count += 1
		if count == 0:
			print('NO')
		else:
			for i in range(n):
				for j in range(n):
					if i < n - 1 and j < n - 1:
						if li[i][j] == 1:
							if li[i + 1][j] == 0 and li[i][j + 1] == 0:
								flag = False
								break
				if flag == False:
					break
			if flag:
				print('YES')
			else:
				print('NO')


for _ in range(int(input())):
	t = int(input())
	l = []
	o = True
	for _ in range(t):
		l.append(input())
	if t == 1:
		o = True
	else:
		for i in range(t - 1):
			for j in range(t - 1):
				if l[i][j] == '1' and l[i + 1][j] == '0' and (l[i][j + 1] == '0'):
					o = False
					break
					break
	if o:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	matrix = ''
	T = int(input())
	for z in range(T):
		matrix = matrix + input()
	possible = True
	for i in range(len(matrix) - T):
		if not possible:
			break
		elif matrix[i] == '1':
			if (i + 1) % T != 0:
				if matrix[i + 1] != '1':
					if matrix[i + T] != '1':
						possible = False
	if possible:
		print('YES')
	else:
		print('NO')


import sys
import os
import heapq
import math
try:
	path = './file/input.txt'
	if os.path.exists(path):
		sys.stdin = open(path, 'r')
except:
	pass
t = int(input())

def printd(value):
	pass
for _ in range(t):
	n = int(input())
	m = []
	for _ in range(n):
		arr = input()
		m.append(arr)
	result = 'YES'
	for row in range(n - 2, -1, -1):
		for col in range(row - 1, -1, -1):
			if m[row][col] == '1':
				if m[row + 1][col] != '1' and m[row][col + 1] != '1':
					result = 'NO'
		for col in range(row, -1, -1):
			if m[col][row] == '1':
				if m[col + 1][row] != '1' and m[col][row + 1] != '1':
					result = 'NO'
	print(result)


t = int(input())

def r(n):
	for i in range(n):
		for j in range(n):
			if m[i][j] == 1 and i < n - 1 and (j < n - 1) and (m[i + 1][j] == 0) and (m[i][j + 1] == 0):
				return False
	return True
for _ in range(t):
	n = int(input())
	m = []
	for i in range(n):
		x = str(input())
		x = [int(i) for i in x]
		m.append(x)
	if r(n):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	data = []
	for i in range(n):
		temp = input()
		data.append(temp)
	result = True
	for i in range(n):
		for j in range(n):
			if data[i][j] == '1' and i != n - 1 and (j != n - 1):
				if data[i + 1][j] == '1' or data[i][j + 1] == '1':
					pass
				else:
					result = False
	if result:
		print('YES')
	else:
		print('NO')


tc = int(input())
while tc > 0:
	n = int(input())
	report = [input() for _ in range(n)]
	valid = True
	for r in range(n):
		for c in range(n):
			if report[r][c] == '1':
				if r == n - 1 or c == n - 1:
					pass
				elif report[r + 1][c] == '1' or report[r][c + 1] == '1':
					pass
				else:
					print('NO')
					valid = False
					break
		if not valid:
			break
	else:
		print('YES')
	tc -= 1


def main():
	import sys
	import copy
	sys.setrecursionlimit(10 ** 6)
	input = sys.stdin.readline
	T = int(input())
	for t in range(T):
		n = int(input())
		mat = [1] * (n + 1) ** 2
		for i in range(n):
			for (j, k) in enumerate([int(x) for x in list(input().strip())]):
				mat[(n + 1) * i + j] = k
		ans = [0] * n ** 2
		for i in range(n - 1):
			for j in range(n - 1):
				ans[(n + 1) * i + j] = mat[(n + 1) * i + j + 1] - mat[(n + 1) * i + j] + mat[(n + 1) * (i + 1) + j] - mat[(n + 1) * i + j]
		if -2 in ans:
			print('NO')
		else:
			print('YES')
main()


from math import *
import math

def r1(t):
	return t(input())

def r2(t):
	return [t(i) for i in input().split()]

def r3(t):
	return [t(i) for i in input()]
for zzz in range(r1(int)):
	n = r1(int)
	g = []
	for i in range(n):
		g.append(r3(int))
	ha = True
	for i in range(n - 1):
		for j in range(n - 1):
			if g[i][j] and (not g[i][j + 1]) and (not g[i + 1][j]):
				ha = False
				break
		if not ha:
			break
	if ha:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	A = [input() for _ in range(n)]
	print('NO' if any((A[i][j] == '1' and A[i][j + 1] == A[i + 1][j] == '0' for i in range(n - 1) for j in range(n - 1))) else 'YES')


for _ in range(int(input())):
	n = int(input())
	lst = []
	for i in range(n):
		temp = list(input())
		lst.append(temp)
	directions = [[1, 0], [0, 1]]
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if lst[i][j] == '1':
				mainFlag = False
				flag = False
				for direction in directions:
					new_i = i + direction[0]
					new_j = j + direction[1]
					if new_i < n and new_j < n:
						if lst[new_i][new_j] == '1':
							mainFlag = True
					else:
						flag = True
					if flag:
						mainFlag = True
				if not mainFlag:
					ans = 'NO'
	print(ans)


n = int(input())
l = []
for _ in range(n):
	g = []
	for _ in range(int(input())):
		g.append(list(input()))
	l.append(g)

def check(l):
	b = True
	for i in range(len(l) - 1):
		for j in range(len(l) - 1):
			if l[i][j] == '1' and l[i + 1][j] == '0' and (l[i][j + 1] == '0'):
				b = False
	return b

def print_normal(b):
	if b:
		print('YES')
	else:
		print('NO')
for t in l:
	print_normal(check(t))


t = int(input())
for i in range(t):
	n = int(input())
	x = []
	for j in range(n):
		a = list(map(int, list(input())))
		x.append(a)
	flag = 0
	for j in range(n - 1):
		for k in range(n - 1):
			if x[j][k] == 1:
				if x[j + 1][k] == 1 or x[j][k + 1] == 1:
					pass
				else:
					flag = 1
					break
	if flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for poligon in range(t):
	good = 1
	n = int(input())
	p = []
	for i in range(n):
		p.append(list(input()))
	for i in range(n):
		for j in range(n):
			if p[i][j] == '1':
				if i != n - 1 and j != n - 1:
					if p[i + 1][j] != '1' and p[i][j + 1] != '1':
						good = 0
	if good == 1:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = [input()[:-1] + '1' for i in range(n)] + ['1' * (n + 1)]
	flag = True
	for i in range(n):
		for j in range(n):
			if s[i][j] == '0':
				continue
			if s[i + 1][j] == '1' or s[i][j + 1] == '1':
				continue
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import math

def solve():
	n = int(input())
	l1 = []
	for i in range(n):
		s = input()
		l1.append(s)
	for i in range(n - 1):
		for j in range(n - 1):
			if l1[i][j] == '1':
				v = l1[i + 1][j]
				v1 = l1[i][j + 1]
				if v != '1' and v1 != '1':
					print('NO')
					return
	print('YES')
for _ in range(int(input())):
	solve()


t = int(input())
for ii in range(t):
	n = int(input())
	(hor, vert) = ([], [[] for _ in range(n)])
	for i in range(n):
		l = [int(i) for i in input()]
		hor.append(l)
	(ok, ok2) = (True, True)
	for i in range(n - 1):
		for j in range(n - 1):
			if hor[i][j] == 1:
				if hor[i][j + 1] != 1 and hor[i + 1][j] != 1:
					ok = False
					break
		if ok == False:
			break
	if ok == False:
		print('NO')
	else:
		print('YES')


t = int(input())
for u in range(t):
	n = int(input())
	w = []
	t = True
	q = [[False] * n for i in range(n)]
	for i in range(n):
		w.append(input())
	for i in range(n):
		f = False
		for j in range(n - 1, -1, -1):
			if w[i][j] == '0':
				f = True
				q[i][j] = True
			else:
				if f:
					pass
				else:
					q[i][j] = True
				f = False
	for j in range(n):
		f = False
		for i in range(n - 1, -1, -1):
			if w[i][j] == '0':
				f = True
				q[i][j] = True
			else:
				if not f:
					q[i][j] = True
				f = False
	for i in range(len(q)):
		for j in range(len(q[i])):
			if q[i][j] is False:
				t = False
	if t:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	s = []
	for h in range(n):
		s.append(input() + '1')
	s.append('1' * n)
	b = 0
	for k in range(n):
		for l in range(n):
			if s[k][l] == '1' and s[k][l + 1] != '1' and (s[k + 1][l] != '1'):
				b = 1
				break
	if b == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	ans = 'YES'
	arr = []
	for i in range(n):
		arr.append(list(map(int, list(input().strip()))) + [1])
	arr.append([1] * n)
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1 and (arr[i][j + 1] == 0 and arr[i + 1][j] == 0):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = []
	for x in range(n):
		a.append(input().rstrip())
	flag = 'YES'
	for x in range(n - 1):
		for y in range(n - 1):
			if a[x][y] == '1' and a[x + 1][y] == '0' and (a[x][y + 1] == '0'):
				flag = 'NO'
		if flag == 'NO':
			break
	print(flag)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	board = []
	for _ in range(n):
		board.append(list(stdin.readline().strip()))
	ok = True
	for i in range(n):
		for j in range(n):
			if board[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				if board[i][j + 1] == '1' or board[i + 1][j] == '1':
					continue
				ok = False
				break
		if not ok:
			break
	if ok:
		print('YES')
	else:
		print('NO')


from math import sqrt
t = int(input())
for q in range(0, t):
	n = int(input())
	a = []
	for i in range(0, n):
		a.append(input())
	flag = 'YES'
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				flag = 'NO'
	print(flag)


import sys
import heapq
import random
import collections

def solve(grid):
	for (i, row) in enumerate(grid[:-1]):
		for (j, val) in enumerate(row[:-1]):
			if val == 1:
				if grid[i + 1][j] == 0 and grid[i][j + 1] == 0:
					return 'NO'
	return 'YES'

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	nrows = int(input())
	grid = []
	for _ in range(nrows):
		grid.append(list(map(int, input())))
	res = solve(grid)
	print(res)


def solve(n, arr):
	for i in range(n):
		for j in range(n):
			if arr[i][j]:
				if i == n - 1 or j == n - 1:
					continue
				if not (arr[i + 1][j] or arr[i][j + 1]):
					print('NO')
					return
	print('YES')
for _ in range(int(input())):
	n = int(input())
	arr = [[int(i) for i in list(input())] for _ in range(n)]
	solve(n, arr)


from sys import stdin, exit, setrecursionlimit
from collections import deque
from heapq import *
from math import gcd
from math import ceil, log2
setrecursionlimit(10000000)
from collections import deque
input = stdin.readline
lmi = lambda : list(map(int, input().split()))
mi = lambda : map(int, input().split())
si = lambda : input().strip('\n')
ssi = lambda : input().strip('\n').split()
for _ in range(int(input())):
	n = int(input())
	grid = [si() for i in range(n)]
	ok = True
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if grid[i][j] == '1':
				if i + 1 >= n or j + 1 >= n or (grid[i + 1][j] == '1' or grid[i][j + 1] == '1'):
					continue
				ok = False
	if ok:
		print('YES')
	else:
		print('NO')


import sys
import math
import collections
import threading

def main():
	n = int(input())
	a = [input() for _ in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					print('NO')
					return
	print('YES')
tn = int(input())
for _ in range(tn):
	main()


import sys, math

def printDivisors(n):
	i = 1
	a = []
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				a.append(i)
			else:
				a.append(i)
				a.append(n // i)
		i = i + 1
	return a
t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					flag = False
					break
		if flag == False:
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(map(int, list(input()))))
	b = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i < n - 1 and j < n - 1:
					if mat[i + 1][j] == 0 and mat[i][j + 1] == 0:
						b = False
	if b:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		s = input().strip('\n')
		temp = []
		for k in s:
			temp.append(k)
		mat.append(temp)
	flag = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif mat[i + 1][j] != '1' and mat[i][j + 1] != '1':
					flag = False
					break
		if flag == False:
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lst = []
	for i in range(n):
		lst.append(input())
	boo = False
	x = n - 1
	for i in range(x):
		for j in range(x):
			if lst[i][j] == '1' and lst[i + 1][j] != '1':
				if lst[i][j + 1] != '1':
					boo = True
	if boo == True:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	polygon = []
	for j in range(n):
		line = list(input())
		polygon.append(line)
	flag = 0
	for x in range(n):
		for y in range(n):
			if polygon[x][y] == '1':
				if x != n - 1 and y != n - 1:
					if polygon[x][y + 1] == '0' and polygon[x + 1][y] == '0':
						flag = 1
						break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


def dfs(i, j):
	global matrix
	if j > 0 and matrix[i][j - 1] == '1':
		matrix[i][j - 1] = '0'
		dfs(i, j - 1)
	if i > 0 and matrix[i - 1][j] == '1':
		matrix[i - 1][j] = '0'
		dfs(i - 1, j)
for y in range(int(input())):
	n = int(input())
	matrix = [list(input()) for i in range(n)]
	for i in range(n):
		if matrix[i][n - 1] == '1':
			dfs(i, n - 1)
		if matrix[n - 1][i] == '1':
			dfs(n - 1, i)
	fl = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1':
				fl = 0
	if fl:
		print('YES')
	else:
		print('No')


import os
os_input = os.read(0, int(10000000.0)).split()
os_input_pos = -1

def cin():
	global os_input_pos
	os_input_pos += 1
	return os_input[os_input_pos]
T = int(cin())
res = []
while T:
	T -= 1
	n = int(cin())
	g = [cin().decode() for i in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			if g[i][j] == '1' and g[i + 1][j] == '0' and (g[i][j + 1] == '0'):
				res.append('NO'.encode())
				break
		else:
			continue
		break
	else:
		res.append('YES'.encode())
os.write(1, '\n'.encode().join(res))


import os
import sys
import math
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		matrix = []
		for i in range(n):
			lis = list(input())
			matrix.append(lis)
		check = []
		counter = 0
		for i in range(n - 1, -1, -1):
			for j in range(n - 1, -1, -1):
				(c1, c2) = (0, 0)
				if matrix[i][j] == '1':
					if j + 1 < n:
						if matrix[i][j + 1] == '1':
							continue
						else:
							c1 = 1
					if i + 1 < n:
						if matrix[i + 1][j] == '1':
							continue
						else:
							c2 = 1
					if c1 == 1 and c2 == 1:
						counter = 1
						break
			if counter == 1:
				break
		if counter == 1:
			print('no')
		else:
			print('yes')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def solve():
	n = int(input())
	arr = []
	for i in range(n):
		arr.append([int(x) for x in input()])
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				elif arr[i + 1][j] == 1 or arr[i][j + 1] == 1:
					continue
				else:
					return 'NO'
	return 'YES'
t = int(input())
while t:
	print(solve())
	t -= 1


import math

def check(x, y, m):
	n = len(m)
	return not (x + 1 < n and m[x + 1][y] == 0 and (y + 1 < n) and (m[x][y + 1] == 0))

def main():
	n = int(input())
	m = [[int(x) for x in input()] for i in range(n)]
	for i in range(n):
		for j in range(n):
			if m[i][j] == 1 and (not check(i, j, m)):
				return 'NO'
	return 'YES'
t = int(input())
while t > 0:
	t -= 1
	ans = main()
	print(ans)


from math import *
t = int(input())
m = []
n = []
for i in range(t):
	n.append(int(input()))
	x = []
	for j in range(n[i]):
		x.append(input())
	m.append(x)
for i in range(t):
	flag = 0
	for j1 in range(n[i] - 1):
		for j2 in range(n[i] - 1):
			if m[i][j1][j2] == '1' and m[i][j1][j2 + 1] == '0' and (m[i][j1 + 1][j2] == '0'):
				flag = 1
	if flag == 1:
		print('No')
	else:
		print('Yes')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	c = []
	for i in range(n):
		c.append([0] * n)
	for i in range(n - 1, -1, -1):
		if l[i][n - 1] == '1':
			c[i][n - 1] = 1
		if l[n - 1][i] == '1':
			c[n - 1][i] = 1
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if i - 1 >= 0:
				if c[i][j] and l[i - 1][j] == '1':
					c[i - 1][j] = 1
			if j - 1 >= 0:
				if c[i][j] and l[i][j - 1] == '1':
					c[i][j - 1] = 1
	flag = 1
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1' and (not c[i][j]):
				flag = 0
				break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


def f():
	n = int(input())
	a = [[int(x) for x in input()] for j in range(n)]
	b = [[0 for i in range(n + 1)] for j in range(n + 1)]
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				b[i][j + 1] = 1
				b[i + 1][j] = 1
				b[i + 1][j + 1] = 2
	for i in range(n):
		for j in range(n):
			if b[i][j] == 2:
				print('NO')
				return
	print('YES')
t = int(input())
for i in range(t):
	f()


def main():
	import sys
	input = sys.stdin.readline
	for __ in [0] * int(input()):
		N = int(input())
		S = [tuple(map(int, input()[:-1])) for _ in [0] * N]
		for i in range(N - 1):
			for j in range(N - 1):
				if S[i][j] and S[i + 1][j] == S[i][j + 1] == 0:
					print('NO')
					break
			else:
				continue
			break
		else:
			print('YES')

def __starting_point():
	main()
__starting_point()


t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for _ in range(n):
		inp = input()
		mat.append(inp)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i][j + 1] == '0' and mat[i + 1][j] == '0':
					flag = 1
					break
		if flag == 1:
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
input = lambda : stdin.readline().strip()
fast_print = stdout.write
from math import sqrt, floor
T = int(input())
for _ in range(T):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(list(input()))
	visited = {}
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if matrix[i][j] == '1':
				try:
					if visited[i + 1, j]:
						visited[i, j] = True
				except:
					try:
						if visited[i, j + 1]:
							visited[i, j] = True
					except:
						if i == n - 1 or j == n - 1:
							visited[i, j] = True
	for i in range(n):
		for j in range(n):
			try:
				if matrix[i][j] == '1' and visited[i, j]:
					pass
			except:
				fast_print('NO\n')
				break
		else:
			continue
		break
	else:
		fast_print('YES\n')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		x = input()
		l.append(x)
	flag = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				flag = 1
				print('NO')
				break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


INF = 10 ** 18
t = int(input())

def solve():
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				ans = False
	if ans:
		print('YES')
	else:
		print('NO')
for tt in range(t):
	solve()


from collections import Counter
import math
import sys
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for _ in range(int(input())):
	n = INT()
	a = []
	for i in range(n):
		x = input()
		arr = [int(i) for i in x]
		arr.append(11)
		a.append(arr)
	x = []
	for i in range(n):
		x.append(11)
	a.append(x)
	flag = 0
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if a[i + 1][j] == 0 and a[i][j + 1] == 0:
					flag = 1
					break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())

def q(l, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == 1:
				if l[i + 1][j] == 0 and l[i][j + 1] == 0:
					return 'NO'
	return 'YES'
for i in range(t):
	n = int(input())
	l = []
	for i in range(n):
		a = input()
		a = list(a)
		a = list(map(int, a))
		l.append(a)
	print(q(l, n))


laco = int(input())
for t in range(laco):
	n = int(input())
	array = []
	for i in range(n):
		b = input()
		temp = list(b)
		array.append(temp)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if array[i][j] == '1' and array[i + 1][j] == '0' and (array[i][j + 1] == '0'):
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	matrix = [[int(num) for num in input()] for _ in range(n)]
	res = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if matrix[i][j] and (not matrix[i + 1][j]) and (not matrix[i][j + 1]):
				res = False
	if res:
		print('YES')
	else:
		print('NO')


def floodfill(mat, i, j):
	if i < 0 or j < 0:
		return
	elif mat[i][j] == '1':
		mat[i][j] = '0'
		floodfill(mat, i - 1, j)
		floodfill(mat, i, j - 1)
total_case = int(input())
for i in range(total_case):
	n = int(input())
	matrix = [list(input().rstrip()) for i in range(n)]
	for i in range(n):
		if matrix[i][n - 1] == '1':
			floodfill(matrix, i, n - 1)
		if matrix[n - 1][i] == '1':
			floodfill(matrix, n - 1, i)
	expected = ['0'] * n
	result = True
	for i in range(n):
		if not matrix[i] == expected:
			result = False
			break
	if result:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [list(input()) + ['1'] for i in range(n)]
	a.append(list('1' * (n + 1)))
	bo = 1
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if a[i + 1][j] != '1' and a[i][j + 1] != '1':
					print('NO')
					bo = 0
					break
		if not bo:
			break
	if bo:
		print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	mat = [input() for i in range(n)]
	dojde_jak_mama_grabcia = [[0] * n for i in range(n)]
	for i in range(n):
		if mat[i][n - 1] == '1':
			dojde_jak_mama_grabcia[i][n - 1] = 1
	for i in range(n):
		if mat[n - 1][i] == '1':
			dojde_jak_mama_grabcia[n - 1][i] = 1
	odp = 'YES'
	for ii in range(n - 1):
		i = n - 2 - ii
		for jj in range(n - 1):
			j = n - 2 - jj
			if mat[i][j] == '1':
				if dojde_jak_mama_grabcia[i + 1][j] + dojde_jak_mama_grabcia[i][j + 1] == 0:
					odp = 'NO'
				else:
					dojde_jak_mama_grabcia[i][j] = 1
	print(odp)


import sys
from collections import defaultdict as dd

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
from math import *
import copy
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(x) for x in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def bo(i):
	return ord(i) - ord('a')
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = []
	for i in range(n):
		p = si()
		a.append(p)
	f = 0
	valid = [[0 for i in range(n)] for j in range(n)]
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if a[i][j] == '1':
				f = 0
				for k in range(j + 1, n):
					if valid[i][k] == 1:
						break
					if a[i][k] != '1':
						f = 1
						break
				for k in range(i + 1, n):
					if valid[k][j] == 1:
						break
					if a[k][j] != '1':
						f += 1
						break
				if f == 2:
					break
				valid[i][j] = 1
		if f == 2:
			break
	print('YES' if f != 2 else 'NO')


w = int(input())
for s in range(w):
	n = int(input())
	a = []
	e = 0
	for i in range(n):
		a.append(input())
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1' and i < n - 1 and (j < n - 1):
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					e = 100
					break
		if e == 100:
			print('NO')
			break
	if e == 0:
		print('YES')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	p = list()
	for _ in range(n):
		p.append(input())
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			pi = p[i][j]
			if pi == '1':
				if p[i + 1][j] != '1' and p[i][j + 1] != '1':
					ans = False
					break
	if ans:
		print('YES')
	else:
		print('NO')


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		maps = []
		for i in range(n):
			kap = list(input())
			maps.append(kap)
		ans = True
		for i in range(n):
			for j in range(n):
				if maps[i][j] == '1':
					if i != n - 1 and j != n - 1:
						if maps[i + 1][j] != '1' and maps[i][j + 1] != '1':
							ans = False
							break
		if ans:
			yield 'YES'
		else:
			yield 'NO'
t = int(input())
ans = gift()
print(*ans, sep='\n')


def f(i, j):
	global used, a
	if i < 0 or j < 0 or used[i][j]:
		return
	if a[i][j] == '0':
		return
	used[i][j] = True
	f(i - 1, j)
	f(i, j - 1)
for _ in range(int(input())):
	n = int(input())
	a = [input() for _ in range(n)]
	used = [[False] * n for _ in range(n)]
	for i in range(n):
		f(i, n - 1)
		f(n - 1, i)
	ok = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1' and (not used[i][j]):
				ok = False
				break
		if not ok:
			break
	if ok:
		print('YES')
	else:
		print('NO')


def qe(matrix, n):
	for i in range(n):
		last = 1
		for j in reversed(range(n)):
			if matrix[i][j] == 1:
				matrix[i][j] += last
			if matrix[i][j] == 0:
				last = 0
			else:
				last = 1
	for i in range(n):
		last = 1
		for j in reversed(range(n)):
			if matrix[j][i] == 1:
				matrix[j][i] += last
			if matrix[j][i] == 1 and last == 0:
				return False
			if matrix[j][i] == 0:
				last = 0
			else:
				last = 1
	return True
m = int(input())
for i in range(m):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(list(map(int, input())))
	print('YES' if qe(matrix, n) else 'NO')


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
from math import sqrt
for _ in range(int(input())):
	n = int(input())
	a = [[0] * (n + 2)]
	for i in range(1, n + 1):
		a.append([0])
		a[i].extend([int(i) for i in input()])
		a[i].append(1)
	a.append([1] * (n + 2))
	flag = 0
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if a[i][j] == 1:
				if not a[i + 1][j] | a[i][j + 1]:
					flag = 1
	print(['YES', 'NO'][flag])


import math
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input()))
	key = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j + 1] != '1' and l[i + 1][j] != '1':
					key = 1
					break
	if key == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	s = ''
	b = 0
	for i in range(n):
		a = input()
		s = s + a
	for i in range(n * n - n):
		if (i + 1) % n != 0:
			if s[i] == '1':
				if s[i + 1] == '0' and s[i + n] == '0':
					b = 1
					print('NO')
					break
	if b == 0:
		print('YES')


from sys import stdin
input = iter(stdin.readlines()).__next__
t = int(input())
for _ in range(t):
	n = int(input())
	grid = [list(map(int, list(input().strip()))) for _ in range(n)]
	okay = True
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if grid[i][j] == 1 and grid[i + 1][j] == 0 and (grid[i][j + 1] == 0):
				okay = False
				break
		if okay == False:
			break
	print('YES' if okay else 'NO')


def run():
	n = int(input())
	grid = [[int(v) for v in input()] for _ in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == 1 and grid[i][j + 1] == 0 and (grid[i + 1][j] == 0):
				print('NO')
				return
	else:
		print('YES')
for _ in range(int(input())):
	run()


def solve(grid, n, ans):
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == '1':
				if grid[i][j + 1] == '0' and grid[i + 1][j] == '0':
					ans.append('NO')
					return
	ans.append('YES')

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		grid = []
		for j in range(n):
			grid.append(input())
		solve(grid, n, ans)
	for i in ans:
		print(i)
main()


for _ in range(int(input())):
	tam = int(input())
	camp = []
	for i in range(tam):
		camp.append([int(i) for i in input()])
	flag = True
	for i in range(tam - 1):
		for j in range(tam - 1):
			if camp[i][j] == 1:
				if camp[i][j + 1] == 0 and camp[i + 1][j] == 0:
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import queue

def solve(n, arr):
	visited = [[0 for j in range(n)] for i in range(n)]
	q = queue.Queue()
	for i in range(0, n):
		if arr[i][n - 1] == '1':
			q.put((i, n - 1))
			visited[i][n - 1] = 1
	for j in range(0, n - 1):
		if arr[n - 1][j] == '1':
			q.put((n - 1, j))
			visited[n - 1][j] = 1
	dirs = [[-1, 0], [0, -1]]
	while q.qsize() > 0:
		cur = q.get()
		for dir in dirs:
			x = cur[0] + dir[0]
			y = cur[1] + dir[1]
			if x >= 0 and x < n and (y >= 0) and (y < n) and (arr[x][y] == '1') and (visited[x][y] == 0):
				q.put((x, y))
				visited[x][y] = 1
	for i in range(0, n):
		for j in range(0, n):
			if arr[i][j] == '1' and visited[i][j] == 0:
				print('NO')
				return
	print('YES')
	return
	return
t = int(input())
for i in range(0, t):
	n = int(input())
	arr = [[] for i in range(n)]
	for j in range(n):
		arr[j] = list(input())
	solve(n, arr)


t = int(input())
for i in range(t):
	n = int(input())
	mtx = []
	for j in range(n):
		mtx.append([int(x) for x in list(input())])
	flag = 0
	for j in range(n):
		for k in range(n):
			if mtx[j][k] == 1 and k + 1 < n and (mtx[j][k + 1] == 0) and (j + 1 < n) and (mtx[j + 1][k] == 0):
				print('NO')
				flag = 1
				break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


t = int(input())
for ex in range(t):
	n = int(input())
	matrix = list()
	legit_ones = list()
	for i in range(n):
		matrix.append([int(j) for j in input()])
		legit_ones.append([int(0) for j in range(n)])
	for i in range(n):
		if matrix[i][n - 1] == 1:
			legit_ones[i][n - 1] = 1
		if matrix[n - 1][i] == 1:
			legit_ones[n - 1][i] = 1
	correct = True
	for i in range(n - 2, -1, -1):
		if not correct:
			break
		for j in range(n - 2, -1, -1):
			if matrix[i][j] == 1 and (legit_ones[i + 1][j] == 1 or legit_ones[i][j + 1] == 1):
				legit_ones[i][j] = 1
			elif matrix[i][j] == 1 and (not (legit_ones[i + 1][j] == 1 or legit_ones[i][j + 1] == 1)):
				correct = False
				break
	if correct:
		print('YES')
	else:
		print('NO')


import sys
inp = [x for x in sys.stdin.read().split()]
ii = 0
ttt = int(inp[ii])
ii += 1
res = []
for _ in range(ttt):
	n = int(inp[ii])
	ii += 1
	a = inp[ii:ii + n]
	ii += n
	ok = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				ok = False
				break
		if not ok:
			break
	res.append('YES' if ok else 'NO')
print('\n'.join((str(x) for x in res)))


for _ in range(int(input())):
	n = int(input())
	l = []
	dp = []
	for i in range(n):
		l.append(input())
		dp.append([False] * n)
	f = True
	for i in range(n - 1, -1, -1):
		flag = False
		for j in range(n - 1, -1, -1):
			if l[i][j] == '0':
				continue
			if i == n - 1 or j == n - 1:
				dp[i][j] = False if l[i][j] == '0' else True
				continue
			if dp[i][j + 1] or dp[i + 1][j]:
				dp[i][j] = True
			else:
				print('NO')
				flag = True
				break
		if flag:
			f = False
			break
	if f:
		print('YES')


def check(m):
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and m[i + 1][j] == '0' and (m[i][j + 1] == '0'):
				return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	print(check([input() for _ in range(n)]))


t = int(input())
for tt in range(t):
	n = int(input())
	a = [[0 for j in range(n)] for i in range(n)]
	for i in range(n):
		s = input()
		for j in range(n):
			a[i][j] = int(s[j])
	f = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if a[i][j] == 1 and a[i + 1][j] == 0 and (a[i][j + 1] == 0):
				f = False
	if f:
		print('YES')
	else:
		print('NO')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [''] * n
	flag = 1
	for i in range(n):
		a[i] = input()
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] == '0' and a[i + 1][j] == '0':
					flag = 0
					break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for k in range(t):
	n = int(input())
	rama = []
	for i in range(n):
		array = []
		string = input()
		for j in string:
			array.append(int(j))
		rama.append(array)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if rama[i][j] == 1:
				if rama[i + 1][j] == 1 or rama[i][j + 1] == 1:
					flag = 1
				else:
					flag = 2
					break
		if flag == 2:
			break
	if flag == 1 or flag == 0:
		print('YES')
	else:
		print('NO')


n = int(input())
for _ in range(n):
	x = int(input())
	arr = [[0 for i in range(x)] for j in range(x)]
	for i in range(0, x):
		arr[i] = list(map(int, str(input())))
	flag = 0
	for i in range(0, x - 1):
		for j in range(0, x - 1):
			if arr[i][j] == 1:
				if arr[i][j + 1] == 0 and arr[i + 1][j] == 0:
					flag = 2
					break
		if flag == 2:
			break
	if flag == 2:
		print('NO')
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections

def solve(n, arr):
	total = 0
	seen = set()
	bfs = collections.deque([])
	for i in range(n):
		for j in range(n):
			if j == n - 1 and arr[i][j] == '1' and ((i, j) not in seen):
				seen.add((i, j))
				bfs.append((i, n - 1))
			if i == n - 1 and arr[i][j] == '1' and ((i, j) not in seen):
				seen.add((i, j))
				bfs.append((i, j))
			total += int(arr[i][j] == '1')
	while bfs:
		(x, y) = bfs.popleft()
		for (i, j) in ((x, y - 1), (x - 1, y)):
			if i >= 0 and j >= 0 and ((i, j) not in seen) and (arr[i][j] == '1'):
				seen.add((i, j))
				bfs.append((i, j))
	return len(seen) == total
t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	ans = solve(n, arr)
	if ans:
		print('YES')
	else:
		print('NO')


def calc(arr, n):
	flag = False
	for i in range(n):
		for j in range(n):
			if i + 1 < n and j + 1 < n:
				if arr[i][j] == '1' and arr[i + 1][j] == '0' and (arr[i][j + 1] == '0'):
					flag = True
	if flag:
		return 'NO'
	else:
		return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(input()))
	print(calc(arr, n))


import sys
import math
file_name = ''
if file_name:
	f = open(file_name, 'r')
else:
	f = sys.stdin
line = f.readline()
q = int(line)
for _ in range(q):
	line = f.readline()
	n = int(line)
	polygon = []
	for i in range(n):
		line = f.readline()
		l = [int(x) for x in line.rstrip()]
		polygon.append(l)
	answer = True
	for i in range(n - 1):
		for j in range(n - 1):
			el = polygon[i][j]
			if el == 1:
				if polygon[i + 1][j] == 0 and polygon[i][j + 1] == 0:
					answer = False
	if answer:
		print('YES')
	else:
		print('NO')


INT_MAX = 10 ** 30 + 7
MOD = 10 ** 12 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]
import math as M

def Factors(n):
	A = [1]
	for i in range(2, int(M.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				A.append(i)
			else:
				A.append(i)
				A.append(n // i)
	if len(A) == 1:
		A.append(n)
	A.sort()
	return A

def calc(n, k):
	B = LIST_2D_ARRAY(n, n)
	bool = True
	for i in range(n):
		for j in range(n):
			if A[i][j] == 1:
				if i != n - 1 and j != n - 1:
					if A[i][j + 1] == 1 or A[i + 1][j] == 1:
						continue
					bool = False
				elif i == n - 1 and j != n - 1:
					continue
				elif i != n - 1 and j == n - 1:
					continue
				elif i == n - 1 and j == n - 1:
					continue
	if bool:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	n = int(input())
	A = []
	for i in range(n):
		x = input()
		D = list((int(i) for i in x))
		A.append(D)
	calc(n, A)


from math import sqrt
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(map(int, list(input()))))
	ans = True
	for i in range(n):
		for j in range(n):
			if a[i][j]:
				flag1 = True
				flag2 = True
				if i != n - 1 and a[i + 1][j] == 0:
					flag1 = False
				if j != n - 1 and a[i][j + 1] == 0:
					flag2 = False
				if flag1 | flag2 == False:
					ans = False
					break
		if ans == False:
			break
	if ans:
		print('YES')
	else:
		print('NO')


import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

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

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def solve():
	for _ in range(ii()):
		n = ii()
		a = []
		for i in range(n):
			a.append(si())
		f = 1
		for i in range(n - 1):
			for j in range(n - 1):
				if a[i][j] == '1':
					if a[i][j + 1] == '1' or a[i + 1][j] == '1':
						f = 1
					else:
						f = 0
						break
			if f == 0:
				break
		if f:
			print('YES')
		else:
			print('NO')
solve()


import sys
cases = int(sys.stdin.readline())
for _ in range(cases):
	n = int(sys.stdin.readline())
	M = []
	for i in range(n):
		M.append(list(map(int, list(sys.stdin.readline().strip()))))
	res = True
	for i in range(n - 1):
		for j in range(n - 1):
			if M[i][j] == 1 and M[i + 1][j] == 0 and (M[i][j + 1] == 0):
				res = False
				break
	print('YES' if res else 'NO')


def solve(n, F: list):
	for i in range(n - 1):
		for j in range(n - 1):
			if F[i][j] == '1' and F[i + 1][j] == '0' and (F[i][j + 1] == '0'):
				return 'NO'
	return 'YES'
for z in range(int(input())):
	k = int(input())
	A = []
	for z in range(k):
		A.append(list(input()))
	print(solve(k, A))


from collections import Counter
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = []
	flag = 0
	for i in range(n):
		a.append(list(str(input())))
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					print('NO')
					flag = 1
					break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		n = II()
		aa = [[int(c) for c in SI()] for _ in range(n)]
		for si in range(n):
			if aa[si][n - 1]:
				aa[si][n - 1] = 0
				stack = []
				stack.append((si, n - 1))
				while stack:
					(i, j) = stack.pop()
					if i and aa[i - 1][j]:
						aa[i - 1][j] = 0
						stack.append((i - 1, j))
					if j and aa[i][j - 1]:
						aa[i][j - 1] = 0
						stack.append((i, j - 1))
		for sj in range(n):
			if aa[n - 1][sj]:
				aa[n - 1][sj] = 0
				stack = []
				stack.append((n - 1, sj))
				while stack:
					(i, j) = stack.pop()
					if i and aa[i - 1][j]:
						aa[i - 1][j] = 0
						stack.append((i - 1, j))
					if j and aa[i][j - 1]:
						aa[i][j - 1] = 0
						stack.append((i, j - 1))
		if sum((sum(row) for row in aa)):
			print('NO')
		else:
			print('YES')
main()


from collections import defaultdict as dfd
import math
import sys
import heapq

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def main():
	for _ in range(II()):
		N = II()
		B = []
		for i in range(N):
			C = input()
			C = list(C)
			B.append(C)
		z = 0
		for i in range(len(B)):
			for j in range(len(B[i])):
				if B[i][j] == '1':
					if j == N - 1 or i == N - 1:
						continue
					elif B[i + 1][j] != '1' and B[i][j + 1] != '1':
						print('NO')
						z = 1
						break
			if z == 1:
				break
		if z == 0:
			print('YES')
main()


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	S = [input()[:-1] for _ in range(n)]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if S[i][j] == '1':
				if S[i + 1][j] == '0' and S[i][j + 1] == '0':
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())

def check(m, i, j, n):
	return i < n - 1 and m[i + 1][j] == '1' or (j < n - 1 and m[i][j + 1] == '1') or i == n - 1 or (j == n - 1)
for _ in range(t):
	n = int(input())
	m = []
	for i in range(n):
		m.append(input())
	ok = True
	for i in range(n):
		if not ok:
			break
		for j in range(n):
			if m[i][j] == '1' and (not check(m, i, j, n)):
				ok = False
				break
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list((int(x) for x in input())))
	pos = [[False] * n for _ in range(n)]
	queue = []
	for i in range(n):
		queue.append((i, n - 1))
		queue.append((n - 1, i))
	while len(queue):
		(x, y) = queue.pop()
		if arr[x][y] == 1:
			if y > 0:
				queue.append((x, y - 1))
			if x > 0:
				queue.append((x - 1, y))
		arr[x][y] = 0
	cnt = 0
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1:
				cnt += 1
	print('NO' if cnt else 'YES')


a = int(input())
for i in range(a):
	x = int(input())
	row = []
	ans = []
	for i in range(x):
		s = input()
		om = []
		for i in range(len(s)):
			om.append(int(s[i]))
		ans.append(om)
		flag = 0
		glag = 0
		for i in range(len(ans)):
			for j in range(len(ans)):
				if ans[i][j] == 1:
					flag = 0
					if i + 1 < len(ans):
						if ans[i + 1][j] == 0:
							flag += 1
					if j + 1 < len(ans):
						if ans[i][j + 1] == 0:
							flag += 1
					if flag == 2:
						glag = 1
						break
	if glag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	square = []
	for i in range(n):
		square.append([int(i) for i in input()])
	ok = 'YES'
	for i in range(n):
		for j in range(n):
			if square[i][j]:
				if j < n - 1 and i < n - 1:
					if not (square[i][j + 1] or square[i + 1][j]):
						ok = 'NO'
	print(ok)


for t in range(int(input())):
	n = int(input())
	mat = [[int(i) for i in input()] for j in range(n)]
	possible = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i < n - 1 and j < n - 1:
					if mat[i][j + 1] == 1 or mat[i + 1][j] == 1:
						continue
					else:
						possible = False
						print('NO')
						break
		if not possible:
			break
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	array = [input() for _ in range(n)]
	flag = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if array[i][j] == '1' and array[i + 1][j] == '0' and (array[i][j + 1] == '0'):
				flag = 0
				break
		if flag == 0:
			break
	if flag:
		print('YES')
	else:
		print('NO')


def feasible(n, a):
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if a[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				if a[i][j + 1] != 1 and a[i + 1][j] != 1:
					return False
	else:
		return True
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	a = []
	for _ in range(n):
		a.append(list(map(int, list(input().strip()))))
	print('YES' if feasible(n, a) else 'NO')


import math

def valid(n, i, j):
	return 0 <= i < n and 0 <= j < n
t = int(input())
for _ in range(t):
	n = int(input())
	matrix = [0 for i in range(n)]
	(x, y) = (0, 0)
	for i in range(n):
		matrix[i] = list(map(int, input()))
	g = 0
	ones = []
	last = []
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if matrix[i][j] == 1:
				ones.append((i, j))
				if i == n - 1 or j == n - 1:
					last.append((i, j))
	for i in last:
		(x, y) = (i[0], i[1])
		q = [(x, y)]
		matrix[x][y] = 0
		while q:
			(x, y) = q.pop(0)
			(a, b) = (x - 1, y)
			if valid(n, a, b) and matrix[a][b] == 1:
				matrix[a][b] = 0
				q.append((a, b))
			(a, b) = (x, y - 1)
			if valid(n, a, b) and matrix[a][b] == 1:
				matrix[a][b] = 0
				q.append((a, b))
	for i in ones:
		if matrix[i[0]][i[1]] == 1:
			g = 1
			break
	if g:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	n = int(input())
	re = n
	lst = []
	while re:
		k = input()
		lst.append(list(k))
		re -= 1
	dp = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		if lst[n - 1][i] == '1':
			dp[n - 1][i] = 1
		if lst[i][n - 1] == '1':
			dp[i][n - 1] = 1
	res = 0
	for row in range(n - 2, -1, -1):
		for col in range(n - 2, -1, -1):
			if lst[row][col] == '1':
				if dp[row + 1][col] == 1 or dp[row][col + 1] == 1:
					dp[row][col] = 1
					continue
				else:
					res = 1
					break
		if res == 1:
			break
	if res == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


import queue
import math
t = int(input())
while t:
	n = int(input())
	a = []
	for i in range(n):
		x = input()
		a.append(x)
	flag = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i + 1 < n and j + 1 < n and (a[i + 1][j] == '0') and (a[i][j + 1] == '0'):
					flag = False
					break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		s = list(input())
		a.append(s)
	ok = 1
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i + 1 < n and a[i + 1][j] == '1' or (j + 1 < n and a[i][j + 1] == '1'):
					ok = 1
				elif i + 1 != n and j + 1 != n:
					ok = 0
			if ok == 0:
				break
		if ok == 0:
			break
	if ok == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	(l, x) = ([], 1)
	for __ in range(n):
		l.append(input())
	for i in range(n - 1):
		y = 0
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '0' and l[i][j + 1] == '0':
					y = 1
					break
		if y:
			x = 0
			break
	if x:
		print('YES')
	else:
		print('NO')


n = int(input())
for _ in range(n):
	fail = False
	n = int(input())
	a = [[int(s) for s in input()] for s in range(n)]
	for i in range(n):
		for j in range(n):
			x = a[i][j]
			if x == 1:
				if i == n - 1 or j == n - 1:
					pass
				elif a[i + 1][j] == 1 or a[i][j + 1] == 1:
					pass
				else:
					fail = True
					break
	if fail:
		print('NO')
	else:
		print('YES')


def solve(grid, n):
	if n == 1:
		return 'YES'
	for i in range(n - 1, 0, -1):
		for x in range(i):
			if grid[i - 1][x] == 1:
				if grid[i][x] != 1 and grid[i - 1][x + 1] != 1:
					return 'NO'
		for y in range(i - 1):
			if grid[y][i - 1] == 1:
				if grid[y + 1][i - 1] != 1 and grid[y][i] != 1:
					return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	grid = [[int(i) for i in input()] for j in range(n)]
	ans = solve(grid, n)
	print(ans)


t = int(input())
while t:
	n = int(input())
	grid = []
	flag = 0
	for i in range(n):
		grid.append(list(map(int, list(input()))))
	for r in range(n - 1):
		for c in range(n - 1):
			if grid[r][c] == 1:
				if grid[r][c + 1] or grid[r + 1][c]:
					continue
				else:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t = t - 1


def func():
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(list(input()))
	for i in range(n - 1):
		for j in range(n - 1):
			if ar[i][j] == '1':
				if ar[i][j + 1] == '0' and ar[i + 1][j] == '0':
					print('NO')
					return
	print('YES')
	return
for _ in range(int(input())):
	func()


def solve():
	n = int(input())
	matrix = [[0 for i in range(n)] for j in range(n)]
	for j in range(n):
		s = input()
		for i in range(n):
			if s[i] == '1':
				matrix[i][j] = 1
	adj = [[False for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == 1:
				if i == n - 1 or j == n - 1:
					adj[i][j] = True
				if adj[i][j] == False:
					if matrix[i][j + 1] == 0 and matrix[i + 1][j] == 0:
						return 'NO'
	return 'YES'
for _ in range(int(input())):
	print(solve())


import collections

def solve(n, grid):
	maxRows = [n for _ in range(n)]
	maxColumns = [n for _ in range(n)]
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if grid[i][j] == '1':
				if maxRows[i] == j + 1 or maxColumns[j] == i + 1:
					maxColumns[j] = i
					maxRows[i] = j
				else:
					return False
	return True
t = int(input().strip())
for i in range(t):
	grid = []
	n = int(input().strip())
	for _ in range(n):
		grid.append(input().strip())
	print('YES' if solve(n, grid) else 'NO')


t = int(input())
for i in range(t):
	n = int(input())
	m = []
	for j in range(n):
		m.append([int(k) for k in input()])
	bad = False
	for row in range(n - 1):
		for column in range(n - 1):
			if m[row][column] == 1 and m[row + 1][column] == 0 and (m[row][column + 1] == 0):
				bad = True
				break
		if bad == True:
			break
	if bad == True:
		print('NO')
	else:
		print('YES')


t = int(input())
for k in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	res = 'YES'
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '1':
				if j == n - 1 or i == n - 1:
					continue
				if arr[i + 1][j] == '1' or arr[i][j + 1] == '1':
					continue
				i = n
				res = 'NO'
				break
	print(res)


for i in ' ' * int(input()):
	n = int(input())
	a = [input() for v in range(n)]
	z = 0
	for row in range(n - 1):
		if z == 1:
			break
		for column in range(n - 1):
			if a[row][column] == '1' == a[row][column + 1] or a[row][column] == '1' == a[row + 1][column]:
				pass
			elif a[row][column] == '1' != a[row][column + 1] and a[row][column] == '1' != a[row + 1][column]:
				z = 1
				print('NO')
				break
	if z == 0:
		print('YES')


sc = iter(open(0).read().split())
ni = lambda : int(next(sc))
ns = lambda : next(sc)

def solve():
	for i in range(N):
		for j in range(N):
			if S[i][j] == '1':
				if not (S[i + 1][j] == '1' or S[i][j + 1] == '1'):
					return False
	return True
T = ni()
for _ in range(T):
	N = ni()
	S = [ns() + '1' for _ in range(N)] + ['1' * (N + 1)]
	if solve():
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		s = input()
		lst = []
		for i in s:
			lst.append(int(i))
		matrix.append(lst)
	(x, y) = (n - 1, n - 1)
	yo = True
	for i in range(x):
		for j in range(y):
			if matrix[i][j + 1] == 0 and matrix[i + 1][j] == 0 and (matrix[i][j] == 1):
				yo = False
	if yo:
		print('YES')
	else:
		print('NO')


times = int(input())
ans = []
for i in range(times):
	n = int(input())
	matrix = []
	columns = []
	for y in range(n):
		columns += [[char for char in input()]]
	matrix = columns
	possible = True
	for y in range(0, n - 1):
		for x in range(0, n - 1):
			if int(matrix[y][x]) == 1:
				if int(matrix[y + 1][x]) == 0:
					if int(matrix[y][x + 1]) == 0:
						possible = False
	if possible:
		ans += ['YES']
	else:
		ans += ['NO']
for i in range(len(ans)):
	print(ans[i])


n = int(input())
for n_time in range(n):
	n_square = int(input())
	polygon = [0] * n_square

	def check(n_square, polygon):
		for y in range(n_square - 1):
			for x in range(n_square - 1):
				if polygon[y][x] == '1' and polygon[y + 1][x] == '0' and (polygon[y][x + 1] == '0'):
					return 'NO'
		return 'YES'
	for n_row in range(n_square):
		polygon[n_row] = list(input())
	print(check(n_square, polygon))


import collections, heapq, itertools, math, functools
groupby = itertools.groupby
rs = lambda : input()
ri = lambda : int(input())
rm = lambda : map(int, input().split())
rai = lambda : [int(x) for x in input().split()]

def solve():
	for i in range(n):
		for j in range(n):
			if i < n - 1 and j < n - 1 and (a[i][j] == 1) and (a[i + 1][j] == 0) and (a[i][j + 1] == 0):
				return False
	return a
for t in range(ri()):
	n = ri()
	a = [[] for i in range(n)]
	for i in range(n):
		temp = list(input())
		for ch in temp:
			a[i].append(int(ch))
	if solve():
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	lines = []
	for _ in range(n):
		lines.append(input())
	breaker = False
	yas = True
	for x in range(0, n - 1):
		if breaker:
			break
		else:
			for y in range(0, n - 1):
				if lines[x][y] == '0' or (lines[x][y] == '1' and (lines[x + 1][y] == '1' or lines[x][y + 1] == '1')):
					pass
				else:
					print('NO')
					breaker = True
					yas = False
					break
	if yas:
		print('YES')


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	lt = []
	for _ in range(n):
		s = input() + '1'
		lt.append(s)
	lt = lt + ['1' * (n + 1)]
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if lt[i][j] == '1':
				if lt[i + 1][j] == '0' and lt[i][j + 1] == '0':
					ans = 'NO'
	print(ans)


t = int(input())
for i in range(t):
	si = int(input())
	k = []
	for i in range(si):
		p = input()
		t = []
		for i in p:
			t.append(int(i))
		k.append(t)
	flag = True
	for i in range(si):
		for j in range(si):
			if k[i][j] == 1:
				if j + 1 <= si - 1 and i + 1 <= si - 1:
					if k[i][j + 1] == 0 and k[i + 1][j] == 0:
						flag = False
						break
	if flag:
		print('YES')
	else:
		print('NO')


from functools import lru_cache

def possible(m, n):

	@lru_cache(None)
	def squareIsPossible(i, j):
		if i == n - 1 or j == n - 1:
			return True
		if m[i + 1][j] == '1' and squareIsPossible(i + 1, j):
			return True
		if m[i][j + 1] == '1' and squareIsPossible(i, j + 1):
			return True
		return False
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if m[i][j] == '1' and squareIsPossible(i, j) == False:
				return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	m = []
	for z in range(n):
		m.append(input())
	print('YES' if possible(m, n) else 'NO')


def bfs(graph, source, n):
	queue = [source]
	visited = {source: True}
	while queue:
		a = queue.pop(0)
		g = graph[a[0]][a[1]]
		for (x, y) in [(a[0] + 1, a[1]), (a[0], a[1] + 1)]:
			if x < n and y < n:
				g += graph[x][y]
				if (x, y) not in visited:
					visited[x, y] = True
					queue.append((x, y))
		if g == '100':
			print('NO')
			return
	print('YES')
for _ in range(int(input())):
	n = int(input())
	s = []
	for _ in range(n):
		s.append(input())
	bfs(s, (0, 0), n)


for i in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l1 = list(input())
		l.append(l1)
	z = 0
	for i in range(n):
		for j in range(n):
			flag = 0
			if l[i][j] == '1':
				if j != n - 1:
					if l[i][j + 1] == '0':
						flag += 1
				if i != n - 1:
					if l[i + 1][j] == '0':
						flag += 1
			else:
				z += 1
			if flag == 2:
				break
		if flag == 2:
			break
	if z == n * n:
		print('YES')
	elif flag == 2:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	arr = [[1 for j in range(n + 1)] for i in range(n + 1)]
	flag = True
	for i in range(n):
		st = input()
		for j in range(n):
			arr[i][j] = int(st[j])
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1:
				flag = flag and (arr[i + 1][j] or arr[i][j + 1])
	if flag:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	a = int(input())
	l = []
	for j in range(a):
		l.append(input())
	r = 0
	for j in range(a - 1):
		for k in range(a - 1):
			if l[j][k] == '1':
				if l[j + 1][k] == '0' and l[j][k + 1] == '0':
					r = 1
					break
		if r == 1:
			break
	if r == 1:
		print('NO')
	else:
		print('YES')


def ans(n, m):
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1':
				if m[i + 1][j] == '1':
					continue
				elif m[i][j + 1] == '1':
					continue
				else:
					return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	m = list()
	for q in range(n):
		m.append(input())
	print(ans(n, m))


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		l = input()
		a.append(l)
	if n == 1:
		print('YES')
		continue
	vis = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == n - 1 or j == n - 1:
				if a[i][j] == '1':
					vis[i][j] = 1
	f = 0
	for i in range(n - 2, 0, -1):
		if vis[i][n - 1] == 0:
			if vis[i - 1][n - 1] and vis[i][n - 2]:
				f = 1
				break
		if vis[n - 1][i] == 0:
			if vis[n - 1][i - 1] == 1 and vis[n - 2][i] == 1:
				f = 1
				break
	if f:
		print('NO')
		continue
	rf = 0
	cf = 0
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if a[i][j] == '0':
				vis[i][j] = 0
			if a[i][j] == '1':
				rf = 0
				cf = 0
				if vis[i][j + 1] == 0:
					rf = 1
				if vis[i + 1][j] == 0:
					cf = 1
				if rf == 1 and cf == 1:
					break
				else:
					vis[i][j] = 1
		if rf == 1 and cf == 1:
			break
	if rf == 1 and cf == 1:
		print('NO')
	else:
		print('YES')


from math import *
from collections import *
from operator import itemgetter
import bisect
i = lambda : input()
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
I = lambda : list(map(int, input().split()))
chrIdx = lambda x: ord(x) - 96
idxChr = lambda x: chr(96 + x)
t = ii()
for _ in range(t):
	n = ii()
	m = [0] * n
	for i in range(n):
		m[i] = input()
	flag = 1
	for i in range(n):
		for j in range(n):
			if m[i][j] == '1':
				if (i < n - 1 and m[i + 1][j] != '1') and (j < n - 1 and m[i][j + 1] != '1'):
					flag = 0
					break
		if flag == 0:
			break
	if flag == 0:
		print('NO')
	else:
		print('YES')


from collections import defaultdict as dd
import math

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
q = nn()
for _ in range(q):
	n = nn()
	grid = []
	for _ in range(n):
		grid.append(input())
	possible = True
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == '1':
				if grid[i + 1][j] == '0' and grid[i][j + 1] == '0':
					possible = False
					break
	if possible:
		print('YES')
	else:
		print('NO')


import sys

def solve():
	return None

def main():
	q = []
	for line in sys.stdin.readlines():
		q.append(line)
	for i in range(len(q)):
		q[i] = q[i].rstrip().split(' ')
	start = 1
	while start < len(q):
		size = q[start][0]
		ret = 'YES'
		test = []
		for j in range(1, int(size) + 1):
			temp = list(q[start + j][0])
			test.append(temp)
		for i in range(len(test)):
			for j in range(len(test[0])):
				if test[i][j] == '1':
					if j + 1 < len(test[0]) and i + 1 < len(test):
						if test[i][j + 1] == '0' and test[i + 1][j] == '0':
							ret = 'NO'
		start += int(size) + 1
		print(ret)
main()


import math
for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		kk = input()
		l.append(kk)
	flag = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '0' and l[i][j + 1] == '0':
					flag = 'NO'
					continue
	print(flag)


import math
for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	u = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					u = 1
				else:
					u = 2
					break
		if u == 2:
			break
	if u == 1 or u == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t += -1
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input()))
	check = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				else:
					check = 0
					break
		if check == 0:
			break
	if check:
		print('YES')
	else:
		print('NO')


import sys
input = lambda : sys.stdin.readline().strip()

def solve():
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(input())
	for i in range(n):
		for j in range(n):
			ok = False
			if mat[i][j] == '1':
				if i + 1 < n:
					if mat[i + 1][j] == '1':
						ok = True
				if j + 1 < n:
					if mat[i][j + 1] == '1':
						ok = True
				if i == n - 1 or j == n - 1:
					ok = True
				if not ok:
					print('NO')
					return
	print('YES')
t = int(input())
while t:
	t -= 1
	solve()


import math
for _ in range(int(input())):
	n = int(input())
	A = []
	c = 0
	for i in range(n):
		a = input()
		A.append(a)
	for i in range(n):
		for j in range(n):
			if A[i][j] == '1':
				if j == n - 1 or i == n - 1:
					pass
				elif A[i][j + 1] == '1' or A[i + 1][j] == '1':
					pass
				else:
					c = 1
					break
	if c == 0:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	l = []
	for j in range(int(input())):
		x = input()
		x = list(x)
		l.append(x)
	ans = 0
	for j in range(len(l) - 1):
		for k in range(len(l) - 1):
			if l[j][k] == '1':
				if l[j][k + 1] == '0' and l[j + 1][k] == '0':
					ans = 1
					break
	if ans == 0:
		print('YES')
	else:
		print('NO')


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	n = int(I())
	arr = []
	for i in range(n):
		arr.append(list(I()))
	flag = 1
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '1':
				if i != n - 1 and j != n - 1:
					if arr[i][j + 1] == '0' and arr[i + 1][j] == '0':
						flag = 0
						break
		if flag == 0:
			break
	if flag:
		ans += 'YES\n'
	else:
		ans += 'NO\n'
print(ans)


from math import *
for u in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input()))
	d = []
	for i in range(n):
		k = []
		for j in range(n):
			k.append(l[j][i])
		d.append(k)
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j] == '1' and l[i + 1][j] == '1' or l[i][j] == l[i][j + 1] == '1':
					continue
				else:
					f = 1
					break
	if f == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	mil = []
	for _ in range(n):
		mil.append(list(input()))
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if mil[i][j] == '1' and (not (mil[i][j + 1] == '1' or mil[i + 1][j] == '1')):
				ans = 'NO'
				break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	array = []
	for i in range(n):
		array.append([int(num) for num in input().strip('\n')])
	exit_code = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if array[i][j] == 1:
				if array[i + 1][j] == 0 and array[i][j + 1] == 0:
					exit_code = 1
					break
		if exit_code == 1:
			break
	print('YES' if exit_code == 0 else 'NO')


from sys import stdin
import math
input = stdin.readline

def dfs(i, j):
	if i < 0 or j < 0:
		return
	if visited[i][j] == False:
		if matrix[i][j] == '1':
			value[i][j] = 1
			visited[i][j] = True
			dfs(i - 1, j)
			dfs(i, j - 1)
		else:
			return
	else:
		return
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	matrix = []
	value = [[0 for x in range(n)] for y in range(n)]
	visited = [[False for x in range(n)] for y in range(n)]
	for i in range(n):
		matrix.append(list(input().strip()))
	for i in range(n):
		if matrix[i][n - 1] == '1':
			dfs(i, n - 1)
	for j in range(n):
		if matrix[n - 1][j] == '1':
			dfs(n - 1, j)
	flag = 1
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == '1' and value[i][j] == 0:
				flag = 0
				break
			if flag == 0:
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for z in range(n):
		s = input()
		l1 = []
		for i in s:
			l1.append(i)
		l.append(l1)
	val = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '0' and l[i][j + 1] == '0':
					val = 1
					break
		if val == 1:
			break
	if val == 1:
		print('NO')
	else:
		print('YES')


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	mat = []
	for i in range(n):
		mat.append(list(data()))
	r = True
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i + 1][j] == '0' and mat[i][j + 1] == '0':
					r = False
					break
		if not r:
			break
	out(('NO', 'YES')[r])


import math
t = int(input())
while t:
	t = t - 1
	n = int(input())
	l = []
	for w in range(n):
		l.append(input())
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and (l[i][j + 1] == '0' and l[i + 1][j] == '0'):
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


(z, zz, dgraphs) = (input, lambda : list(map(int, z().split())), {})
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	l = [z() for _ in range(n)]
	res = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and l[i + 1][j] == '0' and (l[i][j + 1] == '0'):
				res = 'NO'
				break
		if res == 'NO':
			break
	print(res)


for _ in range(int(input())):
	n = int(input())
	m = []
	for i in range(n):
		m.append(list(input()))
	work = True
	for i in range(n):
		for j in range(n):
			if i + 1 >= n or j + 1 >= n:
				continue
			if m[i][j] == '1':
				if m[i + 1][j] == '1' or m[i][j + 1] == '1':
					continue
				else:
					work = False
					break
	if work:
		print('YES')
	else:
		print('NO')


def check(A, j, k, n):
	if j == n - 1 or k == n - 1:
		return 1
	else:
		row = 1
		col = 1
		if A[j + 1][k] == '0':
			row = 0
		if A[j][k + 1] == '0':
			col = 0
		if row == 0 and col == 0:
			return 0
		else:
			return 1
T = int(input())
for i in range(0, T):
	n = int(input())
	A = []
	for j in range(0, n):
		X = str(input())
		A.append(X)
	flag = 1
	for j in range(0, n):
		if flag == 0:
			break
		for k in range(0, n):
			if flag == 0:
				break
			if A[j][k] == '1':
				ans = check(A, j, k, n)
				if ans == 0:
					flag = 0
					break
	if flag == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for test in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append([])
		for nn in input():
			mat[i].append(int(nn))
		mat[i].append(1)
	x = []
	for i in range(n):
		x.append(1)
	mat.append(x)
	f = 0
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if mat[i + 1][j] == 1 or mat[i][j + 1] == 1:
					continue
				else:
					f = 1
					break
	if f == 0:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	board = []
	for i in range(n):
		s = input().strip()
		board.append(s)
	BAD = False
	for i in range(n - 1):
		for j in range(n - 1):
			if board[i][j] == '1':
				if board[i][j + 1] == '0' and board[i + 1][j] == '0':
					BAD = True
	if BAD:
		print('NO')
	else:
		print('YES')


def chkCanon(n, matrix):
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1' and matrix[i + 1][j] == '0' and (matrix[i][j + 1] == '0'):
				return 0
	return 1
t = int(input())
for t_ in range(t):
	n = int(input())
	matrix = []
	for _ in range(n):
		row = list(input())
		matrix.append(row)
	if n == 1:
		print('YES')
	elif chkCanon(n, matrix):
		print('YES')
	else:
		print('NO')


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
for _ in range(inp()):
	n = inp()
	x = []
	for i in range(n):
		x.append([int(w) for w in input()])
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if x[i][j]:
				if x[i + 1][j] + x[i][j + 1] == 0:
					flag = 1
					break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	a = 0
	for j in range(n):
		s = str(input())
		matrix.append(s)
	for k in range(n):
		for l in range(n):
			if matrix[k][l] == '1':
				if k == n - 1 or l == n - 1:
					continue
				elif matrix[k + 1][l] == '1' or matrix[k][l + 1] == '1':
					continue
				else:
					a += 1
					break
	if a == 0:
		print('YES')
	else:
		print('NO')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	m = []
	for _ in range(n):
		m.append(list(sys.stdin.readline().strip()))
	judge1 = True
	for i in range(n):
		for j in range(n):
			if m[i][j] == '1' and i != n - 1 and (j != n - 1):
				if m[i + 1][j] == '0' and m[i][j + 1] == '0':
					judge1 = False
					break
	if judge1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [str(input()) for i in range(n)]
	f = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	n = N()
	grid = []
	for _ in range(n):
		grid.append(input())
	f = True
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == '1' and (not (grid[i + 1][j] == '1' or grid[i][j + 1] == '1')):
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	ar = []
	flag = True
	for i in range(n):
		xx = list(map(int, list(input())))
		ar.append(xx)
	for i in range(n - 1):
		for j in range(n - 1):
			if ar[i][j] == 1:
				if ar[i][j + 1] == 0 and ar[i + 1][j] == 0:
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import io
import os
import functools
import itertools
import math
t = int(input())
while t:
	t -= 1
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append([int(x) for x in input()])
	answer = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == 1 and matrix[i + 1][j] != 1 and (matrix[i][j + 1] != 1):
				answer = False
	print('YES' if answer else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	b = []
	for _ in range(n):
		b.append([x == '1' for x in input()])
	possible = True
	for i in range(n - 1):
		for j in range(n - 1):
			if b[i][j] and (not (b[i][j + 1] or b[i + 1][j])):
				possible = False
	if possible:
		print('YES')
	else:
		print('NO')


tests = int(input())
for i in range(tests):
	n = int(input())
	arr = []
	for j in range(n):
		arr.append(list(map(int, list(input()))))
	tr = True
	for k in range(n - 1):
		for l in range(n - 1):
			if arr[k][l] == 1:
				if arr[k + 1][l] == 0 and arr[k][l + 1] == 0:
					tr = False
	if tr:
		print('YES')
	else:
		print('NO')


t = int(input())
for test in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		temp = input()
		mat.append(list(temp))
	wrong = False
	for i in range(0, n - 1):
		if wrong == True:
			break
		for j in range(0, n - 1):
			if mat[i][j] == '1':
				if mat[i][j + 1] == '1' or mat[i + 1][j] == '1':
					continue
				else:
					wrong = True
					break
	if wrong == False:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	mat = []
	for i in range(n):
		mat.append(list(is1()))
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if not (mat[i + 1][j] == '1' or mat[i][j + 1] == '1'):
					flag = False
					break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list()
	for i in range(n):
		arr.append(input())
	right = list()
	bottom = list()
	isPossible = True
	if n == 1:
		print('YES')
		continue
	for i in range(n):
		right.append(list())
		bottom.append(list())
		for j in range(n):
			right[i].append(False)
			bottom[i].append(False)
	for i in range(n):
		if arr[n - 1][i] == '1':
			bottom[n - 1][i] = True
		if arr[i][n - 1] == '1':
			right[i][n - 1] = True
	for i in range(n - 2, -1, -1):
		for j in range(i, -1, -1):
			if arr[i][j] == '1':
				if bottom[i + 1][j] or right[i][j + 1]:
					right[i][j] = True
					bottom[i][j] = True
				else:
					isPossible = False
					break
			if arr[j][i] == '1':
				if bottom[j + 1][i] or right[j][i + 1]:
					right[j][i] = True
					bottom[j][i] = True
				else:
					isPossible = False
					break
	if isPossible:
		print('YES')
	else:
		print('NO')


import math
T = int(input())

def ans(n, arr):
	for i in range(n):
		for j in range(n):
			if arr[i][j] != '1':
				continue
			if i == n - 1 or j == n - 1:
				continue
			if not (arr[i + 1][j] == '1' or arr[i][j + 1] == '1'):
				return 'NO'
	return 'YES'
for t in range(T):
	n = int(input())
	arr = []
	for i in range(n):
		arr += [input()]
	print(ans(n, arr))


import sys, math, cmath, time
start_time = time.time()
global tt

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

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

def invr():
	return map(int, input().split())

def solve():
	n = inp()
	a = []
	for i in range(n):
		s = insr()
		a.append(s + '1')
	a.append('1' * (len(a) + 1))
	qw = 0
	for i in range(len(a) - 1):
		for j in range(len(a) - 1):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				qw = 1
				break
	if qw == 0:
		print('YES')
	else:
		print('NO')

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
	sys.stdout.close()

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


t = int(input())
for i in range(t):
	n = int(input())
	rows = []
	for i in range(n):
		inp = input()
		rows.append([int(inp[i]) for i in range(n)])
	possible = True
	for y in range(n - 1):
		for x in range(n - 1):
			if rows[y][x] == 1 and (not (rows[y + 1][x] == 1 or rows[y][x + 1] == 1)):
				possible = False
	if possible:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		s = input()
		for j in range(n):
			l[i][j] = s[j]
	flag = 0
	for i in range(n):
		for j in range(n):
			f = 0
			if l[i][j] == '1':
				if i < n - 1 and j < n - 1:
					if l[i][j + 1] == '1' or l[i + 1][j] == '1':
						f = 1
					if f == 0:
						flag = 1
						break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		grid = [input() for _ in range(N)]
		flag = True
		for i in range(N - 2, -1, -1):
			for j in range(i + 1):
				if grid[i][j] == '1':
					if grid[i + 1][j] == '0' and grid[i][j + 1] == '0':
						flag = False
						break
			if not flag:
				break
		for j in range(N - 2, -1, -1):
			for i in range(j + 1):
				if grid[i][j] == '1':
					if grid[i + 1][j] == '0' and grid[i][j + 1] == '0':
						flag = False
						break
			if not flag:
				break
		print('YES' if flag else 'NO')
main()


def possible(matrix, n):
	return all((not matrix[i][j] or (i == n - 1 or j == n - 1 or matrix[i + 1][j] or matrix[i][j + 1]) for i in range(n) for j in range(n)))

def solve():
	n = int(input())
	matrix = [list(map(int, input())) for _ in range(n)]
	print('YES' if possible(matrix, n) else 'NO')
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	S = []
	for i in range(n):
		s = input()
		s = list(s)
		S.append(s)
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if S[i][j] == '1':
				if S[i][j + 1] == '1' or S[i + 1][j] == '1':
					flag = True
				elif S[i][j + 1] == '0' or S[i + 1][j] == '0':
					flag = False
					break
		if flag == False:
			break
	if flag == False:
		print('NO')
	else:
		print('YES')


t = int(input())
for j in range(t):
	n = int(input())
	m = []
	for i in range(n):
		s = input()
		r = []
		for j in s:
			r.append(int(j))
		m.append(r)
	f = 0
	for i in range(n):
		if f == 1:
			break
		for j in range(n):
			if m[i][j] == 1:
				if i != n - 1 and j != n - 1:
					if m[i][j + 1] == 0 and m[i + 1][j] == 0:
						print('NO')
						f = 1
						break
	if f == 0:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	result = True
	for _ in range(n):
		a.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] == '0' and a[i + 1][j] == '0':
					result = False
	if result == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = []
	for i in range(n):
		s.append(input())
	f = 0
	for i in range(n):
		for j in range(len(s[0])):
			if s[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				if s[i + 1][j] == '1' or s[i][j + 1] == '1':
					continue
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


def solve(a, n):
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if i + 1 < n:
					if a[i + 1][j] == 1:
						a[i][j] = 0
				else:
					a[i][j] = 0
				if j + 1 < n:
					if a[i][j + 1] == 1:
						a[i][j] = 0
				else:
					a[i][j] = 0
		if 1 in a[i]:
			return False
	return True

def main():
	t = int(input())
	ans = [0] * t
	for test in range(t):
		n = int(input())
		a = [[int(j) for j in input()] for i in range(n)]
		if solve(a, n):
			ans[test] = 'YES'
		else:
			ans[test] = 'NO'
	print('\n'.join(ans))
main()


def get_data():
	n = int(input())
	data = []
	for i in range(n):
		temp = list(input('').strip())
		data.append(temp)
	return data

def is_valid(data):
	n = len(data)
	dir = [(0, 1), (1, 0)]
	for i in range(n):
		for j in range(n):
			if data[i][j] == '1':
				found = False
				for d in dir:
					n_i = i + d[0]
					n_j = j + d[1]
					if n_i == n or n_j == n:
						found = True
						break
					if data[n_i][n_j] == '1':
						found = True
						break
				if not found:
					return 'NO'
	return 'YES'
num = int(input(''))
for i in range(num):
	print(is_valid(get_data()))


for _ in range(int(input())):
	size = int(input())
	grid = [input() for _ in range(size)]
	ones = [(y, x) for y in range(size) for x in range(size) if grid[y][x] == '1']
	for (y, x) in ones:
		if not (y == size - 1 or x == size - 1 or grid[y][x + 1] == '1' or (grid[y + 1][x] == '1')):
			print('NO')
			break
	else:
		print('YES')


def f(A, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if A[i][j] == '1' and A[i + 1][j] == '0' and (A[i][j + 1] == '0'):
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	A = []
	for j in range(n):
		A.append(input())
	print(f(A, n))


T = int(input())
for ti in range(T):
	ans = True
	n = int(input())
	pattern = []
	for ri in range(n):
		ip = [int(x) for x in list(input())]
		pattern.append(ip)
	for ri in range(n - 1):
		if not ans:
			break
		for ci in range(n - 1):
			if pattern[ri][ci] == 1 and pattern[ri][ci + 1] == 0 and (pattern[ri + 1][ci] == 0):
				ans = False
				break
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	l = []
	for i in range(n):
		k = input()
		l.append(k)
	ans = 'YES'
	g = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j + 1] == '0' and l[i + 1][j] == '0':
					ans = 'NO'
					g = 0
					break
		if g == 0:
			break
	print(ans)


t = int(input())
s = []
for i in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	s.append(l)
	l.append(n)

def f(s):
	for i in range(s[-1] - 1):
		for j in range(s[-1] - 1):
			if s[i][j] == '1':
				if s[i][j + 1] == '1' or s[i + 1][j] == '1':
					pass
				else:
					return False
			else:
				pass
	return True
for i in range(t):
	answer = True
	for j in range(s[i][-1]):
		answer = answer and f(s[i])
	if answer:
		print('Yes')
	else:
		print('No')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	mat = [input() for i in range(n)]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1' and mat[i + 1][j] == '0' and (mat[i][j + 1] == '0'):
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


def c(m, cell):
	if m[cell[0]][cell[1]] == 0:
		return True
	if m[cell[0]][cell[1] + 1] == 1 or m[cell[0] + 1][cell[1]] == 1:
		return True
	return False

def cc(m, i):
	for j in range(i, -1, -1):
		if c(m, [j, i]) == False or c(m, [i, j]) == False:
			return False
	return True

def f(m):
	a = True
	for i in range(len(m) - 2, -1, -1):
		if cc(m, i) == False:
			a = False
	if a:
		return 'YES'
	else:
		return 'NO'
for tc in range(int(input())):
	temp = int(input())
	mat = []
	for k in range(temp):
		ll = list(input())
		for i in range(len(ll)):
			ll[i] = int(ll[i])
		mat.append(ll)
	print(f(mat))


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

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

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
for _ in range(ii()):
	n = ii()
	p = [list(input()) for i in range(n)]
	for i in range(n - 1):
		f = False
		for j in range(n - 1):
			if p[i][j] == '1' and p[i + 1][j] == '0' and (p[i][j + 1] == '0'):
				print('NO')
				f = True
				break
		if f:
			break
	else:
		print('YES')


dy = [0, -1]
dx = [-1, 0]

def bfs():
	global mat, target, n
	cnt = 0
	q = []
	visited = [[0] * n for _ in range(n)]
	for i in range(n):
		if mat[n - 1][i]:
			visited[n - 1][i] = 1
			q.append((n - 1, i))
			cnt += 1
		if mat[i][n - 1] and i != n - 1:
			visited[i][n - 1] = 1
			q.append((i, n - 1))
			cnt += 1
	while q:
		(y, x) = q.pop(0)
		for k in range(2):
			(ny, nx) = (y + dy[k], x + dx[k])
			if not (0 <= ny < n and 0 <= nx < n):
				continue
			if visited[ny][nx]:
				continue
			if mat[ny][nx] == 0:
				continue
			q.append((ny, nx))
			cnt += 1
			visited[ny][nx] = 1
	return 'YES' if cnt == target else 'NO'
t = int(input())
for i in range(t):
	n = int(input())
	mat = []
	for _ in range(n):
		temp = list(map(lambda x: int(x), list(input())))
		mat.append(temp)
	target = sum(sum(mat, []))
	res = bfs()
	print(res)


t = int(input())
while t:
	n = int(input())
	a = [list(input()) for x in range(n)]
	f = 0
	for x in range(n - 1):
		for y in range(n - 1):
			if a[x][y] == '1' and a[x][y + 1] == '0' and (a[x + 1][y] == '0'):
				f = 1
				break
		if f:
			break
	if f:
		print('NO')
	else:
		print('YES')
	t -= 1


t = int(input())
for ii in range(t):
	n = int(input())
	a = [[0] * n for i in range(n)]
	for i in range(n):
		s = input().rstrip()
		for j in range(n):
			a[i][j] = int(s[j])
	flag = 0
	for i in range(n):
		for j in range(n):
			local = 0
			if i == n - 1 or j == n - 1:
				continue
			if a[i][j] == 1:
				if i + 1 < n:
					if a[i + 1][j] == 1:
						local = 1
				if j + 1 < n:
					if a[i][j + 1] == 1:
						local = 1
				if local != 1:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	n = int(input())
	matrix = []
	for i in range(n):
		c = input()
		matrix.append(c)
	flag = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1' and matrix[i][j + 1] == '0' and (matrix[i + 1][j] == '0'):
				flag = 0
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')
	t -= 1


from sys import stdin
input = stdin.buffer.readline
t = int(input())

def solve():
	n = int(input())
	mat = []
	for _ in range(n):
		mat.append(list(map(int, input().decode().strip())))
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == 1 and mat[i + 1][j] != 1 and (mat[i][j + 1] != 1):
				return False
	return True
for _ in range(t):
	print('yes' if solve() else 'no')


def is_poss():
	for (i, row) in enumerate(mtrx):
		for (j, x) in enumerate(row):
			if x:
				if not (j == n - 1 or i == n - 1 or mtrx[i + 1][j] or mtrx[i][j + 1]):
					return False
	return True
for _ in range(int(input())):
	n = int(input())
	mtrx = []
	for _ in range(n):
		mtrx.append(list(map(int, list(input()))))
	print('YES' if is_poss() else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = [list(str(input())) for _ in range(n)]
	A = [a + ['1'] for a in A]
	A.append(['1'] * (n + 1))
	flag = True
	for i in range(n):
		for j in range(n):
			if A[i][j] == '1':
				if A[i][j + 1] == '1' or A[i + 1][j] == '1':
					continue
				else:
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for j in range(n):
		s = input()
		matrix.append(s)
	failflag = 0
	for x in range(n):
		for y in range(n):
			if matrix[x][y] == '1' and x < n - 1 and (y < n - 1):
				if matrix[x + 1][y] == '0' and matrix[x][y + 1] == '0':
					failflag = 1
	if failflag == 0:
		print('YES')
	else:
		print('NO')


from collections import deque

def point_possible(i, j):
	if 0 <= i < n and 0 <= j < n:
		return True
	return False

def safe(i, j, n, sf):
	if sf[i][j]:
		return True
	if i == n - 1 or j == n - 1:
		return True
	return False

def solve(ls, n):
	rowmov = [1, -1, 0, 0]
	colmov = [0, 0, 1, -1]
	if n == 1:
		return True
	visited = [[False for i in range(n)] for j in range(n)]
	que = deque()
	sf = [[False for i in range(n)] for j in range(n)]
	for x_coor in range(n):
		for y_coor in range(n):
			if ls[x_coor][y_coor]:
				que.append([x_coor, y_coor])
			while len(que):
				(i, j) = que.popleft()
				if visited[i][j]:
					continue
				visited[i][j] = True
				if safe(i, j, n, sf):
					for k in range(4):
						x = i + rowmov[k]
						y = i + colmov[k]
						if point_possible(x, y) and ls[x][y]:
							que.append([x, y])
				else:
					if point_possible(i + 1, j) and ls[i + 1][j]:
						que.append([i + 1, j])
						sf[i][j] = True
					if point_possible(i, j + 1) and ls[i][j + 1]:
						que.append([i, j + 1])
						sf[i][j] = True
					if point_possible(i - 1, j) and ls[i - 1][j]:
						que.append([i - 1, j])
					if point_possible(i, j - 1) and ls[i][j - 1]:
						que.append([i, j - 1])
					if not sf[i][j]:
						return False
	return True
for _ in range(int(input())):
	n = int(input())
	ls = []
	for i in range(n):
		ls.append(list(map(int, input())))
	if solve(ls, n):
		print('YES')
	else:
		print('NO')


import math
tc = int(input())
for _ in range(tc):
	num = int(input())
	arr = []
	for _ in range(num):
		arr.append(list(map(int, list(input().strip()))))
	ans = False
	for i in range(num):
		for j in range(num):
			poss = False
			touch = False
			if arr[i][j] == 0:
				continue
			if i + 1 < num and j + 1 < num:
				touch = True
			if i + 1 < num:
				if arr[i + 1][j] == 1:
					poss = poss or True
			if j + 1 < num:
				if arr[i][j + 1] == 1:
					poss = poss or True
			if touch is True and poss is False:
				print('NO')
				ans = True
			if ans is True:
				break
		if ans is True:
			break
	if ans is False:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	check = []
	for i in range(n):
		check.append([True] * n)
	hits = []
	for i in range(n):
		hits.append(input())
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			if hits[i][j] == '1':
				if hits[i + 1][j] == '1' or hits[i][j + 1] == '1':
					continue
				flag = True
				break
		if flag:
			print('NO')
			break
	if not flag:
		print('YES')


import sys
input = sys.stdin.readline
t = int(input())
for q in range(t):
	n = int(input())
	square = []
	flag = False
	for x in range(n):
		square.append(input())
	for x in range(n - 1):
		for y in range(n - 1):
			if square[x][y] == '1':
				if square[x + 1][y] == '0' and square[x][y + 1] == '0':
					flag = True
					break
			else:
				continue
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	al = []
	t -= 1
	n = int(input())
	while n:
		n -= 1
		l = list(input())
		al.append(l)
	r = 'YES'
	rang = len(al)
	for i in range(rang):
		for j in range(rang):
			if al[i][j] == '1':
				if i < rang - 1 and j < rang - 1:
					if al[i + 1][j] == '0' and al[i][j + 1] == '0':
						r = 'NO'
						break
	print(r)


n_data = int(input())
datas = []
for i in range(n_data):
	l_array = int(input())
	tab = []
	for i in range(l_array):
		row = input()
		r = []
		for c in row:
			r.append(c)
		tab.append(r)
	datas.append(tab)

def is_border(x, y, grid):
	if x == len(grid[0]) - 1:
		return True
	if y == len(grid) - 1:
		return True
	return False

def possible(x, y, grid):
	if is_border(x, y, grid):
		return True
	if grid[x + 1][y] == '1':
		return True
	if grid[x][y + 1] == '1':
		return True
	return False

def grid_OK(grid):
	for y in range(len(grid)):
		for x in range(len(grid[0])):
			if grid[x][y] == '0':
				continue
			if possible(x, y, grid):
				continue
			return 'NO'
	return 'YES'
for d in datas:
	print(grid_OK(d))


t = int(input())
ans = []
for g in range(t):
	n = int(input())
	a = [input() for i in range(n)]
	f = 0
	for i in range(n):
		for j in range(n):
			if int(a[i][j]) == 1:
				if i < n - 1 and j < n - 1:
					if not int(a[i + 1][j]) + int(a[i][j + 1]):
						ans += ['NO']
						f = 1
						break
		if f == 1:
			break
	else:
		ans += ['YES']
for i in ans:
	print(i)


from sys import stdin
a = int(stdin.readline())
for b in range(0, a):
	A = int(stdin.readline())
	K = 0
	if A == 1:
		stdin.readline()
		print('YES')
		continue
	AA = set()
	for k in range(0, A):
		b = stdin.readline()
		for t in AA:
			if b[t] == '0':
				K = 1
				print('NO')
				break
		if K == 1:
			for p in range(k + 1, A):
				stdin.readline()
			break
		AA.clear()
		for j in range(0, A):
			if (b[j] == '1' and b[j + 1] == '0') and j != str(A - 1):
				AA.add(j)
	if K == 0:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(list(map(int, input())))
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if ar[i][j] == 1:
				flag = 0
				if 0 <= i + 1 <= n - 1 and ar[i + 1][j] == 1:
					flag = 1
				elif 0 <= j + 1 <= n - 1 and ar[i][j + 1] == 1:
					flag = 1
				if flag == 0:
					ans = 'NO'
					break
	print(ans)


n_array = []
matrix_array = []
for _ in range(int(input())):
	n_array.append(int(input()))
	matrix = []
	for _ in range(n_array[-1]):
		row = str(input())
		row = [bool(int(i)) for i in row]
		matrix.append(row)
	matrix_array.append(matrix)
for (num, n) in enumerate(n_array):
	answer = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix_array[num][i][j] == True:
				if not (matrix_array[num][i + 1][j] == True or matrix_array[num][i][j + 1] == True):
					answer = 'NO'
					break
	print(answer)


import math
for _ in range(int(input())):
	n = int(input())
	inp = []
	for i in range(n):
		inp.append(input())
	isvalid = True
	for i in range(n - 1):
		for j in range(n - 1):
			if inp[i][j] == '1':
				if inp[i][j + 1] == '0' and inp[i + 1][j] == '0':
					isvalid = False
					break
	if isvalid:
		print('YES')
	else:
		print('NO')


import sys
import collections as cc
I = lambda : list(map(int, input().split()))
input = sys.stdin.readline
for tc in range(int(input())):
	a = []
	(n,) = I()
	tf = 1
	for i in range(n):
		a.append(list(input()))
	for i in range(n - 1):
		tf = 1
		for j in range(n - 1):
			if a[i][j] == '1':
				x = i + 1
				y = j + 1
				f = 0
				if x < n:
					if a[x][j] == '1':
						f = 1
				if y < n:
					if a[i][y] == '1':
						f = 1
				if not f:
					tf = 0
					break
		if not tf:
			break
	if tf:
		print('YES')
	else:
		print('NO')


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = int(input())
for _ in range(t):
	n = int(input())
	grid = []
	for _ in range(n):
		grid.append(input())
	poss = True
	for i in range(n):
		for j in range(n):
			if grid[i][j] == '1' and i != n - 1 and (j != n - 1) and (grid[i + 1][j] != '1') and (grid[i][j + 1] != '1'):
				poss = False
				break
	if poss:
		print('YES')
	else:
		print('NO')


def poligono(matrice):
	for i in range(len(matrice) - 1):
		for j in range(len(matrice[0]) - 1):
			if matrice[i][j] == 1:
				if matrice[i + 1][j] == 0 and matrice[i][j + 1] == 0:
					print('NO')
					return
	print('YES')
T = int(input())
for k in range(T):
	N = int(input())
	m = [[0] * N for i in range(N)]
	for i in range(N):
		m[i] = [int(x) for x in ' '.join(input().split())]
	poligono(m)


for i in range(int(input())):
	n = int(input())
	m = [input() for _ in range(n)]
	ans = 'YES'
	for r in range(n - 2, -1, -1):
		for c in range(n - 2, -1, -1):
			if m[r][c] == '1':
				if m[r + 1][c] == '0' and m[r][c + 1] == '0':
					ans = 'NO'
					break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		s = input()
		l.append(s)
	k = 0
	for i in range(n - 1):
		for y in range(n - 1):
			if l[i][y] == '1':
				if l[i + 1][y] == '1' or l[i][y + 1] == '1':
					continue
				else:
					k = 1
	if k == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	flag = 0
	res = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1' and mat[i + 1][j] == '0' and (mat[i][j + 1] == '0'):
				res = 'NO'
				break
		if res == 'NO':
			break
	print(res)


def main():
	n = int(input())
	a = []
	for i in range(n):
		b = input()
		a.append(b)
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				try:
					if a[i][j + 1] == '0' and a[i + 1][j] == '0':
						print('NO')
						return 0
				except:
					pass
	print('YES')
a = int(input())
for i in range(a):
	main()


for i in range(int(input())):
	n = int(input())
	a = []
	c = 'YES'
	for i in range(n):
		s = input()
		a.append(s)
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					c = 'NO'
					break
		if c == 'NO':
			break
	print(c)


for zx in range(int(input())):
	(n, a, fl) = (int(input()), [], 1)
	for i in range(n):
		a.append(list(str(input())))
	for i in range(n):
		for j in range(n):
			if i == n - 1 or j == n - 1:
				continue
			elif a[i][j] == '1':
				if a[i][j + 1] == '0' and a[i + 1][j] == '0':
					fl = 0
					break
	print('NO') if fl == 0 else print('YES')


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		m = []
		for _ in range(n):
			m.append(input())
		flag = 0
		for i in range(n):
			for j in range(n):
				if m[i][j] == '1':
					if i != n - 1 and j != n - 1:
						if m[i + 1][j] == '0' and m[i][j + 1] == '0':
							flag = 1
							break
			if flag == 1:
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')
main()


for _ in range(int(input())):
	n = int(input())
	grid = []
	for i in range(n):
		grid.append(list(input()))
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == '1':
				if grid[i + 1][j] != '1' and grid[i][j + 1] != '1':
					ans = 'NO'
					break
		if ans == 'NO':
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [list(map(int, input())) for _ in range(n)]
	a.append([1] * n)
	for i in range(n):
		a[i].append(1)
	f = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if a[i + 1][j] == 0 and a[i][j + 1] == 0:
					f = False
					break
		if not f:
			break
	print('YES' if f else 'NO')


def main():
	for tc in range(int(input())):
		n = int(input())
		a = ['0'] * (n + 1)
		for i in range(n):
			a[i] = input()

		def ok(i, j):
			if i + 1 < n and a[i + 1][j] == '1':
				return True
			if i + 1 == n:
				return True
			if j + 1 < n and a[i][j + 1] == '1':
				return True
			if j + 1 == n:
				return True
			return False
		f = True
		for i in range(n):
			for j in range(n):
				if a[i][j] == '1' and (not ok(i, j)):
					f = False
		if f:
			print('YES')
		else:
			print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	l = [input() for i in range(n)]
	f = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1' and (i != n - 1 and j != n - 1):
				if l[i + 1][j] != '1' and l[i][j + 1] != '1':
					f = 1
					break
		if f == 1:
			break
	if f == 1:
		print('No')
	else:
		print('Yes')


import sys
try:
	sys.stdin = open('Input.txt', 'r')
	sys.stdout = open('Output.txt', 'w')
except:
	pass
for testCases in range(int(input())):
	n = int(input())
	mat = []
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		mat.append(list(map(int, list(input()))))
	for i in range(n):
		for j in range(n):
			if i == n - 1:
				if mat[i][j] == 1:
					ans[i][j] = 1
			elif mat[i][j] == 1 and mat[i + 1][j] == 1:
				ans[i][j] = 1
	for j in range(n):
		for i in range(n):
			if j == n - 1:
				if mat[i][j] == 1:
					ans[i][j] = 1
			elif mat[i][j] == 1 and mat[i][j + 1]:
				ans[i][j] = 1
	flag = False
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1 and ans[i][j] != 1:
				print('NO')
				flag = True
				break
		if flag:
			break
	if flag != True:
		print('YES')


def okay(s, n):
	for sij in range(2 * n - 2, -1, -1):
		ijl = [(i, sij - i) for i in range(n) if sij >= i and sij <= i + (n - 1)]
		for (i, j) in ijl:
			if i < n - 1 and j < n - 1 and (s[i][j] > s[i + 1][j] + s[i][j + 1]):
				return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	s = [[int(c) for c in input()] for i in range(n)]
	print('YES' if okay(s, n) else 'NO')


for i in ' ' * int(input()):
	a = []
	for j in ' ' * int(input()):
		a.append(list(input()))
	count = 0
	for f in range(len(a)):
		if count == 1:
			break
		for j in range(len(a[0])):
			if a[f][j] == '1':
				if f + 1 < len(a) and j + 1 < len(a[0]):
					if a[f + 1][j] == '0' and a[f][j + 1] == '0':
						count = 1
						break
	if count == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	t -= 1
	n = int(input())
	grid = []
	for i in range(n):
		sss = input()
		aaa = []
		for j in sss:
			if j == '1':
				aaa.append(1)
			else:
				aaa.append(0)
		grid.append(aaa)
	temp = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == 1:
				if grid[i][j + 1] == 0 and grid[i + 1][j] == 0:
					print('NO')
					temp = 1
					break
		if temp == 1:
			break
	if temp == 0:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	d = {}
	for i in range(1, n + 1):
		s = input()
		j = 1
		while j <= n:
			x = s[j - 1]
			d[i, j] = x
			j = j + 1
	f = 0
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			x = d[i, j]
			if x == '1':
				if i == n or j == n:
					continue
				else:
					y1 = d[i + 1, j]
					y2 = d[i, j + 1]
					if y1 == '1' or y2 == '1':
						continue
					else:
						f = 1
			if f == 1:
				break
	if f == 1:
		print('NO')
	else:
		print('YES')


def check(a, n):
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i + 1 == n or j + 1 == n:
					pass
				elif a[i + 1][j] == '1' or (j + 1 < n and a[i][j + 1] == '1'):
					pass
				else:
					return False
	return True
for _ in range(int(input())):
	n = int(input())
	a = [input() for i in range(n)]
	print(['NO', 'YES'][check(a, n)])


for t in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l1 = list(input())
		l.append(l1)
	ans = 'YES'
	for i in range(n):
		flag = True
		for j in range(n):
			if l[i][j] == '1':
				if i == n - 1 and j == n - 1:
					pass
				elif i == n - 1:
					pass
				elif j == n - 1:
					pass
				elif l[i][j + 1] == '0' and l[i + 1][j] == '0':
					flag = False
		if flag == False:
			ans = 'NO'
	print(ans)


for xyz in range(0, int(input())):
	n = int(input())
	l = []
	for i in range(0, n):
		s = input()
		s += '1'
		l.append(s)
	s = '1' * (n + 1)
	l.append(s)
	f = True
	for i in range(0, n):
		for j in range(0, n):
			if l[i][j] == '1':
				if l[i][j + 1] == '0':
					if l[i + 1][j] == '0':
						f = False
						break
		if f == False:
			break
	if f:
		print('YES')
	else:
		print('NO')


t = int(input())
sLst = []
for i in range(t):
	n = int(input())
	lst = []
	for j in range(n):
		lst.append(list(input()))
	flag = 0
	for j in range(n - 1):
		for k in range(n - 1):
			if lst[j][k] == '1':
				if lst[j + 1][k] == '0' and lst[j][k + 1] == '0':
					sLst.append('NO')
					flag = 1
					break
		if flag == 1:
			break
	else:
		sLst.append('YES')
for i in sLst:
	print(i)


t = int(input())
while t:
	n = int(input())
	l1 = []
	for i in range(0, n):
		x = input()
		l2 = []
		for i in x:
			y = int(i)
			l2.append(y)
		l1.append(l2)
	value = ''
	for j in range(0, n):
		for k in range(0, n):
			if l1[j][k] == 1 and j < n - 1 and (k < n - 1):
				if l1[j + 1][k] == 0 and l1[j][k + 1] == 0:
					value = 'NO'
	if value == '':
		print('YES')
	else:
		print('NO')
	t = t - 1


def valid_training(n, a):
	for i in range(n):
		for j in range(n):
			if a[i][j] == 0:
				continue
			if j + 1 == n:
				continue
			if i + 1 == n:
				continue
			if a[i + 1][j] == 1:
				continue
			if a[i][j + 1] == 1:
				continue
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	a = []
	for _ in range(n):
		a.append([int(d) for d in input()])
	print('YES' if valid_training(n, a) else 'NO')


def f(i, j, a):
	if i == n - 1 or j == n - 1:
		return True
	elif a[i][j + 1] == 1:
		return True
	elif a[i + 1][j] == 1:
		return True
	return False
t = int(input())
for w in range(t):
	n = int(input())
	got = True
	a = []
	for i in range(n):
		rows = []
		row = str(input())
		for i in row:
			rows.append(int(i))
		a.append(rows)
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if not f(i, j, a):
					print('NO')
					got = False
					break
		if not got:
			break
	if got:
		print('YES')


def cannon(arr):
	flag = True
	if any((arr[i][j] == '1' and arr[i + 1][j] != '1' and (arr[i][j + 1] != '1') for i in range(0, len(arr) - 1) for j in range(0, len(arr) - 1))):
		flag = False
	if any((arr[i][j] == '1' and arr[i][j + 1] != '1' and (arr[i + 1][j] != '1') for i in range(0, len(arr) - 1) for j in range(0, len(arr) - 1))):
		flag = False
	if flag:
		return 'YES'
	else:
		return 'NO'
s = int(input())
for t_itr in range(0, s):
	t = int(input())
	blanck = []
	for i in range(0, t):
		blanck.append(list(map(str, input().strip().split())))
		lst = sum(blanck, [])
	print(cannon(lst))


t = int(input())
for tt in range(t):
	row = int(input())
	l = []
	for i in range(row):
		string = input()
		a = list(string)
		l.append(a)
	col = row
	flag = 0
	for i in range(row):
		for j in range(col):
			if l[i][j] == '0':
				continue
			if l[i][j] == '1' and ((i == row - 1 or j == row - 1) or (l[i][j + 1] == '1' or l[i + 1][j] == '1')):
				continue
			flag = 1
			break
		if flag == 1:
			print('NO')
			break
	if flag == 0:
		print('YES')


def main_function():
	n = int(input())
	memo = [input() for i in range(n)]
	ans = True
	for i in range(n):
		for j in range(n):
			if memo[i][j] == '1':
				if not (i + 1 == n or j + 1 == n or memo[i + 1][j] == '1' or (memo[i][j + 1] == '1')):
					ans = False
					break
	if ans:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	main_function()


def polygon(list1, n):
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if list1[i][j] == '1' and list1[i + 1][j] == '0' and (list1[i][j + 1] == '0'):
				ans = 'NO'
	return ans
t = int(input())
for i in range(t):
	list1 = []
	n = int(input())
	for j in range(n):
		list1.append(list(input()))
	print(polygon(list1, n))


import sys
mass = []
mass2 = []

def check(x, y):
	global mass2
	if x > 0:
		if mass[x][y] == '1':
			mass2[x - 1][y] = 1
			check(x - 1, y)
	if y > 0:
		if mass[x][y] == '1':
			mass2[x][y - 1] = 1
			check(x, y - 1)
	return

def check2():
	for k in range(n - 1):
		for j in range(n - 1):
			flag = 2
			try:
				if int(mass[k][j]) > int(mass[k + 1][j]):
					flag -= 1
			except IndexError:
				pass
			try:
				if int(mass[k][j]) > int(mass[k][j + 1]):
					flag -= 1
			except IndexError:
				pass
			if flag == 0:
				print('NO')
				return 0
	return 1
q = int(input())
for i in range(q):
	n = int(sys.stdin.readline())
	mass = []
	mass2 = []
	for j in range(n):
		if j < n - 1:
			ch = [0] * (n - 1)
			ch.append(1)
		else:
			ch = [1] * n
		a = sys.stdin.readline()
		mass.append(a)
		mass2.append(ch)
	if check2() == 1:
		print('YES')


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = []
	for i in range(n):
		a.append([int(i) for i in stdin.readline().strip()])
	if n == 1:
		print('YES')
		continue
	else:
		chk = True
		for i in range(n - 1):
			if chk:
				for j in range(n - 1):
					if a[i][j] == 1:
						if a[i + 1][j] == 0 and a[i][j + 1] == 0:
							chk = False
							break
			else:
				break
		if chk:
			print('YES')
		else:
			print('NO')


import sys
import math

def I():
	return int(sys.stdin.readline())

def IL():
	return list(map(int, sys.stdin.readline().strip().split()))

def ILS():
	return list(map(str, sys.stdin.readline().strip().split()))
t = I()
for iii in range(t):
	n = I()
	l = []
	for ii in range(n):
		s = input()
		nl = [int(s[i]) for i in range(len(s))]
		l.append(nl)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == 1:
				if l[i + 1][j] == 1 or l[i][j + 1] == 1:
					continue
				else:
					flag = 1
					break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def solve():
	n = int(input())
	mat = []
	for _ in range(n):
		mat.append(list(input()))
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i][j + 1] == '1' or mat[i + 1][j] == '1':
					pass
				else:
					print('NO')
					return
	print('YES')
t = int(input())
for _ in range(t):
	solve()


import math
t = int(input())
for u in range(t):
	n = int(input())
	deff = 0
	a = [[0] * n for i in range(n)]
	for i in range(n):
		k = list(map(int, list(input())))
		for j in range(n):
			a[i][j] = k[j]
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i][j + 1] == 1 or a[i + 1][j] == 1:
					pass
				else:
					deff = 1
	if deff == 1:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		l = input()
		l = [int(i) for i in l]
		mat.append(l)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == 1:
				if mat[i][j + 1] == 1 or mat[i + 1][j] == 1:
					continue
				else:
					flag = 1
					break
	if flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	ok = True
	for _ in range(n):
		tmp = input()
		arr.append(tmp)
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1' and arr[i][j + 1] == '0' and (arr[i + 1][j] == '0'):
				ok = False
		if not ok:
			break
	print('YES' if ok else 'NO')


import math
t = int(input())
for _ in range(t):
	n = int(input())
	vec = []
	for i in range(n):
		s = input()
		vc = []
		for j in range(n):
			vc.append(int(s[j]))
		vec.append(vc)
	ans = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if vec[i][j] == 1 and vec[i][j + 1] == 0 and (vec[i + 1][j] == 0):
				ans = 0
				break
		if ans == 0:
			break
	if ans == 1:
		print('YES')
	else:
		print('NO')


n_tests = int(input())
for _ in range(n_tests):
	polygon_size = int(input())
	polygon_matrix = []
	for _ in range(polygon_size):
		row = list(map(int, input()))
		polygon_matrix.append(row)
	check_sequence = 1
	for i in range(polygon_size - 1):
		for j in range(polygon_size - 1):
			if polygon_matrix[i][j] == 1:
				if polygon_matrix[i][j + 1] == 0 and polygon_matrix[i + 1][j] == 0:
					check_sequence = 0
					break
	if check_sequence == 0:
		print('NO')
	else:
		print('YES')


def main_function():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	print = stdout.write
	t = int(input())
	for _ in range(t):
		n = int(input())
		memo = [input()[:-1] for i in range(n)]
		ans = True
		for i in range(n):
			for j in range(n):
				if memo[i][j] == '1':
					if not (i + 1 == n or j + 1 == n or memo[i + 1][j] == '1' or (memo[i][j + 1] == '1')):
						ans = False
						break
		if ans:
			print('YES\n')
		else:
			print('NO\n')
main_function()


from sys import stdin, stdout, setrecursionlimit
import threading

class Tail_Recursion_Optimization:

	def __init__(self, RECURSION_LIMIT, STACK_SIZE):
		setrecursionlimit(RECURSION_LIMIT)
		threading.stack_size(STACK_SIZE)
		return None

class SOLVE:

	def DP(self, x, y, n, node, dp):
		if dp[x][y] != None:
			return dp[x][y]
		if x == y == n - 1:
			dp[x][y] = int(node[x][y])
		elif x == n - 1:
			self.DP(x, y + 1, n, node, dp)
			dp[x][y] = int(node[x][y])
		elif y == n - 1:
			self.DP(x + 1, y, n, node, dp)
			dp[x][y] = int(node[x][y])
		else:
			val1 = self.DP(x, y + 1, n, node, dp)
			val2 = self.DP(x + 1, y, n, node, dp)
			dp[x][y] = 0 if node[x][y] == '0' else max(val1, val2)
		return dp[x][y]

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			n = int(R())
			node = []
			for j in range(n):
				node.append(R())
			dp = [[None for k in range(n)] for j in range(n)]
			self.DP(0, 0, n, node, dp)
			ret = 1
			for j in range(n):
				for k in range(n):
					if node[j][k] == '1' and (not dp[j][k]):
						ret = 0
						break
				if not ret:
					break
			ans.append(['NO', 'YES'][ret])
		W('\n'.join(ans))
		return 0

def main():
	s = SOLVE()
	s.solve()
Tail_Recursion_Optimization(10 ** 7, 2 ** 26)
threading.Thread(target=main).start()


for q in range(int(input())):
	n = int(input())
	arr = []
	flag = 0
	for i in range(n):
		arr.append(list(input()))
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
					flag = 1
					break
			if flag == 1:
				break
		if flag == 1:
			print('NO')
			break
	if flag != 1:
		print('YES')


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def solve(matrix, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1' and matrix[i + 1][j] == '0' and (matrix[i][j + 1] == '0'):
				return 'No'
	return 'Yes'

def main():
	t = inp()
	for _ in range(t):
		n = inp()
		matrix = [insr() for _ in range(n)]
		ans = solve(matrix, n)
		print(ans)
main()


for i in range(int(input())):
	n = int(input())
	matrix = []
	for j in range(n):
		l1 = list(input())
		matrix.append(l1)
	if n == 1:
		print('YES')
	else:
		ans = 'YES'
		for j in range(n - 1):
			for k in range(n - 1):
				if matrix[j][k] == '1':
					if matrix[j][k + 1] == '0' and matrix[j + 1][k] == '0':
						ans = 'NO'
						break
		print(ans)


import math
el = int(input())
for k in range(el):
	num = int(input())
	ar = []
	for i in range(num):
		row = []
		st = input()
		ar.append([x for x in st])
	ch = 0
	for i in range(len(ar) - 1):
		for j in range(len(ar[i]) - 1):
			if ar[i][j] == '0':
				continue
			if ar[i][j + 1] == '1':
				continue
			if ar[i + 1][j] == '1':
				continue
			ch = 1
			break
		if ch == 1:
			break
	if ch == 1:
		print('NO')
	else:
		print('YES')


def canon(n, l):
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == 1 and l[i + 1][j] != 1 and (l[i][j + 1] != 1):
				return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		l.append([int(x) for x in list(input())])
	print(canon(n, l))


import os
import sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		a = [input() for _ in range(n)]
		fl = True
		for i in range(n - 1):
			for j in range(n - 1):
				if a[i][j] == '0' or a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				fl = False
		print('YES' if fl else 'NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	grid = []
	for i in range(n):
		a = list(input())
		grid.append(a)
	ans = 1
	for i in range(n):
		for j in range(n):
			if grid[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				if grid[i][j + 1] != '1' and grid[i + 1][j] != '1':
					ans = -1
					break
	if ans == 1:
		print('YES')
	else:
		print('NO')


from collections import deque
dd = [(1, 0), (0, 1)]

def dcheck(m, i, j):
	L = len(m)
	q = deque()
	q.append((i, j))
	while q:
		(x, y) = q.pop()
		if x == L - 1 or y == L - 1 or cmap[x][y] == 1:
			cmap[x][y] = 1
			return True
		for ddd in dd:
			u = x + ddd[0]
			v = y + ddd[1]
			if not (0 <= u <= L - 1 and 0 <= v <= L - 1):
				continue
			if int(m[u][v]) == 0:
				continue
			q.append((u, v))
	return False

def check(m):
	global cmap
	cmap = [[0 for _ in range(len(m))] for _ in range(len(m))]
	for i in reversed(range(len(m))):
		for j in reversed(range(len(m))):
			if int(m[i][j]) == 1:
				if dcheck(m, i, j):
					continue
				print('NO')
				return
	print('YES')
	return
for i in range(int(input())):
	n = int(input())
	m = [[] for _ in range(n)]
	for k in range(n):
		aa = input()
		for j in range(len(aa)):
			m[k].append(aa[j])
	check(m)


from pprint import pprint
import sys
q = int(input())
for _ in range(q):
	goal = []
	maze = []
	n = int(input())
	for i in range(n):
		l = [0] * n
		maze.append(l)
		l = list(map(int, input()))
		goal.append(l)
	for turn in range(n, 0, -1):
		targets = []
		for i in range(turn - 1):
			targets.append([turn - 1, i])
			targets.append([i, turn - 1])
		targets.append([turn - 1, turn - 1])
		isChange = True
		while isChange is True:
			isChange = False
			for (yy, xx) in targets:
				if goal[yy][xx] == 0:
					continue
				if maze[yy][xx] == 1:
					continue
				(nyy, nxx) = (yy + 1, xx + 1)
				if nxx >= n or nyy >= n:
					maze[yy][xx] = 1
					isChange = True
					continue
				if maze[nyy][xx] == 1 or maze[yy][nxx] == 1:
					maze[yy][xx] = 1
					isChange = True
					continue
	if maze == goal:
		print('YES')
	else:
		print('NO')


import sys
import math

def validate_grid(grid, N):
	for i in range(N):
		for j in range(N):
			if grid[i][j] == 1:
				return False
	return True

def check_grid(grid, N):
	for i in range(N - 1, -1, -1):
		bfs(grid, i, N - 1)
	for j in range(N - 1, -1, -1):
		bfs(grid, N - 1, j)

def bfs(grid, i, j):
	if grid[i][j] != 1:
		return
	grid[i][j] = 9
	if i - 1 >= 0:
		bfs(grid, i - 1, j)
	if j - 1 >= 0:
		bfs(grid, i, j - 1)
t = int(sys.stdin.readline().strip())
for i in range(t):
	n = int(sys.stdin.readline().strip())
	grid = []
	for j in range(n):
		line = [int(e) for e in sys.stdin.readline().strip()]
		grid.append(line)
	check_grid(grid, n)
	if validate_grid(grid, n):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	p = []
	for i in range(n):
		p.append(list(map(int, list(input()))))
	for i in range(n - 1):
		t = 0
		for j in range(n - 1):
			if p[i][j]:
				if not (p[i][j + 1] or p[i + 1][j]):
					print('NO')
					t = 1
					break
		if t:
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		d = input()
		h = [int(d[i]) for i in range(n)]
		a.append(h)
	d = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] == 1 or a[i][j + 1]:
					continue
				else:
					d = 1
					break
		if d == 1:
			break
	if d != 1:
		print('Yes')
	else:
		print('No')


T = int(input())
for t in range(T):
	n = int(input())
	A = [input() for i in range(n)]
	flag = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if A[i][j] == '1' and A[i + 1][j] == '0' and (A[i][j + 1] == '0'):
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	poly = [list(map(int, input())) for _ in range(n)]
	ok = True
	for (i, r) in enumerate(poly[:-1]):
		for (j, b) in enumerate(r[:-1]):
			if b:
				if not (poly[i + 1][j] or poly[i][j + 1]):
					ok = False
					break
		if not ok:
			break
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	m = [list(input()) for i in range(n)]
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and (not (m[i][j + 1] == '1' or m[i + 1][j] == '1')):
				flag = False
				break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


def solve():
	from sys import stdin, stdout
	import math
	t = int(stdin.readline())
	while t > 0:
		n = int(input())
		l = [list(map(int, input())) for x in range(n)]
		f = 1
		for i in range(n):
			for j in range(n):
				if l[i][j] == 0:
					continue
				elif i == n - 1 or j == n - 1:
					f = 1
				elif l[i][j + 1] == 1 or l[i + 1][j] == 1:
					f = 1
				else:
					f = 0
					break
			if f == 0:
				break
		if f == 1:
			print('YES')
		else:
			print('NO')
		t -= 1
solve()


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	k = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if j == n - 1 or i == n - 1:
					pass
				elif i + 1 < n and l[i + 1][j] == '1' or (l[i][j + 1] == '1' and j + 1 < n):
					pass
				else:
					k = 1
					break
	if k == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		row = input()
		matrix.append(list(row))
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1':
				if matrix[i + 1][j] == '1' or matrix[i][j + 1] == '1':
					ans = True
				else:
					ans = False
					break
		if ans == False:
			break
	if ans:
		print('YES')
	else:
		print('NO')


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 7)
	from collections import Counter, deque
	from itertools import combinations, permutations, accumulate, groupby, product
	from bisect import bisect_left, bisect_right
	from heapq import heapify, heappop, heappush
	import math
	t = int(input())
	for _ in range(t):
		n = int(input())
		d = [list(map(int, list(input().rstrip()))) for _ in range(n)]
		res = [[0] * n for _ in range(n)]
		s = deque()
		for i in range(n):
			if d[n - 1][i] == 1:
				res[n - 1][i] = 1
				s.append([n - 1, i])
			if d[i][n - 1] == 1:
				res[i][n - 1] = 1
				s.append([i, n - 1])
		while s:
			(x, y) = s.popleft()
			for (nx, ny) in [[x - 1, y], [x, y - 1]]:
				if 0 <= nx < n and 0 <= ny < n:
					if res[nx][ny] == 0 and d[nx][ny] == 1:
						res[nx][ny] = 1
						s.append([nx, ny])
		f = 0
		for i in range(n):
			for j in range(n):
				if d[i][j] != res[i][j]:
					f = 1
		if f == 1:
			print('NO')
		else:
			print('YES')
main()


for _ in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		l.append(list(input()))
	for row in range(n - 1):
		check = 0
		for col in range(n - 1):
			if l[row][col] == '1':
				if l[row + 1][col] != '1' and l[row][col + 1] != '1':
					check = 1
					break
		if check == 1:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(map(str, input())))
	f = 0
	for i in range(n):
		for j in range(n):
			if i == n - 1 or j == n - 1:
				continue
			g = 0
			if mat[i][j] == '1':
				if j + 1 < n:
					if mat[i][j + 1] == '1':
						g = 1
				if i + 1 < n:
					if mat[i + 1][j] == '1':
						g = 1
				if g == 0:
					f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	list1 = []
	for i in range(n):
		s = list(map(int, input()))
		list1.append(s)
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if list1[i][j] == 1:
				if not (list1[i + 1][j] == 1 or list1[i][j + 1] == 1):
					f = 1
	if f == 0:
		print('YES')
	else:
		print('NO')


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for _ in range(t):
	l = []
	n = int(input())
	for i in range(n):
		ka = list(input())
		l.append(ka)
	flag = 0
	for i in range(n):
		for j in range(n - 1, -1, -1):
			if l[i][j] == '1':
				if j == n - 1 or i == n - 1:
					continue
				elif l[i][j + 1] == '1' or l[i + 1][j] == '1':
					continue
				else:
					flag = 1
					break
		if flag:
			break
	if not flag:
		print('YES')
	else:
		print('NO')


def check(arr, n):
	answer = True
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == 1 and (arr[i + 1][j] == 0 and arr[i][j + 1] == 0):
				answer = False
				break
		if not answer:
			break
	return answer
t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = []
	for j in range(n):
		temp = list(map(int, input()))
		arr.append(temp)
	val = check(arr, n)
	if val:
		print('YES')
	else:
		print('NO')
	i += 1


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for j in range(n):
		matrix.append(input())
	result = True
	for j in range(n):
		for k in range(n):
			if matrix[j][k] == '1':
				if (j < n - 1 and matrix[j + 1][k] == '0') and (k < n - 1 and matrix[j][k + 1] == '0'):
					result = False
	print('YES' if result else 'NO')


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
tcid = 0
tcmax = 99999999

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def rev(lo, hi, positif=1):
	return range(hi, lo - 1, -positif)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

def perr(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def num(str=None):
	str = str or line()
	return float(str) if '.' in str else int(str)

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(num, o or split()))
if true:
	tcmax = num()

def mainloop(tcid=1):
	n = num()
	a = []
	for zzz in range(n):
		aa = line()
		a.append(list(map(int, aa)))

	def get(rr, cc):
		if rr == n or cc == n:
			return true
		return a[rr][cc] == 1

	def aaa():
		for rr in rev(0, n - 1):
			for cc in rev(0, n - 1):
				if get(rr, cc):
					xx = not get(rr + 1, cc)
					yy = not get(rr, cc + 1)
					if xx and yy:
						return 0
		return 1
	ans = aaa()
	ans = ['NO', 'YES'][ans]
	print(ans)
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


t = int(input())
for i in range(t):
	n = int(input())
	mat = []
	f = False
	for z in range(n):
		s = input()
		mat.append(s)
	for j in range(n):
		for k in range(n):
			if mat[j][k] == '1':
				if j == n - 1 or k == n - 1:
					pass
				elif mat[j + 1][k] == '1' or mat[j][k + 1] == '1':
					pass
				else:
					f = True
	if not f:
		print('YES')
	else:
		print('NO')


import math as mt
t = int(input())
for _ in range(t):
	n = int(input())
	a = [0] * (n + 1)
	for i in range(n):
		a[i] = list(input())
		a[i].append('1')
	a[n] = list('1')
	for j in range(n):
		a[n].append('1')
	valid = 1
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					valid = 1 * valid
				else:
					valid = 0 * valid
	if valid == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	Ans = []
	flag = 0
	for i in range(n):
		s = input()
		Ans.append(s)
	for i in range(n):
		for j in range(n):
			t1 = 0
			t2 = 0
			if Ans[i][j] == '1':
				if i != n - 1:
					if Ans[i + 1][j] == '0':
						t1 = 1
				if j != n - 1:
					if Ans[i][j + 1] == '0':
						t2 = 1
				if t1 == 1 and t2 == 1:
					flag = 1
					break
	print('YES') if flag == 0 else print('NO')


I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n,) = I()
	l = []
	f = 0
	for i in range(n):
		l.append(input())
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1' and i != n - 1 and (j != n - 1):
				k = int(l[i + 1][j]) + int(l[i][j + 1])
				if k == 0:
					f = 1
					break
		if f:
			break
	print('YNEOS'[f::2])


t = int(input())
while t > 0:
	n = int(input())
	a = []
	ans = True
	for i in range(n):
		a.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				ans = False
	if ans:
		print('YES')
	else:
		print('NO')
	t -= 1


T = int(input())
for t in range(T):
	ans = 'YES'
	n = int(input())
	l = []
	for k in range(n):
		l.append(list(input()))
	for i in range(n):
		for j in range(n):
			val = l[i][j]
			if val == '1':
				try:
					v1 = l[i][j + 1]
				except:
					v1 = '1'
				try:
					v2 = l[i + 1][j]
				except:
					v2 = '1'
				if v1 == '1' or v2 == '1':
					pass
				else:
					ans = 'NO'
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [input() for __ in range(n)]
	ans = False
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1' and (i + 1 < n and l[i + 1][j] != '1') and (j + 1 < n and l[i][j + 1] != '1'):
				ans = True
	print('YNEOS'[ans::2])


T = int(input())
for _ in range(T):
	n = int(input())
	li = []
	for i in range(n):
		temp = input()
		li.append(temp)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if li[i][j] == '1':
				if li[i][j + 1] == '0' and li[i + 1][j] == '0':
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	mat = []
	flag = 0
	for i in range(n):
		p = []
		k = input()
		for i in range(n):
			p.append(int(k[i]))
		mat.append(p)
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == 1:
				if mat[i][j + 1] != 1 and mat[i + 1][j] != 1:
					print('NO')
					flag = 1
					break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


import sys
input = sys.stdin.readline

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for _ in range(inin()):
	n = inin()
	l = []
	for i in range(n):
		l.append(input().strip())
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j + 1] != '1' and l[i + 1][j] != '1':
					f = 1
					break
		if f == 1:
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for k in range(t):
	n = int(input())
	x = []
	ans = 'YES'
	for aa in range(n):
		x.append(list(input()))
	p = len(x[0])
	for i in range(n - 1):
		for j in range(p - 1):
			arr1 = x[i]
			arr2 = x[i + 1]
			if arr1[j] == '1' and arr2[j] == '0':
				if arr1[j + 1] == '0':
					ans = 'NO'
	print(ans)


def solve():
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1 and (not (a[i + 1][j] == 1 or a[i][j + 1] == 1)):
				return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	a = [[int(i) for i in list(input())] for j in range(n)]
	print(solve())


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N,) = map(int, input().split())
	S = []
	for _ in range(N):
		S.append(input().strip())
	f = 0
	for i in range(N - 1):
		for j in range(N - 1):
			if S[i][j] == '1' and (S[i + 1][j] == '0' and S[i][j + 1] == '0'):
				f = 1
				break
		if f:
			break
	if f:
		print('NO')
	else:
		print('YES')


def gridcheck(n, grid):
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == '1' and grid[i + 1][j] == '0' and (grid[i][j + 1] == '0'):
				return 'NO'
	return 'YES'
numberofcases = int(input())
for _ in range(numberofcases):
	n = int(input())
	grid = []
	for _ in range(n):
		grid.append(input())
	print(gridcheck(n, grid))


for i in range(int(input())):
	n = int(input())
	matrix = []
	p = 0
	for i in range(n):
		matrix.append(list(input()))
	for x in range(n):
		for y in range(n):
			if matrix[x][y] == '1':
				if y == n - 1 or x == n - 1:
					continue
				elif matrix[x][y + 1] == '1' or matrix[x + 1][y] == '1':
					continue
				else:
					p = 1
	if p == 0:
		print('YES')
	else:
		print('NO')


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def main():
	n = itg()
	arr = [list(map(int, inp())) for _ in range(n)]
	g = [[0] * n for _ in range(n)]
	for i in range(n):
		g[i][-1] = arr[i][-1]
	g[-1] = arr[-1].copy()
	modify = True
	while modify:
		modify = False
		for i in range(n - 2, -1, -1):
			for j in range(n - 2, -1, -1):
				if arr[i][j] and (not g[i][j]) and (arr[i + 1][j] or arr[i][j + 1]):
					g[i][j] = 1
					modify = True
					break
			if modify:
				break
	return 'YES' if arr == g else 'NO'
for case in range(itg()):
	print(main())


import sys
import math
from collections import *
input = sys.stdin.readline

def getans(N):
	ARR = []
	for i in range(N):
		ARR.append(list(input()))
		ARR[i].pop()
	(FLAG1, FLAG2) = (True, True)
	for i in range(N):
		if FLAG1 == False and FLAG2 == False:
			break
		(FLAG1, FLAG2) = (True, True)
		for var in range(N):
			if FLAG1 == False and FLAG2 == False:
				break
			(FLAG1, FLAG2) = (True, True)
			if ARR[i][var] == '1':
				for fk in range(i + 1, N):
					if ARR[fk][var] == '1':
						break
					if ARR[fk][var] == '0':
						FLAG1 = False
						break
				for tot in range(var + 1, N):
					if ARR[i][tot] == '1':
						break
					if ARR[i][tot] == '0':
						FLAG2 = False
	if FLAG1 == True and FLAG2 == True:
		print('YES')
	else:
		print('NO')
test = int(input())
for _ in range(test):
	N = int(input())
	getans(N)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [[int(c) for c in input()] for i in range(n)]
	t = [0] * (n * n)
	ok = True
	for sij in range(2 * n - 2, -1, -1):
		if not ok:
			break
		il = max(0, sij - n - 1)
		ir = min(n - 1, sij)
		for i in range(il, ir + 1):
			j = sij - i
			if j < 0 or j >= n or s[i][j] == 0:
				continue
			ii = i * n + j
			if i == n - 1 or j == n - 1 or t[ii + n] == 1 or (t[ii + 1] == 1):
				t[ii] = 1
			else:
				ok = False
				break
	print('YES' if ok else 'NO')


for _ in range(int(input())):
	N = int(input())
	Matrix = []
	for i in range(N):
		temp = input()
		Matrix.append(temp)
	flag = 1
	for i in range(N - 1):
		for j in range(N - 1):
			if Matrix[i][j] == '1':
				if Matrix[i][j + 1] == '0' and Matrix[i + 1][j] == '0':
					print('NO')
					flag = 0
					break
		if flag == 0:
			break
	if flag:
		print('YES')


from sys import stdin

def func(mat, n):
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i != n - 1 and j != n - 1:
					if mat[i + 1][j] == '0' and mat[i][j + 1] == '0':
						return False
	return True
for _ in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		s = input().strip()
		mat.append(s)
	if func(mat, n) == True:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	a = []
	for _ in range(n):
		x = input()
		temp = []
		for i in x:
			temp.append(int(i))
		a.append(temp)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i][j + 1] != 1 and a[i + 1][j] != 1:
					flag = 1
			if flag == 1:
				break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


for _ in range(int(input())):
	n1 = int(input())
	a = []
	p = 0
	for i in range(n1):
		l = list(map(int, input()))
		a.append(l)
	for i in range(len(a)):
		b = a[i]
		for j in range(n1):
			if b[j] == 1:
				if (j != n1 - 1 and b[j + 1] != 1) and (i != n1 - 1 and a[i + 1][j] != 1):
					p = 1
			if p == 1:
				break
		if p == 1:
			break
	if p == 0:
		print('YES')
	else:
		print('NO')


a = int(input())
for i in range(a):
	lis = []
	n = int(input())
	for j in range(n):
		u = input()
		lis.append(list(u))
	p = 0
	if n == 1:
		print('YES')
	else:
		for j in range(0, n - 1):
			for k in range(0, n - 1):
				if lis[j][k] == '1':
					if lis[j + 1][k] == '1' or lis[j][k + 1] == '1':
						continue
					else:
						p = 1
						break
			if p == 1:
				break
		if p == 0:
			print('YES')
		else:
			print('NO')


from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(input())
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if ar[i][j] == '1':
				if ar[i + 1][j] == '0' and ar[i][j + 1] == '0':
					ans = 'NO'
			elif ar[i][j] == '1':
				if ar[i + 1][j] == '0':
					ans = 'NO'
			elif ar[i][j] == '1':
				if ar[i][j + 1] == '0':
					ans = 'NO'
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = []
	for i in range(n):
		x.append(list(map(int, list(input().strip()))))
	flg = False
	for i in range(n - 1):
		for j in range(n - 1):
			if x[i][j] and (not (x[i + 1][j] or x[i][j + 1])):
				flg = True
				break
		if flg:
			break
	if flg:
		print('NO')
	else:
		print('YES')


class Solution:

	def __init__(self):
		self.t = int(input())
		self.n = int
		self.board = []
		self.valid = []

	def read(self):
		self.n = int(input())
		self.board = []
		self.valid = [[0 for _ in range(self.n)] for __ in range(self.n)]
		for _ in range(self.n):
			self.board.append(input().strip())

	def recursion(self, i, j):
		if i < 0 or j < 0:
			return
		if self.valid[i][j] or self.board[i][j] == '0':
			return
		self.valid[i][j] = 1
		self.recursion(i - 1, j)
		self.recursion(i, j - 1)

	def solve(self):
		for i in range(self.n):
			self.recursion(i, self.n - 1)
			self.recursion(self.n - 1, i)
		sol = 1
		for i in range(self.n):
			for j in range(self.n):
				if self.board[i][j] == '1' and (not self.valid[i][j]):
					sol = 0
		if sol:
			print('YES')
		else:
			print('NO')
sol = Solution()
while sol.t:
	sol.t -= 1
	sol.read()
	sol.solve()


for y in range(int(input())):
	n = int(input())
	lst = []
	foo = 0
	for i in range(n):
		lst.append(list(input()))
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if lst[i][j] == '1':
				if lst[i + 1][j] == '1' or lst[i][j + 1] == '1':
					pass
				else:
					print('NO')
					foo = 1
					break
		if foo == 1:
			break
	if foo == 0:
		print('YES')


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for tt in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		l.append(input())
	check = [[False for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if j + 1 == n:
					check[i][j] = True
				elif l[i][j + 1] == '1':
					check[i][j] = True
	for i in range(n):
		for j in range(n):
			if l[j][i] == '1':
				if j + 1 == n:
					check[j][i] = True
				elif l[j + 1][i] == '1':
					check[j][i] = True
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1' and check[i][j] == False:
				ans = 'NO'
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	s = []
	for i in range(n):
		s.append(input())
	z = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if s[i][j] == '1' and (s[i + 1][j] != '1' and s[i][j + 1] != '1'):
				z = 1
	if z == 1:
		print('NO')
	else:
		print('YES')
	t = t - 1


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal
from decimal import *

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def preorder_postorder(root, paths, parent, left, level):
	l = 0
	queue = [root]
	while queue != []:
		node = queue[-1]
		flag = 0
		for i in paths[node]:
			if i != parent[node] and level[i] == sys.maxsize:
				parent[i] = node
				level[i] = level[node] + 1
				flag = 1
				queue.append(i)
		if flag == 0:
			left[parent[node]] += 1 + left[node]
			queue.pop()
	return [parent, left, level]

def nCr(n, r, p):
	if r == 0:
		return 1
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 998244353
	limit = 10 ** 18
	for test in range(tests):
		n = inp()
		m = []
		for i in range(n):
			m.append(insr())
		for i in range(n):
			m[i] = m[i] + ['2']
		m = m + [['2' for i in range(n + 1)]]
		flag = 0
		for i in range(n - 1, -1, -1):
			for j in range(n - 1, -1, -1):
				if m[i][j] == '1':
					if m[i + 1][j] == '2' or m[i][j + 1] == '2':
						m[i][j] = '2'
					else:
						flag = 1
						break
		if flag == 0:
			print('YES')
		else:
			print('NO')
main()


for h in range(int(input())):
	n = int(input())
	a = []
	t = 1
	for i in range(n):
		f = list(map(int, input()))
		a.append(f)
	for i in range(n):
		for j in range(n):
			if i < n - 1 and j < n - 1:
				if a[i][j] == 1:
					if a[i][j + 1] == 1 or a[i + 1][j] == 1:
						t = 1
					else:
						print('No')
						t = 0
						break
				if t == 0:
					break
			if t == 0:
				break
		if t == 0:
			break
	if t == 1:
		print('Yes')


a = int(input())
for x in range(a):
	a = int(input())
	field = [None] * a
	flag = True
	for x in range(a):
		field[x] = [str(y) for y in input()]
	for x in range(a - 1):
		for y in range(a - 1):
			if field[x][y] == '1':
				if field[x + 1][y] == '0' and field[x][y + 1] == '0':
					print('NO')
					flag = False
					break
		if not flag:
			break
	if flag:
		print('YES')


t = int(input())
while t:
	t -= 1
	n = int(input())
	vek = []
	b = 1
	for i in range(n):
		vek.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if vek[i][j] == '1':
				if vek[i + 1][j] != '1' and vek[i][j + 1] != '1':
					b = 0
	if b:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = []
	for _ in range(n):
		temp = list(map(int, input()))
		list1.append(temp)
	b = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if list1[i][j] == 1:
				if list1[i + 1][j] == 0 and list1[i][j + 1] == 0:
					b = 0
					break
		if b == 0:
			break
	if b == 0:
		print('NO')
	else:
		print('YES')


from sys import stdin
for _ in range(int(input())):
	n = int(input())
	need_one_col = [False] * n
	res = True
	for _ in range(n):
		s = input()
		if res:
			need_one = False
			for i in range(n):
				if need_one_col[i]:
					if s[i] == '1':
						need_one = True
						need_one_col[i] = False
					else:
						res = False
						break
				elif need_one and s[i] == '0':
					need_one_col[i - 1] = True
					need_one = False
				elif s[i] == '1' and i < n - 1:
					need_one = True
	print(['No', 'Yes'][res])


t = int(input())
for _ in range(t):
	n = int(input())
	matrix = []
	for r in range(n):
		row = [int(i) for i in input()]
		matrix.append(row)
	ans = 'YES'
	for row in range(n):
		for col in range(n):
			if matrix[row][col] == 1:
				if row == n - 1 or col == n - 1:
					pass
				elif matrix[row + 1][col] == 1 or matrix[row][col + 1] == 1:
					pass
				else:
					ans = 'NO'
					break
	print(ans)


for t in range(int(input())):
	n = int(input())
	m = ''
	for i in range(n):
		m += input()
	if '0' not in m or '1' not in m:
		print('YES')
	else:
		for i in range(n * n - n - 1):
			if m[i] == '1':
				if m[i + 1] != '1' and m[i + n] != '1' and ((i + 1) % n != 0):
					print('NO')
					break
		else:
			print('YES')


n = 0
S = []

def check():
	for i in range(n):
		for j in range(n):
			if S[i][j] == '1':
				c1 = True
				c2 = True
				if i + 1 < n:
					c1 = S[i + 1][j] == '1'
				if j + 1 < n:
					c2 = S[i][j + 1] == '1'
				if not (c1 or c2):
					return False
	return True
TT = int(input())
for _ in range(TT):
	n = int(input())
	S = []
	for x in range(n):
		aa = input()
		S.append(aa)
	ans = check()
	if ans:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	n = int(input())
	dict1 = {}
	posy = True
	for s in range(n):
		dict1[s] = input()
	for k in range(n):
		for m in range(n):
			coord = dict1[k][m]
			if k == n - 1 or m == n - 1:
				continue
			else:
				right = dict1[k][m + 1]
				left = dict1[k + 1][m]
				if coord == str(1):
					if right == str(0) and left == str(0):
						posy = False
	if posy == True:
		print('YES')
	else:
		print('NO')


from sys import stdin
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
for _ in range(int(input())):
	n = I()
	a = []
	k = 1
	for i in range(n):
		a.append(S() + '1')
	a.append('1' * (n + 1))
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				k = 0
				break
		if not k:
			break
	if k:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = []
	for _ in range(n):
		l = list(input())
		A.append(l)
	allonesgood = True
	for j in range(n - 1, -1, -1):
		for i in range(n - 1, -1, -1):
			if A[i][j] == '1':
				if i == n - 1 or j == n - 1:
					A[i][j] = '+'
				elif A[i][j + 1] == '+' or A[i + 1][j] == '+':
					A[i][j] = '+'
				else:
					allonesgood = False
		if not allonesgood:
			break
	if allonesgood:
		print('YES')
	else:
		print('NO')


cases = int(input())
for i in range(cases):
	n = int(input())
	arr = []
	for j in range(n):
		line = list(input())
		sub = []
		for a in line:
			sub.append(int(a))
		arr.append(sub)
	floating = False
	for k in range(n - 1):
		for p in range(n - 1):
			floating = False
			ele = arr[k][p]
			right = arr[k][p + 1]
			down = arr[k + 1][p]
			if ele == 0:
				continue
			if ele == 1:
				if right == down == 0:
					floating = True
					break
		if floating:
			break
	if floating:
		print('NO')
	else:
		print('YES')


for nt in range(int(input())):
	n = int(input())
	m = []
	for i in range(n):
		m.append(input())
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if m[i][j] == '1':
				if j == n - 1 or i == n - 1:
					continue
				if j != n - 1 and m[i][j + 1] == '1':
					continue
				if i != n - 1 and m[i + 1][j] == '1':
					continue
				ans = 'NO'
	print(ans)


I = input
for _ in range(int(I())):
	n = int(I())
	l = [[int(i) for i in list(I())] for j in range(n)]
	try:
		for i in range(n - 1, -1, -1):
			for j in range(n - 1, -1, -1):
				if l[i][j] == 1 and j + 1 != n and (i + 1 != n):
					if l[i][j + 1] == 1 or l[i + 1][j] == 1:
						continue
					assert 1 == 0
		print('YES')
	except AssertionError:
		print('NO')


import math
import sys
from collections import deque
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	p = []
	for i in range(n):
		p.append(list(input().strip()))
	w = True
	for i in range(n - 1):
		if not w:
			break
		for j in range(n - 1):
			if p[i][j] == '1':
				if p[i + 1][j] == '0' and p[i][j + 1] == '0':
					print('NO')
					w = False
					break
	if w:
		print('YES')


import collections

def find_training():
	one = []
	edge = []
	inside = []
	k = []
	all_one = True
	all_zero = True
	n = int(input())
	for i in range(n):
		k.append(list(input()))
		counter = collections.Counter(k[-1])
		if counter['1'] != len(k[-1]):
			all_one = False
		if counter['0'] != len(k[-1]):
			all_zero = False
	if all_one or all_zero:
		return 'YES'
	for (i, row) in enumerate(k):
		row = list(map(int, row))
		for (idx, val) in enumerate(row):
			if val == 1:
				if idx == n - 2 or i == n - 2:
					one.insert(0, [i, idx])
				else:
					one.append([i, idx])
				if idx == n - 1 or i == n - 1:
					edge.append([i, idx])
				else:
					inside.append([i, idx])
	result = 'YES'
	for element in inside:
		if [element[0] + 1, element[1]] in one or [element[0], element[1] + 1] in one:
			result = 'YES'
		else:
			result = 'NO'
			break
	return result
t = int(input())
output = []
for i in range(t):
	output.append(find_training())
for i in output:
	print(i)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = [S() for _ in range(n)]
		ok = True
		for i in range(n - 1):
			for j in range(n - 1):
				if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
					ok = False
		rr.append(IF(ok, 'YES', 'NO'))
	return JA(rr, '\n')
print(main())


for i in range(int(input())):
	n = int(input())
	l = []
	for j in range(n):
		l.append(input())
	log = 0
	for x in range(n):
		for y in range(n):
			if int(l[x][y]) == 1:
				if not (x + 1 == n or y + 1 == n or int(l[x + 1][y]) == 1 or (int(l[x][y + 1]) == 1)):
					log = 1
					break
		if log == 1:
			break
	if log == 1:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline

def ini():
	return int(input())

def inli():
	return list(map(int, input().split()))

def dili():
	return list(map(int, input()[:-1]))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def soln():
	n = ini()
	mat = []
	for i in range(n):
		mat.append(dili())
	flag = False
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i < n - 1 and mat[i + 1][j] == 0 and (j < n - 1) and (mat[i][j + 1] == 0):
					flag = True
					print('NO')
					return
	print('YES')
for i in range(ini()):
	soln()


for _ in range(int(input())):
	n = int(input())
	a = [input() for i in range(n)]
	flag = True
	if n == 1:
		print('YES')
	else:
		for i in range(n - 2, -1, -1):
			for j in range(n - 2, -1, -1):
				if a[i][j] == '1':
					if a[i][j + 1] == '1' or a[i + 1][j] == '1':
						continue
					else:
						flag = False
						break
			if flag == False:
				break
		if flag:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline
for t in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input()[:-1])
	result = True
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j + 1] == '0' and l[i + 1][j] == '0':
					result = False
			if result == False:
				break
		if result == False:
			break
	if result:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	for _ in range(N()):
		n = N()
		gp = [list(input()) for _ in range(n)]
		tag = True
		for i in range(n):
			for j in range(n):
				if gp[i][j] == '1':
					if (i + 1 < n and gp[i + 1][j] == '0') and (j + 1 < n and gp[i][j + 1] == '0'):
						tag = False
						break
			if tag == False:
				break
		if tag:
			print('YES')
		else:
			print('NO')
main()


def main():
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(map(int, input())))
	for i in range(n):
		for j in range(n):
			if i == n - 1 or j == n - 1:
				continue
			elif a[i][j] == 1:
				if a[i][j + 1] != 1 and a[i + 1][j] != 1:
					return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	print(main())


n = int(input())
m = []
for i in range(n):
	size = int(input())
	elem = [[int(j) for j in input()] for k in range(size)]
	m += [elem]

def check_zero(l):
	size = len(l[0])
	for i in range(size):
		for j in range(size):
			if l[i][j] != 0:
				return False
	return True

def first_rule(l):
	size = len(l[0])
	for i in range(size):
		if l[i][size - 1] == 1 or l[size - 1][i] == 1:
			return True
	return False

def second_rule(l):
	size = len(l[0])
	for j in range(size - 1):
		for i in range(size - 1):
			if l[i][j] == 1 and (not (l[i + 1][j] == 1 or l[i][j + 1] == 1)):
				return False
	return True
for i in m:
	if check_zero(i) or (first_rule(i) and second_rule(i)):
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append([int(x) for x in input()])
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == 1:
				if (i == n - 1 or j == n - 1 or (i < n - 1 and matrix[i + 1][j] == 1) or (j < n - 1 and matrix[i][j + 1] == 1)) == 0:
					ans = 'NO'
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = []
	x = []
	for i in range(n):
		l.append(input())
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if l[i][j] == '1':
				f = 0
				if i + 1 >= n or j + 1 >= n:
					continue
				if i + 1 < n and l[i + 1][j] == '1':
					f = 1
				if j + 1 < n and l[i][j + 1] == '1':
					f = 1
				x.append(f)
	p = 1
	for i in x:
		if i == 0:
			p = 0
			break
	if p == 1:
		print('YES')
	else:
		print('NO')


import math as mt
import sys, string
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
t = I()
for _ in range(t):
	l = []
	n = I()
	for i in range(n):
		x = input().strip()
		l.append(x)
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j + 1] == '1' or l[i + 1][j] == '1':
					continue
				else:
					f = 1
					break
	if f == 1:
		print('NO')
	else:
		print('YES')


def check(mat, n):
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i == n and j == n:
					pass
				if i != n and j == n:
					if mat[i + 1][j] == '1':
						pass
					else:
						return False
				if i == n and j != n:
					if mat[i][j + 1] == '1':
						pass
					else:
						return False
				elif mat[i][j + 1] == '1' or mat[i + 1][j] == '1':
					pass
				else:
					return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		s = input()
		mat.append(s)
	if check(mat, n - 1):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	lis = []
	for x in range(n):
		s = input()
		lis1 = []
		for y in range(n):
			lis1.append(int(s[y]))
		lis.append(lis1)
	check = True
	for x in range(n):
		for y in range(n):
			if lis[x][y] == 1:
				if x == n - 1 or y == n - 1:
					continue
				else:
					ny = y + 1
					nx = x + 1
					if lis[nx][y] == 1 or lis[x][ny] == 1:
						pass
					else:
						check = False
						break
		if check == False:
			break
	if check == True:
		print('Yes')
	else:
		print('No')


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(i) for i in st()]
for _ in range(val()):
	n = val()
	l = []
	for i in range(n):
		l.append(li3())
	ans = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == 1 and l[i + 1][j] == 0 and (l[i][j + 1] == 0):
				ans = 0
				break
	print('YES' if ans else 'NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
for _ in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append([int(x) for x in list(input())])
	flag = 1
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				elif mat[i][j + 1] == 1 or mat[i + 1][j] == 1:
					continue
				else:
					flag = 0
					break
	print('YNEOS'[flag == 0::2])


from sys import stdin, stdout
for case in range(int(stdin.readline())):
	n = int(stdin.readline())
	grid = []
	for a in range(n):
		grid.append([int(x) for x in stdin.readline().strip()])
	valid = True
	for x in range(n - 1):
		for y in range(n - 1):
			if grid[x][y] == 1:
				if not grid[x + 1][y] and (not grid[x][y + 1]):
					valid = False
					break
	if valid:
		print('YES')
	else:
		print('NO')


t = int(input())
for k in range(t):
	n = int(input())
	pol = [input() for i in range(n)]
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if pol[i][j] == '1' and pol[i + 1][j] != '1' and (pol[i][j + 1] != '1'):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)


from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict
import math
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))

def valid(M, i, j):
	(rows, cols) = (len(M), len(M[0]))
	inside = lambda r, c: r >= 0 and c >= 0 and (r < rows) and (c < cols)
	if M[i][j] == 0 or not inside(i + 1, j) or (not inside(i, j + 1)):
		return True
	if M[i + 1][j] == 0 and M[i][j + 1] == 0:
		return False
	return True

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		M = []
		for __ in range(n):
			row = []
			for c in rll()[0]:
				row.append(int(c))
			M.append(row)
		ans = True
		for r in range(n):
			for c in range(n):
				if not valid(M, r, c):
					ans = False
					break
		print('YES' if ans else 'NO')
	stdout.close()
main()


def solve(A):
	for i in range(len(A)):
		for j in range(len(A)):
			if A[i][j] == '1':
				if i + 1 < len(A) and j + 1 < len(A):
					if A[i + 1][j] != '1' and A[i][j + 1] != '1':
						return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for o in range(n):
		lst = input()
		arr.append(lst)
	print(solve(arr))


q = int(input())
for w in range(q):
	n = int(input())
	a = []
	fl = True
	for i in range(n):
		s = input()
		a.append(s)
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == a[i][j + 1] == '0':
				fl = False
	if fl:
		print('YES')
	else:
		print('NO')


tests = int(input())
for _ in range(tests):
	n = int(input())
	mat = []
	c = 0
	z = 0
	for _ in range(n):
		mat.append(input())
	for j in range(n):
		for i in range(n):
			if mat[j][i] == '1':
				t = 0
				p = 0
				if j + 1 <= n - 1:
					if mat[j + 1][i] != '0':
						t = 1
				else:
					t = 1
				if i + 1 <= n - 1:
					if mat[j][i + 1] != '0':
						p = 1
				else:
					p = 1
				if p != 0 or t != 0:
					pass
				else:
					z = 1
					break
		if z == 1:
			break
	if z == 1:
		print('NO')
	else:
		print('YES')


n = int(input())
outt = []
for i in range(n):
	m = int(input())
	mapp = [[] for j in range(m)]
	for j in range(m):
		mapp[j].extend(list(map(int, list(input()))))
	boo = True
	for xx in range(m - 1):
		for x in range(m - 1):
			if mapp[xx][x] > 0:
				if mapp[xx + 1][x] == 0 and mapp[xx][x + 1] == 0:
					outt.append('NO')
					boo = False
					break
		if not boo:
			break
	else:
		outt.append('YES')
for i in outt:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	x = []
	count = 0
	for j in range(n):
		x.append(input())
	for j in range(n):
		for k in range(n):
			if x[j][k] == '1':
				if k == n - 1 or j == n - 1:
					continue
				elif x[j][k + 1] == '1' or x[j + 1][k] == '1':
					continue
				else:
					count = 1
	if count == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	arr = [[0 for i in range(n)] for j in range(n)]
	ans = 'YES'
	for i in range(n):
		st = input()
		for k in range(n):
			arr[i][k] = int(st[k])
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] == 1 or arr[i][j + 1] == 1:
					ans = 'YES'
				else:
					ans = 'NO'
					break
		if ans == 'NO':
			break
	print(ans)


import sys
print = sys.stdout.write
for _ in range(int(input())):
	(n, ans) = (int(input()), True)
	(prev, pres) = ([int(el) for el in input()], [])
	for _ in range(n - 1):
		for (i, el) in enumerate(input()):
			pres.append(int(el))
			if prev[i] == 1 and i != n - 1:
				if prev[i + 1] != 1 and pres[-1] != 1:
					ans = False
		prev = pres.copy()
		pres.clear()
	if ans:
		print('YES' + '\n')
	else:
		print('NO' + '\n')


for k in range(int(input())):
	n = int(input())
	polygon = []
	for i in range(n):
		string = [j for j in input()]
		polygon += [string]
	if n == 1:
		print('YES')
	else:
		flag = 0
		for i in range(n - 2, -1, -1):
			for j in range(n - 2, -1, -1):
				if polygon[i][j] == '1':
					if polygon[i + 1][j] == '1' or polygon[i][j + 1] == '1':
						pass
					else:
						print('NO')
						flag = 1
						break
			if flag == 1:
				break
		if flag == 0:
			print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	f = True
	for j in range(n):
		if not f:
			break
		for k in range(n):
			if l[j][k] == '1':
				if j == n - 1 or k == n - 1:
					continue
				elif l[j + 1][k] == '0' and l[j][k + 1] == '0':
					f = False
					break
	if f:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(I()):
	n = I()
	a = []
	for i in range(n):
		a.append(IN())
	b = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					b = 0
					break
		if b == 0:
			break
	if b == 0:
		print('NO')
	else:
		print('YES')


def check(r, c, ar, si):
	if r + 1 == si or c + 1 == si:
		return True
	if r + 1 < si:
		if ar[r + 1][c] == 1:
			return True
	if c + 1 < si:
		if ar[r][c + 1] == 1:
			return True
	return False
for _ in range(int(input())):
	si = int(input())
	(ar, flag) = ([], 1)
	for _ in range(si):
		ar.append([int(s) for s in input()])
	for x in range(si):
		for y in range(si):
			if ar[x][y]:
				if not check(x, y, ar, si):
					print('NO')
					flag = 0
					break
		if not flag:
			break
	if flag:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	lines = []
	for i in range(n):
		lines.append(input())
	flg = True
	for i in range(n - 1):
		for j in range(n - 1):
			if lines[i][j] == '1' and lines[i][j + 1] == '0' and (lines[i + 1][j] == '0'):
				print('NO')
				flg = False
				break
		if not flg:
			break
	if flg:
		print('YES')


n = int(input())
for _ in range(n):
	m = int(input())
	mat = []
	for i in range(m):
		mat.append(input())
	done = False
	for i in range(m):
		if done:
			break
		for j in range(m):
			if mat[i][j] == '1' and i != m - 1 and (j != m - 1):
				if mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
					continue
				else:
					done = True
					break
	print('NO' if done else 'YES')


import math

def dict_sort(ans):
	ans = sorted(ans.items(), reverse=True, key=lambda kv: kv[1])

def is_prime(n, k):
	if n == 1:
		return 0
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			res = i
			break
	return res

def inp():
	ls = list(map(int, input().split()))
	return ls
t = int(input())
for ii in range(t):
	n = int(input())
	ls = []
	for i in range(n):
		ls.append(input())
	flag = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if ls[i][j] == '1' and (ls[i + 1][j] == '0' and ls[i][j + 1] == '0'):
				flag = False
				break
		if not flag:
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


q = int(input())
while q:
	q -= 1
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(map(int, list(input()))))
	g = 1
	for i in range(0, n):
		for j in range(0, n):
			if a[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				if a[i][j + 1] == 1:
					continue
				if a[i + 1][j] == 1:
					continue
				g = 0
	if g:
		print('YES')
	else:
		print('NO')


import sys
import heapq
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for i in range(int(input())):
	q = int(input())
	a = []
	k = 0
	for i in range(q):
		a.append(list(input()))
	for j in range(q):
		for i in range(q, -1, -1):
			if a[j][i] == '1':
				if i == q - 1 or j == q - 1 or a[j][i + 1] == '1' or (a[j + 1][i] == '1'):
					pass
				else:
					print('NO')
					k = 1
					break
		if k == 1:
			break
	if k == 1:
		pass
	else:
		print('YES')


import math
for _ in range(int(input())):
	n = int(input())
	matrix = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		s = input()
		for j in range(n):
			matrix[i][j] = s[j]
	fg = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1':
				if matrix[i][j + 1] == '0' and matrix[i + 1][j] == '0':
					print('NO')
					fg = 1
					break
		if fg == 1:
			break
	if fg == 0:
		print('YES')


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	n = int(input())
	mat = []
	for i in range(n):
		r = list(input())
		mat.append(r)
	res = 'YES'
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i < n - 1:
					if mat[i + 1][j] == '1':
						continue
				if j < n - 1:
					if mat[i][j + 1] == '1':
						continue
				if i < n - 1 and j < n - 1:
					res = 'NO'
					break
	print(res)
t = int(input())
for _ in range(t):
	main()


T = int(input())
for t in range(T):
	N = int(input())
	l = []
	flag = 0
	for i in range(N):
		x = [int(i) for i in input()]
		l.append(x)
	for i in range(N):
		for j in range(N):
			if i == N - 1 or j == N - 1:
				continue
			if l[i][j] == 1:
				res = False
				if i + 1 < N and l[i + 1][j] == 1:
					res = res or True
				if j + 1 < N and l[i][j + 1] == 1:
					res = res or True
				if not res:
					flag = 1
					break
			else:
				continue
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	ls = list()
	flag = False
	for j in range(n):
		ls.append(list(input()))
	for j in range(n - 1):
		for k in range(n - 1):
			if ls[j][k] == '1':
				if ls[j + 1][k] != '1' and ls[j][k + 1] != '1':
					flag = True
					break
		if flag:
			break
	print('NO' if flag else 'YES')


def check(l, n):
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if l[i][j] == '1':
				if j == n - 1 or i == n - 1 or (l[i][j + 1] == '1' or l[i + 1][j] == '1'):
					continue
				else:
					return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for j in range(n):
		l.append(list(input()))
	print(check(l, n))


kl = int(input())
for l in range(kl):
	n = int(input())
	a = [0] * n
	for i in range(n):
		a[i] = input()
	pr = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and (a[i][j + 1] == '0' and a[i + 1][j] == '0'):
				pr = 1
	if pr:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	m = []
	for i in range(n):
		a = list(map(int, input()))
		m.append(a)
	f = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if m[i][j] == 1 and m[i + 1][j] == 0 and (m[i][j + 1] == 0):
				f = False
	print('YES') if f else print('NO')


mod = int(1000000000.0) + 7

def ip():
	return int(input())

def Ip():
	return map(int, input().split())
for _ in range(0, int(input())):
	n = ip()
	l = [0] * n
	for i in range(0, n):
		s = input()
		l[i] = s
	c = 1
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] != '1' and l[i][j + 1] != '1':
					c = 0
					break
		if c == 0:
			break
	ans = ['NO', 'YES']
	print(ans[c])


def ans(arr, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] == 0 and arr[i][j + 1] == 0:
					return 'NO'
	return 'YES'
m = int(input())
for i in range(m):
	n = int(input())
	arr = []
	for j in range(n):
		w = []
		a = input()
		for k in a:
			w.append(int(k))
		arr.append(w)
	print(ans(arr, n))


testcase = int(input())
for test in range(testcase):
	n = int(input())
	mat = []
	for i in range(n):
		row = list(input())
		row = [int(i) for i in row]
		mat.append(row)
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == 1:
				if mat[i + 1][j] == 0 and mat[i][j + 1] == 0:
					flag = True
					break
	print('YES' if not flag else 'NO')


for case in range(int(input())):
	n = int(input())
	mat = []
	for row in range(n):
		mat.append(input())
	for i in range(n):
		if mat[i][-1] == '1':
			flag = True
			break
		elif i == n - 1 and '1' in mat[i]:
			flag = True
			break
		else:
			flag = False
	if flag == True:
		for i in range(n - 1):
			for j in range(n - 1):
				if mat[i][j] == '1':
					if mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
						flag = True
					else:
						flag = False
						break
			if flag == False:
				break
	for i in mat:
		if '1' in i:
			x = 0
			break
		else:
			x = 1
	if flag == True:
		print('YES')
	elif x == 1:
		print('YES')
	else:
		print('NO')


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
mod = 1000000007
import math
import bisect
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def main():
	for _ in range(ii()):
		n = ii()
		a = []
		for i in range(n):
			a.append(si())
		f = 1
		for i in range(n):
			for j in range(n):
				if a[i][j] == '1':
					if i == n - 1 or j == n - 1:
						continue
					if a[i + 1][j] == '0' and a[i][j + 1] == '0':
						f = 0
						break
		if f:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(str(input())))
	flag = 0
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if j + 1 < n and mat[i][j + 1] == '1':
					continue
				elif i + 1 < n and mat[i + 1][j] == '1':
					continue
				elif i == n - 1 or j == n - 1:
					continue
				else:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = [list(input()) for i in range(n)]
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				else:
					f = 1
					break
		if f == 1:
			break
	if f:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		x = list(input())
		a.append(list(map(int, x)))
	c = 0
	for j in range(n):
		for k in range(n):
			if a[j][k] == 1:
				if j == n - 1 or k == n - 1:
					continue
				elif k + 1 < n and a[j][k + 1] == 0:
					if a[j + 1][k] == 0:
						c = 1
						break
				elif j + 1 < n and a[j + 1][k] == 0:
					if a[j][k + 1] == 0:
						c = 1
						break
		if c == 1:
			break
	if c == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					pass
				else:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	dp = [[0 for _ in range(n)] for _ in range(n)]
	for i in range(n):
		s = input()
		arr.append(s)
		if s[-1] == '1':
			dp[i][-1] = 1
		if i == n - 1:
			for i in range(n):
				if s[i] == '1':
					dp[-1][i] = 1
	flag = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if arr[i][j] == '1':
				if dp[i + 1][j] == 1 or dp[i][j + 1] == 1:
					dp[i][j] = 1
				else:
					flag = False
					break
	if flag == False:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	lis = []
	c = 0
	for __ in range(n):
		s = list(input())
		lis.append([int(a) for a in s])
	for i in range(n - 1):
		for j in range(n - 1):
			if lis[i][j] == 1:
				if lis[i][j + 1] == 0 and lis[i + 1][j] == 0:
					print('no')
					c = 1
					break
		if c:
			break
	if not c:
		print('yes')


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		b = input()
		a.append(b)
	flag = 0
	for k in range(n):
		for l in range(n):
			if a[k][l] == '1':
				if k == n - 1 or l == n - 1:
					continue
				elif a[k][l + 1] == '1' or a[k + 1][l] == '1':
					continue
				else:
					flag = 1
					break
		if flag > 0:
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [''] * n
	for i in range(n):
		a[i] = list(input())
	if n == 1:
		print('YES')
		continue
	flg = True
	for y in range(n - 2, -1, -1):
		for x in range(n - 2, -1, -1):
			if a[y][x] == '1':
				if a[y + 1][x] == '1' or a[y][x + 1] == '1':
					pass
				else:
					flg = False
					break
		if not flg:
			break
	if flg:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [list(map(int, input())) for i in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] != 1 and a[i][j + 1] != 1:
					break
		else:
			continue
		break
	else:
		print('YES')
		continue
	print('NO')


for i in range(int(input())):
	n = int(input())
	poly = [[int(i) for i in input()] for i in range(n)]
	safe = [[0 for i in range(n)] for i in range(n)]
	yi = n - 1
	xi = n - 1
	nd = 0
	try:
		for i in range(2 * n - 1):
			if i > n - 1:
				nd += 2
			x = xi
			y = yi
			for i in range(i - nd + 1):
				if x == n - 1 or y == n - 1:
					safe[y][x] = poly[y][x]
				elif poly[y][x]:
					if not (safe[y + 1][x] or safe[y][x + 1]):
						raise Exception
					else:
						safe[y][x] = 1
				y -= 1
				x += 1
			if xi:
				xi -= 1
			else:
				yi -= 1
		print('YES')
	except Exception:
		print('NO')


import sys, random, bisect
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush
from math import gcd
mod = int(1000000000.0 + 7)
inf = int(1e+20)
input = lambda : sys.stdin.readline().rstrip()
mi = lambda : map(int, input().split())
li = lambda : list(mi())
ii = lambda : int(input())
py = lambda : print('YES')
pn = lambda : print('NO')
t = ii()
for _ in range(t):
	n = ii()
	mat = []
	for __ in range(n):
		mat.append(input())
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i + 1][j] != '1' and mat[i][j + 1] != '1':
					flag = False
					break
		if not flag:
			break
	if flag:
		py()
	else:
		pn()


import sys
input = sys.stdin.readline

def solve(n, m):
	for r in range(n):
		for c in range(n):
			if m[r][c] == '1':
				valid = False
				for (dr, dc) in [(r + 1, c), (r, c + 1)]:
					if 0 <= dr < n and 0 <= dc < n:
						if m[dr][dc] == '1':
							valid = True
					else:
						valid = True
				if not valid:
					return 'NO'
	return 'YES'
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	m = []
	for _ in range(n):
		m.append(input().rstrip())
	print(solve(n, m))


ans = []
t = int(input())
for _ in range(t):
	n = int(input())
	a = [input().strip() for _ in range(n)]
	possible = True
	for i in range(n):
		for j in range(n):
			if i + 1 < n and j + 1 < n and (a[i + 1][j] == '0') and (a[i][j + 1] == '0') and (a[i][j] == '1'):
				possible = False
	if possible:
		ans.append('YES')
	else:
		ans.append('NO')
print(*ans, sep='\n')


t = int(input())
for i in range(t):
	valid = True
	n = int(input())
	arr = []
	for j in range(n):
		line = input()
		inarr = [int(k) for k in line]
		arr.append(inarr)
	for row in range(len(arr)):
		for col in range(len(arr[row])):
			element = arr[row][col]
			if element == 1:
				if row == n - 1 or col == n - 1:
					pass
				elif arr[row][col + 1] == 0 and arr[row + 1][col] == 0:
					valid = False
			if not valid:
				break
		if not valid:
			break
	if valid:
		print('YES')
	else:
		print('NO')


import sys
import math
import collections
from collections import deque
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = []
	k = 0
	for i in range(n):
		a.append(str(stdin.readline().strip('\n')))
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i][j + 1] == '0' and (a[i + 1][j] == '0'):
				k = 1
				break
	if k == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
ans = []
for ti in range(t):
	a = []
	f = 0
	n = int(input())
	for i in range(n):
		a.append(list(map(int, input())))
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] != 1 and a[i][j + 1] != 1:
					f = 1
					break
	if f == 1:
		ans.append('NO')
	else:
		ans.append('YES')
for p in ans:
	print(p)


t = int(input())
import math
for i in range(t):
	n = int(input())
	mat = []
	k = 0
	for i in range(n):
		a = list(input())
		mat.append(a)
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if mat[i + 1][j] == '0' and mat[i][j + 1] == '0':
					print('NO')
					k = 1
					break
		if k == 1:
			break
	if k != 1:
		print('YES')


a = input()
for x in range(int(a)):
	b = input()
	list1 = [[1 for i in range(int(b) + 2)] for i in range(int(b) + 2)]
	i = 1
	j = 1
	list2 = []
	for x in range(int(b)):
		c = input()
		for y in range(len(c)):
			list1[i + x][j + y] = int(c[y])
			if c[y] == '1':
				list2.append([i + x, j + y])
	ans = 'YES'
	for i in list2:
		if list1[i[0]][i[1] + 1] != 1 and list1[i[0] + 1][i[1]] != 1:
			ans = 'NO'
	print(ans)


for u in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	f = False
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and l[i + 1][j] != '1' and (l[i][j + 1] != '1'):
				f = True
	if f == True:
		print('NO')
	else:
		print('YES')


import sys

def main():
	res = ''
	input = sys.stdin.readline
	print = sys.stdout.write
	t = int(input())
	for _ in range(t):
		n = int(input())
		mat = []
		for i in range(n):
			mat.append(input())
		t = True
		for i in range(n - 1):
			for j in range(n - 1):
				if mat[i][j] == '1' and mat[i + 1][j] != '1' and (mat[i][j + 1] != '1'):
					t = False
					break
		if t:
			res += 'YES\n'
		else:
			res += 'NO\n'
	print(res)
main()


T = int(input())
while T != 0:
	num = int(input())
	arr = []
	while num != 0:
		arap = list(map(int, input()))
		arr.append(arap)
		num -= 1
	ans = True
	for i in range(len(arr) - 1):
		for j in range(len(arr[i]) - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] != 1 and arr[i][j + 1] != 1:
					ans = False
					break
	if ans:
		print('YES')
	else:
		print('NO')
	T -= 1


t = int(input())
for p in range(t):
	n = int(input())
	mat = []
	flag = 0
	for i in range(n):
		temp = input()
		temp = list(temp)
		mat.append(temp)
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1' and mat[i][j + 1] == '0' and (mat[i + 1][j] == '0'):
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	x = 0
	matrix = []
	for i in range(n):
		string = input()
		matrix.append(string)
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == '1' and j != n - 1 and (i != n - 1):
				if matrix[i + 1][j] == '0' and matrix[i][j + 1] == '0':
					x = 1
					break
		if x == 1:
			break
	if x == 0:
		print('YES')
	else:
		print('NO')


for I in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		s = list(input())
		mat.append(s)
	result = 'YES'
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i != n - 1 and j != n - 1:
					if mat[i][j + 1] == '0' and mat[i + 1][j] == '0':
						result = 'NO'
	print(result)


for lo in range(int(input())):
	n = int(input())
	ls = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		st = input()
		for j in range(n):
			ls[i][j] = st[j]
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if ls[i][j] == '1' and (i != n - 1 and j != n - 1):
				if ls[i + 1][j] == '0' and ls[i][j + 1] == '0':
					ans = 'NO'
					break
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	pol = []
	for j in range(n):
		s = input()
		s = [s[z] for z in range(n)]
		pol.append(s)
	good = 1
	for x in range(n - 1):
		for y in range(n - 1):
			if pol[x][y + 1] == '0' and pol[x + 1][y] == '0' and (pol[x][y] == '1'):
				good = 0
	if good == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	dp = []
	for i in range(n):
		dp += [list(map(int, input()))]
	c = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if dp[i][j] == 1:
				if dp[i + 1][j] == 0 and dp[i][j + 1] == 0:
					c = 'NO'
					break
		if c == 'NO':
			break
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [list(input()) for _ in range(n)]
	c = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if s[i][j] == '1':
				if s[i + 1][j] == '0' and s[i][j + 1] == '0':
					c = 0
			if c == 0:
				break
		if c == 0:
			break
	print('YES' if c == 1 else 'NO')


def solve(grid):
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if grid[i][j] == '1' and grid[i + 1][j] == '0' and (grid[i][j + 1] == '0'):
				return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	grid = [input() for _ in range(n)]
	print(solve(grid))


def main():
	for t in range(int(input())):
		n = int(input())
		a = []
		for i in range(n):
			a.append([int(i) for i in input()])
		possible = True
		for i in range(n):
			for j in range(n):
				if a[i][j] == 1:
					if i == n - 1 and j == n - 1:
						break
					if i != n - 1 and j != n - 1:
						if a[i + 1][j] == 0 and a[i][j + 1] == 0:
							possible = False
							break
			if not possible:
				break
		if possible:
			print('YES')
		else:
			print('NO')
main()


from math import *
from collections import deque
from copy import deepcopy
import sys

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def multi():
	return map(int, input().split())

def strmulti():
	return map(str, inp().split())

def lis():
	return list(map(int, inp().split()))

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def stringlis():
	return list(map(str, inp().split()))

def out(var):
	sys.stdout.write(str(var))

def printlist(a):
	print(' '.join((str(a[i]) for i in range(len(a)))))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(inp()))
	flag = False
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1' and i != n - 1 and (j != n - 1):
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				else:
					flag = True
					break
		if flag:
			print('NO')
			break
	if not flag:
		print('YES')


def r(j, n):
	yield (j, j)
	for i in range(j + 1, n):
		yield (i, j)
		yield (j, i)
for _ in range(int(input())):
	n = int(input())
	s = [input() for i in range(n)]
	fault = False
	for i in range(n - 1):
		for j in range(n - 1):
			if s[i][j] == '1' and s[i + 1][j] != '1' and (s[i][j + 1] != '1'):
				fault = True
				break
		if fault:
			break
	if fault:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append([int(i) for i in input()])
	training = True
	for i in range(n - 1):
		for l in range(n - 1):
			if arr[i][l] == 1:
				if arr[i + 1][l] == 1 or arr[i][l + 1] == 1:
					pass
				else:
					training = False
	if training:
		print('YES')
	else:
		print('NO')


t = int(input())
answer = []
for kek in range(t):
	n = int(input())
	a = [list(map(int, input())) for i in range(n)]
	flag = True
	for x in range(n - 1):
		if flag == False:
			break
		for y in range(n - 1):
			if a[x][y] == 1:
				if a[x][y + 1] + a[x + 1][y] == 0:
					flag = False
					break
	if flag:
		answer.append('YES')
	else:
		answer.append('NO')
print(*answer, sep='\n')


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	gh = [0 for _ in range(n)]
	dp = [gh[:] for _ in range(n)]
	visited = [gh[:] for _ in range(n)]
	r = n
	c = n
	total = 0
	for rr in range(r):
		temparr = input()
		arr = []
		for i in temparr:
			i = int(i)
			arr.append(i)
			total += i
		dp[rr] = arr[:]
	numofvalid = 0
	curstack = []
	for i in range(n):
		if dp[i][n - 1] == 1 and visited[i][n - 1] == 0:
			curstack.append((i, n - 1))
			visited[i][n - 1] = 1
			numofvalid += 1
		if dp[n - 1][i] == 1 and visited[n - 1][i] == 0:
			curstack.append((n - 1, i))
			visited[n - 1][i] = 1
			numofvalid += 1
	while curstack:
		newone = curstack.pop()
		i = newone[0]
		j = newone[1]
		if i - 1 >= 0 and visited[i - 1][j] == 0 and (dp[i - 1][j] == 1):
			curstack.append((i - 1, j))
			numofvalid += 1
			visited[i - 1][j] = 1
		if j - 1 >= 0 and visited[i][j - 1] == 0 and (dp[i][j - 1] == 1):
			curstack.append((i, j - 1))
			numofvalid += 1
			visited[i][j - 1] = 1
	if numofvalid == total:
		print('YES')
	else:
		print('NO')


def solve(n, polygon):
	row = 0
	column = 0
	while row < n - 1:
		column = 0
		while column < n - 1:
			if polygon[row][column] == '1':
				if polygon[row + 1][column] == '0' and polygon[row][column + 1] == '0':
					return 'NO'
			column += 1
		row += 1
	return 'YES'
for x in range(int(input())):
	n = int(input())
	p = []
	for y in range(n):
		p.append(list(input()))
	print(solve(n, p))


from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread
srl(int(1000000000.0) + 7)
stack_size(int(100000000.0))

def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		l = [[0 for j in range(n)] for i in range(n)]
		for i in range(n):
			s = input()
			for j in range(n):
				l[i][j] = int(s[j])
		end = 0
		for i in range(n):
			for j in range(n):
				if l[i][j] == 1 and i < n - 1 and (j < n - 1) and (l[i + 1][j] == 0) and (l[i][j + 1] == 0):
					print('NO')
					end = 1
					break
			if end:
				break
		if end == 0:
			print('YES')
Thread(target=solve).start()


test = int(input())
while test:
	test -= 1
	n = int(input())
	arr = []
	for i in range(n):
		tmp = input()
		arr.append(tmp)
	i = 0
	break_loop = False
	while i < n - 1:
		j = 0
		while j < n - 1:
			if arr[i][j] == '1':
				if arr[i + 1][j] == '1' or arr[i][j + 1] == '1':
					pass
				else:
					break_loop = True
					break
			j += 1
		i += 1
		if break_loop:
			break
	if break_loop:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = [input() for i in range(n)]
	res = True
	for i in range(n - 1):
		for j in range(n - 1):
			if ord(a[i][j]) - 48:
				res &= ord(a[i + 1][j]) - 48 | ord(a[i][j + 1]) - 48
	print('NYOE S'[res::2])


import math
t = int(input())
for _ in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		temp = input()
		matrix.append(temp)
	flag = 0
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == '1':
				try:
					if matrix[i + 1][j] == '0' and matrix[i][j + 1] == '0':
						flag = 1
						print('NO')
						break
				except:
					pass
		if flag == 1:
			break
	if flag == 0:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	occur = True
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1' and l[i][j + 1] == '0' and (l[i + 1][j] == '0'):
				occur = False
				break
		if not occur:
			break
	if occur:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	polygon = []
	for _ in range(n):
		polygon.append(input())
	queue = []
	queueIndex = 0
	isAddedInQueue = []
	for i in range(n):
		isAddedInQueue.append([])
		for _ in range(n):
			isAddedInQueue[i].append(False)
	for i in range(n):
		if polygon[i][n - 1] == '1':
			queue.append((i, n - 1))
			isAddedInQueue[i][n - 1] = True
		if i != n - 1 and polygon[n - 1][i] == '1':
			queue.append((n - 1, i))
			isAddedInQueue[n - 1][i] = True
	while True:
		if queueIndex >= len(queue):
			break
		cur = queue[queueIndex]
		queueIndex += 1
		if cur[0] - 1 >= 0 and isAddedInQueue[cur[0] - 1][cur[1]] == False and (polygon[cur[0] - 1][cur[1]] == '1'):
			isAddedInQueue[cur[0] - 1][cur[1]] = True
			queue.append((cur[0] - 1, cur[1]))
		if cur[1] - 1 >= 0 and isAddedInQueue[cur[0]][cur[1] - 1] == False and (polygon[cur[0]][cur[1] - 1] == '1'):
			isAddedInQueue[cur[0]][cur[1] - 1] = True
			queue.append((cur[0], cur[1] - 1))
	training = True
	for i in range(n):
		for j in range(n):
			if isAddedInQueue[i][j] == False and polygon[i][j] == '1':
				training = False
				break
		if training == False:
			break
	if training:
		print('yes')
	else:
		print('no')


def go(x, y):
	if x - 1 >= 0:
		if a[x - 1][y] == '1':
			a[x - 1][y] = '0'
			go(x - 1, y)
	if y - 1 >= 0:
		if a[x][y - 1] == '1':
			a[x][y - 1] = '0'
			go(x, y - 1)
t = int(input())
for u in range(t):
	n = int(input())
	a = []
	for _ in range(n):
		a.append(list(input()))
	for i in range(n):
		if a[i][n - 1] == '1':
			a[i][n - 1] = '0'
			go(i, n - 1)
	for i in range(n):
		if a[n - 1][i] == '1':
			a[n - 1][i] = '0'
			go(n - 1, i)
	done = False
	for i in range(n - 1):
		if '1' in a[i]:
			print('NO')
			done = True
			break
	if not done:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	boo = False
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if i + 1 == n or j + 1 == n:
					continue
				elif l[i][j + 1] == '1' or l[i + 1][j] == '1':
					continue
				else:
					print('NO')
					boo = True
					break
		if boo:
			break
	if not boo:
		print('YES')


for case in range(int(input())):
	n = int(input())
	matrix = [0] * n
	flag = True
	for row in range(n):
		matrix[row] = input()
	for row in range(n - 1):
		for col in range(n - 1):
			if matrix[row][col] == '1' and matrix[row][col + 1] == '0' and (matrix[row + 1][col] == '0'):
				flag = False
	if flag:
		print('YES')
	else:
		print('NO')


def CanBe(board, row, col):
	if board[row + 1][col] == 1:
		return True
	if board[row][col + 1] == 1:
		return True
	return False

def Test():
	n = int(input())
	board = []
	for j in range(n):
		board.append([int(c) for c in input()])
	if n == 1:
		print('YES')
	else:
		for row in range(n - 2, -1, -1):
			for col in range(n - 2, -1, -1):
				if board[row][col] == 1:
					if CanBe(board, row, col):
						pass
					else:
						print('NO')
						return
		print('YES')
for i in range(int(input())):
	Test()


t = input()
t = int(t)
shetchikshetchikov = 0
shetchik123 = 0
gg = 0
while shetchikshetchikov < t:
	n = input()
	n = int(n)
	a = []
	shetchik123 = 0
	while shetchik123 < n:
		a.append(input())
		shetchik123 = shetchik123 + 1
	strochka = 0
	stolbets = 0
	gg = 0
	while strochka < n - 1:
		if gg == 1:
			break
		stolbets = 0
		while stolbets < n - 1:
			if a[strochka][stolbets] == '1':
				if a[strochka][stolbets + 1] == '0' and a[strochka + 1][stolbets] == '0':
					gg = 1
			stolbets = stolbets + 1
		strochka = strochka + 1
	shetchikshetchikov = shetchikshetchikov + 1
	if gg == 1:
		print('NO')
	else:
		print('YES')


N = int(input())
for i in range(N):
	n = int(input())
	L = []
	for j in range(n):
		s = list(input()) + ['1']
		L.append(s)
	L.append(['1'] * (n + 1))
	F = 0
	for i in range(n):
		for j in range(n):
			if L[i][j] == '1':
				if L[i + 1][j] == '1' or L[i][j + 1] == '1':
					continue
				else:
					F = 1
	if F == 0:
		print('Yes')
	else:
		print('NO')


def solve(n, mat):
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if mat[i][j] == 0:
				continue
			if i == n - 1 or j == n - 1:
				continue
			if mat[i + 1][j] == 1 or mat[i][j + 1] == 1:
				continue
			else:
				return 'no'
	return 'yes'
t = int(input())
for _ in range(t):
	inp = input().split()
	n = int(inp[0])
	mat = []
	for i in range(n):
		arr = [int(x) for x in input()]
		mat.append(arr)
	print(solve(n, mat))


import sys
from bisect import bisect
from math import sqrt, floor, factorial, gcd
from collections import deque, Counter, defaultdict
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def a():
	ans = ''
	for _ in range(int(inp())):
		(a, b) = read()
		i = 1
		while i < max(max(a, b), min(a, b) * 2) or i ** 2 < a * b * 2:
			i += 1
		ans += str(i ** 2) + '\n'
	print(ans)

def b():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		arr = sorted(read())
		x = 10 ** 9
		for i in range(n - 1):
			x = min(x, abs(arr[i] - arr[i + 1]))
		ans += str(x) + '\n'
	print(ans)

def c():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		arr = read()
		odd_set = []
		even_set = []
		for i in arr:
			if i % 2:
				odd_set.append(i)
			else:
				even_set.append(i)
		if len(even_set) % 2 == 0 and len(odd_set) % 2 == 0:
			ans += 'YES\n'
			continue
		elif len(even_set) % 2 == 1 and len(odd_set) % 2 == 1:
			flag = 0
			for i in odd_set:
				if i + 1 in even_set or i - 1 in even_set:
					ans += 'YES\n'
					flag = 1
					break
			if flag:
				continue
			else:
				ans += 'NO\n'
		else:
			ans += 'NO\n'
	print(ans)

def printDivisors(n):
	ans = []
	i = 1
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				ans.append(i)
			else:
				ans.extend([i, n // i])
		i = i + 1
	return sorted(ans)

def d():
	ans = ''
	for _ in range(int(inp())):
		(n, k) = read()
		if n <= k:
			ans += str(1) + '\n'
			continue
		divs = printDivisors(n)
		ind = bisect(divs, k)
		ans += str(n // divs[ind - 1]) + '\n'
	print(ans)

def e():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		mat = []
		for i in range(n):
			mat.append(input().strip())
		flag2 = 0
		for i in range(n):
			flag = 0
			for j in range(n):
				if mat[i][j] == '1':
					if i < n - 1 and j < n - 1:
						if mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
							pass
						else:
							flag = 1
							break
			if flag:
				flag2 = 1
				break
		ans += 'YNEOS'[flag2::2] + '\n'
	print(ans)
e()


from sys import stdin, stdout

def input():
	return stdin.readline()
for _ in range(int(input())):
	n = int(input())
	row = []
	row.append(['0'] * (n + 2))
	for i in range(n):
		c = input().split()
		m = ['0']
		for j in range(n):
			m.append(c[0][j])
		m.append('1')
		row.append(m)
	row.append(['1'] * (n + 2))
	f = 1
	for i in range(1, n):
		for j in range(1, n):
			if row[i][j] == '1' and (row[i][j + 1] == '0' and row[i + 1][j] == '0'):
				f = 0
				print('NO')
				break
		if f == 0:
			break
	if f == 1:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = [input() for _ in range(n)]
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] != '1' and a[i][j + 1] != '1':
					ans = False
					break
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import math

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		arr = []
		for _ in range(n):
			arr.append(stdin.readline().rstrip())
		is_ok = True
		for i in range(n):
			for j in range(n):
				if arr[i][j] == '1':
					if i + 1 == n or j + 1 == n:
						continue
					if arr[i + 1][j] == '1':
						continue
					if arr[i][j + 1] == '1':
						continue
					is_ok = False
					break
			if not is_ok:
				break
		if is_ok:
			stdout.write('YES\n')
		else:
			stdout.write('NO\n')
main()


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(0, n):
		a = input()
		a = a.rstrip()
		a = list(a)
		l.append(a)
	if n == 1:
		print('YES')
		continue
	flag = 0
	(i, j) = (n - 2, n - 2)
	while i > 0 and j > 0:
		for k in range(j, -1, -1):
			if l[i][k] == '1':
				if l[i][k + 1] == '1' or l[i + 1][k] == '1':
					continue
				else:
					flag = 1
					break
		if flag == 1:
			break
		for k in range(i, -1, -1):
			if l[k][j] == '1':
				if l[k + 1][j] == '1' or l[k][j + 1] == '1':
					continue
				else:
					flag = 1
					break
		i -= 1
		j -= 1
	if l[0][0] == '1':
		if l[0][1] == '0' and l[1][0] == '0':
			flag = 1
	if flag == 0:
		print('YES')
	elif flag == 1:
		print('NO')


T = int(input())

def solve(arr, n):
	for i in range(0, n):
		for j in range(0, n):
			el = arr[i][j]
			if el == 1:
				if i != n - 1 and j != n - 1:
					if arr[i + 1][j] == 0 and arr[i][j + 1] == 0:
						return 0
	return 1
while T > 0:
	n = int(input())
	arr = []
	for i in range(0, n):
		row = []
		line = input()
		for el in line:
			row.append(int(el))
		arr.append(row)
	T -= 1
	if solve(arr, n) == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = []
	for __ in range(n):
		a.append(input())
	res = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					res = False
					break
		if not res:
			break
	if res:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	matrix = []
	is_ = True
	for j in range(n):
		matrix.append(list(input()))
	for row in range(len(matrix)):
		for col in range(len(matrix[0])):
			if row == n - 1 or col == n - 1:
				continue
			elif matrix[row][col] == '1':
				if matrix[row][col + 1] != '1' and matrix[row + 1][col] != '1' and is_:
					print('NO')
					is_ = False
					break
	if is_:
		print('YES')


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline().rstrip()
res = ['YES', 'NO']
test_case = ii()
while test_case:
	test_case -= 1
	n = ii()
	a = []
	for _ in range(n):
		a.append(lsi())
	f = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				f = 1
				break
		if f:
			break
	print(res[f])


def solve():
	n = int(input())
	mtr = [input() for _ in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			if mtr[i][j] == '1':
				if mtr[i][j + 1] == '1' or mtr[i + 1][j] == '1':
					continue
				else:
					print('NO')
					return
	print('YES')
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for _ in range(0, t):
	n = int(input())
	arr = []
	for _ in range(0, n):
		arr.append(input())
	grid = []
	for i in range(0, len(arr)):
		temp = arr[i]
		asd = []
		for j in range(0, len(temp)):
			asd.append(temp[j])
		grid.append(asd)
	ans = True
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if grid[i][j] == '1' and grid[i + 1][j] == '0' and (grid[i][j + 1] == '0'):
				ans = False
				break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for q in range(t):
	n = int(input())
	matrix = []
	for k in range(n):
		linha = input()
		matrix.append(linha)
	flag1 = False
	flag2 = False
	for i in range(n):
		for j in range(n):
			if j + 1 <= n - 1:
				if matrix[i][j] == '1' and matrix[i][j + 1] == '0':
					flag1 = True
			if i + 1 <= n - 1:
				if matrix[i][j] == '1' and matrix[i + 1][j] == '0':
					flag2 = True
			if flag1 == True and flag2 == True:
				break
			else:
				flag1 = False
				flag2 = False
		if flag1 == True and flag2 == True:
			break
	if flag1 == True and flag2 == True:
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(input()))
	for y in range(n - 1):
		for x in range(n - 1):
			if a[y][x] == '1' and a[y + 1][x] == '0' and (a[y][x + 1] == '0'):
				print('NO')
				break
		else:
			continue
		break
	else:
		print('YES')


def tc():
	n = int(input())
	m = [[int(c) for c in input()] for _ in range(n)]
	for i in range(len(m) - 1):
		for j in range(len(m[0]) - 1):
			if m[i][j] and (not m[i + 1][j]) and (not m[i][j + 1]):
				print('NO')
				return
	print('YES')
t = int(input())
for _ in range(t):
	tc()


t = int(input())

def solution(n, matrix):
	for r in range(n - 2, -1, -1):
		for c in range(n - 2, -1, -1):
			if matrix[r][c] == 1 and (matrix[r + 1][c] == 0 and matrix[r][c + 1] == 0):
				return 'NO'
	return 'YES'
for _ in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append([int(x) for x in list(input())])
	print(solution(n, matrix))


t = int(input())
for j in range(t):
	ans = True
	lista = []
	n = int(input())
	for k in range(n):
		lista.append(input())
	for i in range(len(lista[0]) - 1):
		if lista[0][i] == '1':
			if not (lista[1][i] == '1' or lista[0][i + 1] == '1'):
				ans = False
	for k in range(1, n - 1):
		for i in range(0, n - 1):
			if lista[k][i] == '1':
				if not (lista[k + 1][i] == '1' or lista[k][i + 1] == '1'):
					ans = False
	if ans:
		print('YES')
	else:
		print('NO')


import sys
from math import floor, sqrt, ceil, factorial, gcd, pi
from itertools import chain, combinations, permutations, accumulate
from collections import Counter, defaultdict, OrderedDict
from array import array
INT_MAX = sys.maxsize
INT_MIN = -sys.maxsize - 1
mod = 1000000007

def solve():
	t = int(sys.stdin.readline())
	for _ in range(t):
		n = int(sys.stdin.readline())
		a = [[] for i in range(n)]
		for i in range(n):
			x = [int(i) for i in input()]
			a[i].extend(x)
		one = set()
		for i in range(n):
			for j in range(n):
				if a[i][j] == 1:
					one.add((i, j))
		check = set()
		for i in range(n - 1):
			for j in range(n - 1):
				if a[i][j] == 1:
					if a[i + 1][j] == 1 or a[i][j + 1] == 1:
						check.add((i, j))
		for i in range(n):
			if a[n - 1][i] == 1:
				check.add((n - 1, i))
		for j in range(n):
			if a[j][n - 1] == 1:
				check.add((j, n - 1))
		print('YES' if one == check else 'NO')
solve()


T = int(input())
for _ in range(T):
	N = int(input())
	grid = []
	for _ in range(N):
		line = input()
		grid += (list(line),)
	ans = 'YES'
	for y in range(N - 1):
		for x in range(N - 1):
			if grid[y][x] == '1' and grid[y + 1][x] == '0' and (grid[y][x + 1] == '0'):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)


import math, sys, bisect
from collections import defaultdict, Counter, deque

def input():
	return sys.stdin.readline().strip()
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])
for _ in range(int(input())):
	N = int(input())
	A = []
	for i in range(N):
		B = input()
		A.append(B)
	f = 1
	for i in range(N - 1):
		for j in range(N - 1):
			if A[i][j] == '1' and A[i + 1][j] == '0' and (A[i][j + 1] == '0'):
				f = 0
				break
		if f == 0:
			break
	Y(f)


t = int(input())
for k in range(t):
	n = int(input())
	field = []
	possible = True
	for i in range(n):
		row = [i for i in input()]
		field.append(row)

	def isSurrounded(row, col):
		if col + 1 == n or row + 1 == n:
			return True
		elif field[row][col + 1] == '1' or field[row + 1][col] == '1':
			return True
		else:
			return False
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if field[i][j] == '1':
				if isSurrounded(i, j) == False:
					possible = False
					break
		if possible == False:
			break
	if possible:
		print('YES')
	else:
		print('NO')


import math
from math import gcd
t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	a = True
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if (j + 1 < n and l[i][j + 1] == '0') and (i + 1 < n and l[i + 1][j] == '0'):
					print('NO')
					a = False
					break
		if a == False:
			break
	if a == True:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	b = []
	r = 0
	for i in range(n):
		a = list(input())
		b.append(a)
	for i in range(len(b)):
		for j in range(len(b)):
			if b[i][j] == '1':
				t = 0
				p = 0
				if i < n - 1 and j < n - 1:
					if b[i + 1][j] == '1':
						t += 1
					else:
						t = 0
					if b[i][j + 1] == '1':
						p += 1
					else:
						p = 0
					if p == 0 and t == 0:
						r += 1
						break
	if r > 0:
		print('NO')
	else:
		print('YES')


import sys
import math
from collections import deque
read = sys.stdin.buffer.read
readlines = sys.stdin.buffer.readlines
t = int(input())
for case in range(t):
	n = int(input())
	a = [['-1'] * n for _ in range(n)]
	for i in range(n):
		s = input()
		for j in range(n):
			a[i][j] = s[j]
	q = deque()
	for i in range(n):
		if a[n - 1][i] == '1':
			q.append([n - 1, i])
		if a[i][n - 1] == '1' and i != n - 1:
			q.append([i, n - 1])
	while q:
		pos = q.popleft()
		a[pos[0]][pos[1]] = '2'
		if pos[0] - 1 >= 0 and a[pos[0] - 1][pos[1]] == '1':
			a[pos[0] - 1][pos[1]] = '2'
			q.append([pos[0] - 1, pos[1]])
		if pos[1] - 1 >= 0 and a[pos[0]][pos[1] - 1] == '1':
			a[pos[0]][pos[1] - 1] = '2'
			q.append([pos[0], pos[1] - 1])
	ok = 1
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				ok = 0
	if ok:
		print('YES')
	else:
		print('NO')


t = int(input())
for test in range(t):
	n = int(input())
	m = []
	for _ in range(n):
		m.append(input())
	isPossible = True
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and m[i + 1][j] == '0' and (m[i][j + 1] == '0'):
				isPossible = False
				break
		if not isPossible:
			break
	if isPossible:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	matrix = []
	for _ in range(n):
		matrix.append(list(input()))
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1':
				if matrix[i + 1][j] == '1' or matrix[i][j + 1] == '1':
					continue
				else:
					flag = False
					break
	if flag:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for q in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input() + '1'))
	l.append(['1'] * (n + 1))
	for i in range(n):
		boo = True
		for j in range(n):
			if l[i][j] == '1' and l[i + 1][j] != '1' and (l[i][j + 1] != '1'):
				boo = False
				break
		if boo == False:
			break
	print('YES' if boo else 'NO')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	board = []
	for _ in range(n):
		a = list(input())
		board.append(a)
	result = 'YES'
	for y in range(n - 1):
		for x in range(n - 1):
			if board[y][x] == '1':
				if board[y + 1][x] == '0' and board[y][x + 1] == '0':
					result = 'NO'
					break
		if result == 'NO':
			break
	print(result)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')

def main():
	for _ in range(iin()):
		n = iin()
		l = []
		for __ in range(n):
			l.append(list(sin()))
		flag = 0
		for i in range(n - 1):
			for j in range(n - 1):
				if l[i][j] == '1':
					if l[i][j + 1] == '0' and l[i + 1][j] == '0':
						flag = 1
			if flag == 1:
				break
		if flag == 0:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


class Node:

	def __init__(self, i, j, state, n) -> None:
		self.i = i
		self.j = j
		self.state = state
		self.childs = set()
		self.is_border = i + 1 == n or j + 1 == n

	def __repr__(self) -> str:
		return f'Node(i={self.i}; j={self.j}; state={self.state})'

def solution():
	n = int(input())
	nodes = {(i, j): Node(i, j, name, n) for i in range(n) for (j, name) in enumerate(input())}
	for i in range(n):
		for j in range(n - 1):
			nodes[i, j].childs.add(nodes[i, j + 1])
	for i in range(n - 1):
		for j in range(n):
			nodes[i, j].childs.add(nodes[i + 1, j])
	nodes = nodes.values()
	for node in filter(lambda n: n.state == '1', nodes):
		if can_travel(node):
			continue
		else:
			print('NO')
			break
	else:
		print('YES')

def can_travel(node: Node, visited={}):
	if node.state == '0':
		return False
	elif node.state == '1':
		if node.is_border:
			return True
		childs = list(filter(lambda child: child.state == '1', node.childs))
		if len(childs) == 0:
			return False
		can = False
		for child in childs:
			if child not in visited:
				visited[child] = can_travel(child, visited)
			can = visited[child]
		return can
	else:
		raise RuntimeError('undefined state')
t = int(input())
for _ in range(t):
	solution()


t = int(input())
for i in range(t):
	r = True
	A = []
	n = int(input())
	for j in range(n):
		A.append(list(map(int, input())))
	for i in range(n):
		for j in range(n):
			if j == n - 1 or i == n - 1:
				continue
			if A[i][j] == 1:
				if A[i + 1][j] != 1 and A[i][j + 1] != 1:
					r = False
					break
		if not r:
			break
	if r:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		l.append(list(input()))
	flag = 0
	for x in range(n - 1):
		for y in range(n - 1):
			if l[x][y] == '1' and l[x + 1][y] == '0' and (l[x][y + 1] == '0'):
				flag = 1
				break
		if flag == 1:
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


def polygons(n, m):
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1':
				if j == n - 1 or i == n - 1 or m[i + 1][j] == '1' or (m[i][j + 1] == '1'):
					ans = 'YES'
				else:
					return 'NO'
	return ans
t = int(input())
a = []
for i in range(t):
	n = int(input())
	m = []
	for j in range(n):
		m.append(input())
	a.append([n, m])
for x in a:
	print(polygons(*x))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	r = 0
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '1':
				if i == n - 1 or j == n - 1:
					pass
				elif arr[i + 1][j] == '1' or arr[i][j + 1] == '1':
					pass
				else:
					r = 1
					break
		if r == 1:
			break
	if r == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for ts in range(t):
	n = int(input())
	grid = []
	for rows in range(n):
		grid.append([int(i) for i in input()] + [1])
	grid.append([1] * (n + 1))
	res = 'YES'
	for i in range(n):
		for j in range(n):
			if grid[i][j] == 1:
				if grid[i + 1][j] != 1 and grid[i][j + 1] != 1:
					res = 'NO'
					break
	print(res)


import math
for _ in range(int(input())):
	n = int(input())
	m = []
	for i in range(n):
		a = input()
		m.append(a)
	f = True
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and m[i + 1][j] == '0' and (m[i][j + 1] == '0'):
				f = False
				break
		if not f:
			break
	if not f:
		print('no')
	else:
		print('yes')


for t in range(int(input())):
	a = int(input())
	b = [list(input()) for i in range(a)]
	s = 0
	for i in range(a - 1):
		for j in range(a - 1):
			if b[i][j] == '1' and b[i + 1][j] == '0' and (b[i][j + 1] == '0'):
				print('NO')
				s = 1
				break
		if s == 1:
			break
	else:
		print('YES')


import sys
import math
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
for t in range(int(input())):
	n = int(input())
	arr = []
	ans = True
	for i in range(n):
		arr.append(input())
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '0':
				continue
			if j == n - 1:
				continue
			if i == n - 1:
				continue
			if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
				ans = False
	if ans:
		print('Yes')
	else:
		print('no')


import sys

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	a = []
	d = 0
	for i in range(n):
		s = input()
		b = [int(x) for x in s]
		a.append(b)
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] == 1 or a[i][j + 1] == 1:
					continue
				else:
					d += 1
	if d > 0:
		print('NO')
		continue
	if d == 0:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = [None] * n
	for j in range(n):
		s = input()
		a[j] = s
	flag = False
	for j in range(n - 1):
		for k in range(n - 1):
			if a[j][k] == '1' and a[j + 1][k] == a[j][k + 1] == '0':
				flag = True
				break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for gf in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	flag = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '0' and a[i][j + 1] == '0':
					flag = False
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for x in range(t):
	yes = True
	n = int(input())
	big = [['1'] * (n + 2)]
	for y in range(n):
		big.append(list('1' + input() + '1'))
	big.append(['1'] * (n + 2))
	for z in range(1, len(big) - 1):
		for a in range(1, len(big[z]) - 1):
			if int(''.join([big[z][a], big[z][a + 1], big[z + 1][a]])) == 100:
				print('NO')
				yes = False
				break
		if yes == False:
			break
	if yes:
		print('YES')


T = int(input())
for t in range(T):
	N = int(input())
	matrix = [input() for i in range(N)]
	res = 'YES'
	try:
		for i in range(N - 1):
			for j in range(N - 1):
				if matrix[i][j] == '1':
					t = matrix[i + 1][j] == '1'
					t |= matrix[i][j + 1] == '1'
					assert t
	except Exception:
		res = 'NO'
	print(res)


T = int(input())
for t in range(T):
	n = int(input())
	M = []
	row = 0
	while row < n:
		data = input().rstrip()
		temp = []
		i = 0
		while i < n:
			temp.append(int(data[i]))
			i += 1
		M.append(temp)
		row += 1
	exit = False
	row = 0
	while row < n:
		col = 0
		while col < n:
			if M[row][col] == 1:
				if col + 1 != n and row + 1 != n:
					if M[row + 1][col] != 1 and M[row][col + 1] != 1:
						print('NO')
						exit = True
						break
				M[row][col] = 0
			col += 1
		if exit:
			break
		row += 1
	if not exit:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	s = [input() for _ in range(n)]
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if s[i][j] == '0':
				continue
			if not (s[i + 1][j] == '1' or s[i][j + 1] == '1'):
				print('NO')
				break
		else:
			continue
		break
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for j in range(n):
		s = list(input())
		arr.append(list(map(int, s)))
	check = 0
	for j in range(n - 2, -1, -1):
		for k in range(n - 2, -1, -1):
			if arr[j][k] == 1 and arr[j + 1][k] == 0 and (arr[j][k + 1] == 0):
				check = 1
				break
		if check == 1:
			break
	if check == 1:
		print('NO')
	else:
		print('YES')


from collections import deque
T = int(input())
for _ in range(T):
	n = int(input())
	arr = []
	for _ in range(n):
		arr.append(list(map(int, list(input()))))
	vis = [[False for _ in range(n)] for _ in range(n)]
	q = deque()
	for i in range(n):
		if arr[n - 1][i] == 1:
			q.append((n - 1, i))
		if arr[i][n - 1] == 1:
			q.append((i, n - 1))
	while len(q) > 0:
		(i, j) = q.popleft()
		if i < 0 or j < 0 or arr[i][j] != 1 or vis[i][j]:
			continue
		vis[i][j] = True
		q.append((i - 1, j))
		q.append((i, j - 1))
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1 and (not vis[i][j]):
				ans = 'NO'
	print(ans)


def solve():
	n = int(input())
	li = []
	for i in range(n):
		li.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if li[i][j] == '1' and (not (li[i + 1][j] == '1' or li[i][j + 1] == '1')):
				print('NO')
				return
	else:
		print('YES')
		return
t = int(input())
for i in range(t):
	solve()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		cands = set()
		prev_cands = set()
		good = True
		for i in range(n):
			row = input()
			if not good:
				continue
			for c in range(n - 1):
				if row[c] == '1':
					if row[c + 1] == '0':
						cands.add(c)
				elif c in prev_cands:
					good = False
					break
			prev_cands = cands
			cands = set()
		if good:
			print('YES')
		else:
			print('NO')
main()


def solution():
	n = int(input())
	mmap = []
	for i in range(n):
		mmap.append(input())
	for i in range(n):
		for j in range(n):
			if mmap[i][j] == '1':
				has_right = j + 1 == n or (j + 1 < n and mmap[i][j + 1] == '1')
				has_bottom = i + 1 == n or (i + 1 < n and mmap[i + 1][j] == '1')
				if has_bottom or has_right:
					pass
				else:
					print('NO')
					return
	print('YES')
t = int(input())
for _ in range(t):
	solution()


import sys, math
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = [str(input()) for i in range(n)]
	f = True
	for i in range(n):
		if f == False:
			break
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				f1 = f2 = False
				for k1 in range(i + 1, n):
					if a[k1][j] == '1':
						f1 = True
					break
				for k2 in range(j + 1, n):
					if a[i][k2] == '1':
						f2 = True
					break
				if f1 == f2 == False:
					f = False
					break
	print('YES' if f == True else 'NO')


t = int(input())
for tt in range(t):
	n = int(input())
	a = [str(input()) for i in range(n)]
	b = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and a[i + 1][j] == '0' and (a[i][j + 1] == '0'):
				b = 0
	if b == 1:
		print('YES')
	else:
		print('NO')


from collections import deque
from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = [list(input().strip()) for i in range(n)]
	can = True
	for i in range(n):
		if not can:
			break
		for g in range(n):
			if arr[i][g] != '1':
				continue
			if not (i + 1 == n or g + 1 == n or arr[i + 1][g] == '1' or (arr[i][g + 1] == '1')):
				can = False
				break
	print('YES' if can else 'NO')


from collections import deque
D = [(0, -1), (-1, 0)]

def bfs(que, A, N):
	while que:
		(x, y) = que.popleft()
		for (u, v) in D:
			p = x + u
			q = y + v
			if not (0 <= p <= N - 1 and 0 <= q <= N - 1):
				continue
			if A[x][y] == 2 and A[p][q] == 1:
				A[p][q] = 2
				que.append((p, q))
	for x in range(N):
		for y in range(N):
			if A[x][y] == 1:
				return False
	return True

def check(A, N):
	que = deque()
	for x in range(N):
		if A[x][N - 1] == 1:
			que.append((x, N - 1))
			A[x][N - 1] = 2
	for y in range(N):
		if A[N - 1][y] == 1:
			que.append((N - 1, y))
			A[N - 1][y] = 2
	return bfs(que, A, N)
for i in range(int(input())):
	N = int(input())
	A = [[] for _ in range(N)]
	for j in range(N):
		for s in input():
			A[j].append(int(s))
	if check(A, N):
		print('YES')
	else:
		print('NO')


def solution():
	for _ in range(int(input())):
		n = int(input())
		l = []
		for i in range(n):
			x = input()
			x += '1'
			l.append(list(x))
		x = '1' * n
		l.append(list(x))
		flag = 0
		for i in range(n):
			for j in range(n):
				if l[i][j] == '1':
					if l[i + 1][j] == '0' and l[i][j + 1] == '0':
						flag = 1
		if flag == 0:
			print('YES')
		else:
			print('NO')
solution()


t = int(input())
for ti in range(t):
	n = int(input())
	a = []
	for ni in range(n):
		a.append([int(j) for j in list(input())])
	solve = 0
	for i in range(n):
		stop = 0
		for j in range(n):
			if a[i][j] == 1:
				if i != n - 1 and j != n - 1 and (a[i + 1][j] != 1) and (a[i][j + 1] != 1):
					stop = 1
					solve = 1
					break
		if stop == 1:
			break
	if solve == 1:
		print('NO')
	else:
		print('YES')


def check_for_root(x, y, tab, n, old_check):
	if str([x, y]) in old_check:
		return old_check[str([x, y])]
	if (x == n - 1 or y == n - 1) and tab[x][y] == '1':
		old_check[str([x, y])] = True
		return True
	if tab[x][y] == '0':
		old_check[str([x, y])] = False
		return False
	check_from_left = check_for_root(x + 1, y, tab, n, old_check)
	check_from_top = check_for_root(x, y + 1, tab, n, old_check)
	if check_from_left or check_from_top:
		old_check[str([x, y])] = True
		return True
	old_check[str([x, y])] = False
	return False
t = int(input())
answer = []
for _ in range(t):
	old_check = {}
	check = True
	tab = []
	n = int(input())
	for _ in range(n):
		line = input()
		tab.append(line)
	for i in range(n):
		for j in range(n):
			if tab[i][j] == '1':
				check = check_for_root(i, j, tab, n, old_check)
				if check == False:
					break
		if check == False:
			break
	answer.append('YES' if check else 'NO')
print('\n'.join(answer))


def solve(row):
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if row[i][j] == '1' and row[i][j + 1] != '1' and (row[i + 1][j] != '1'):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)
for i in range(int(input())):
	n = int(input())
	row = [input() for i in range(n)]
	solve(row)


tc = int(input())
for _ in range(tc):
	n = int(input())
	g = []
	a = 'YES'
	for i in range(n):
		s = input()
		g.append(s)
	for i in range(n - 1):
		for j in range(n - 1):
			if g[i + 1][j] != '1' and g[i][j + 1] != '1' and (g[i][j] == '1'):
				print('NO')
				a = 'NO'
				break
		if a == 'NO':
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = []
	for _ in range(n):
		a = list(map(int, list(input())))
		l.append(a)
	flag = 0
	for i in range(n - 1):
		if flag == 1:
			break
		for j in range(n - 1):
			if l[i][j] == 1 and l[i + 1][j] == 0 and (l[i][j + 1] == 0):
				flag = 1
				print('NO')
				break
	if flag == 0:
		print('YES')


testing_mode = True
import sys, traceback
test_data = ''
final_output = ''

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for x in range(multi)]
	else:
		next_line = next(test_data, None).strip() if test_data else sys.stdin.readline()
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = next_line.split()
		elif input_format == 4:
			formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
		elif input_format == 5:
			formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
		else:
			formatted_input = [next_line]
		return formatted_input

def log(*args):
	if testing_mode:
		print(*args)

def out(output_line, output_format=0, newline=True):
	global final_output
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 1:
		formatted_output = '\n'.join(map(str, output_line))
	if testing_mode:
		final_output += formatted_output + '\n' * newline
	else:
		print(formatted_output, end='\n' if newline else '')
test_inputs = ['\n5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111\n\n', '\n$$$\n', '\n$$$\n', '\n$$$\n']
test_outputs = ['\nYES\nNO\nYES\nYES\nNO\n\n', '\n$$$\n', '\n$$$\n', '\n$$$\n']

def solve():
	for z in range(input(1)[0]):
		n = input(1)[0]
		m = [[int(j) for j in x[0]] for x in input(3, n)]
		log(m)
		valid = True
		for i in range(n - 1):
			if not valid:
				break
			for j in range(n - 1):
				if m[i][j] and (not (m[i][j + 1] or m[i + 1][j])):
					valid = False
					break
		out('YES' if valid else 'NO')
	return

def run_all(overwrite_testing=False):
	global testing_mode, final_output, test_data
	testing_mode = overwrite_testing and testing_mode
	if testing_mode:
		tests_failed = 0
		for (test_number, (test_in, test_out)) in enumerate(zip(test_inputs, test_outputs)):
			final_output = ''
			if test_in.lstrip()[:3] == '$$$':
				continue
			test_data = iter([i for i in test_in.split('\n') if i])
			print(f'Test Case #{test_number + 1}:')
			print('<' * 20)
			fail = False
			verdict = '\x1b[92m\x1b[1mPASS\x1b[0m'
			try:
				solve()
				final = final_output.rstrip('\n')
				raw_out = iter(map(str, final.split('\n'))) if final else iter(())
			except Exception:
				raw_out = ''
				fail = True
				print('\x1b[95m')
				traceback.print_exc()
				print('\x1b[0m')
			if raw_out:
				if test_out.lstrip()[:3] == '$$$':
					verdict = '\x1b[94m\x1b[1mSKIP\x1b[0m'
					print('\x1b[94m' + '\n'.join(list(raw_out)) + '\x1b[0m')
				else:
					for line_out in [i for i in test_out.split('\n') if i]:
						line_raw = next(raw_out, '% None %')
						if line_raw == line_out.strip():
							print(f'\x1b[92m{line_raw}\x1b[0m')
						else:
							print(f'\x1b[91m{line_raw}\x1b[0m')
							fail = True
					line_raw = next(raw_out, None)
					while line_raw:
						print(f'\x1b[91m{line_raw}\x1b[0m')
						line_raw = next(raw_out, None)
						fail = True
			print('>' * 20)
			if fail:
				verdict = '\x1b[91m\x1b[1mFAIL\x1b[0m'
				tests_failed += 1
			print(verdict + '\n')
		if tests_failed:
			print(f"\x1b[91m\x1b[1m{tests_failed} TEST{('S' if tests_failed > 1 else '')} FAILED!\x1b[0m")
		else:
			print(f'\x1b[92m\x1b[1mALL TESTS PASSED!\x1b[0m')
	else:
		test_data = False
		solve()
run_all(False)


t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for _ in range(n):
		mat.append(input())
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '0':
				continue
			if mat[i + 1][j] == '0' and mat[i][j + 1] == '0':
				ans = 'NO'
	print(ans)


from sys import stdin, stdout

def II():
	return int(stdin.readline())

def SI():
	return stdin.readline()[:-1]
t = II()
for _ in range(t):
	n = II()
	s = []
	for i in range(n):
		si = SI()
		s.append(si)
	ans = 'YES'
	for r in range(n - 1):
		for c in range(n - 1):
			if s[r][c] == '1' and (not (s[r + 1][c] == '1' or s[r][c + 1] == '1')):
				ans = 'NO'
				break
	stdout.write(str(ans) + '\n')


def valid_matrix(an, amatrix):
	for i in range(an):
		for j in range(an):
			if amatrix[i][j] == 1:
				if i < an - 1 and amatrix[i + 1][j] == 0:
					if j < an - 1 and amatrix[i][j + 1] == 0:
						return False
	return True
num_of_cases = int(input())
for cas in range(num_of_cases):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(list(map(int, input())))
	if valid_matrix(n, matrix):
		print('YES')
	else:
		print('NO')


n = int(input())
for x in range(n):
	nn = int(input())
	A = []
	for xx in range(nn):
		A.append(list(map(int, input())))
	FALSE = 0
	for (i, arr) in enumerate(A):
		for (ii, num) in enumerate(arr):
			if num == 1:
				if ii + 1 == len(arr) or arr[ii + 1] == 1:
					pass
				elif i + 1 == len(arr) or A[i + 1][ii] == 1:
					pass
				else:
					FALSE = 1
					break
	if FALSE == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	grid = [input() for _ in range(n)]
	done = False
	for i in range(n):
		for j in range(n):
			right = grid[i][j + 1] if j < n - 1 else '1'
			bot = grid[i + 1][j] if i < n - 1 else '1'
			if grid[i][j] == '1' and right != '1' and (bot != '1'):
				done = True
				break
	if not done:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import collections
import heapq
import itertools
import functools
import math
import operator

def read_line():
	return stdin.readline().strip()

def read_int():
	return int(read_line())

def read_arr():
	return read_line().split(' ')

def read_int_arr():
	return [int(x) for x in read_arr()]

def query_interactive(s):
	print(s)
	stdout.flush()

def main():
	T = 1
	T = read_int()
	for _ in range(T):
		N = read_int()
		mat = []
		for n in range(N):
			mat.append(read_line())
		dp = [[False] * N for _ in range(N)]
		for r in range(N):
			if mat[r][N - 1] == '1':
				dp[r][N - 1] = True
		for c in range(N):
			if mat[N - 1][c] == '1':
				dp[N - 1][c] = True
		for r in reversed(range(N - 1)):
			for c in reversed(range(N - 1)):
				if mat[r][c] == '0':
					dp[r][c] = False
				else:
					dp[r][c] = dp[r + 1][c] or dp[r][c + 1]
		ans = True
		for r in range(N):
			for c in range(N):
				if mat[r][c] == '1' and (not dp[r][c]):
					ans = False
		print('YES' if ans else 'NO')
main()


t = int(input())
for __ in range(t):
	n = int(input())
	rows = []
	for row_num in range(n):
		rows.append(input())

	def check_possible(rows):
		for i in range(n):
			for j in range(n):
				if i == n - 1 or j == n - 1:
					continue
				if rows[i][j] == '1' and rows[i + 1][j] == '0' and (rows[i][j + 1] == '0'):
					return 'NO'
		return 'YES'
	print(check_possible(rows))


def solve():
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input()))
	flag = 0
	for i in range(n):
		for j in range(n):
			c = 0
			if l[i][j] == '1':
				if j < n - 1:
					if l[i][j + 1] != '1':
						c += 1
				if i < n - 1:
					if l[i + 1][j] != '1':
						c += 1
			if c == 2:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')
t = 1
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append([int(ch) for ch in input()])
	dp = []
	for i in range(n):
		dp.append([False] * n)
	for i in range(n):
		if mat[i][n - 1] == 1:
			dp[i][n - 1] = True
		if mat[n - 1][i] == 1:
			dp[n - 1][i] = True
	x = 1
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if mat[i][j] == 1:
				if dp[i + 1][j] == 1 or dp[i][j + 1] == 1:
					dp[i][j] = True
				else:
					x = 0
					break
	if x == 1:
		print('YES')
	else:
		print('NO')


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		mat = []
		for _ in range(n):
			mat.append(input())
		poss = True
		for i in range(n - 1):
			for j in range(n - 1):
				if mat[i][j] == '1':
					if mat[i + 1][j] == '0' and mat[i][j + 1] == '0':
						poss = False
		if poss:
			print('YES')
		else:
			print('NO')
main()


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	state = [list(input().rstrip()) for _ in range(N)]
	Query.append((N, state))
for (N, state) in Query:
	ok = True
	for h in range(N - 1):
		for w in range(N - 1):
			if state[h][w] == '1' and state[h + 1][w] == '0' and (state[h][w + 1] == '0'):
				ok = False
	print('YES' if ok else 'NO')


import sys
input = sys.stdin.readline
for f in range(int(input())):
	n = int(input())
	poss = True
	field = [0] * n
	for i in range(n):
		foo = input()
		field[i] = [0] * n
		for j in range(n):
			if foo[j] == '1':
				field[i][j] = 1
	for i in range(n):
		for j in range(n):
			if field[i][j] == 1 and i < n - 1 and (j < n - 1):
				if field[i + 1][j] == 0 and field[i][j + 1] == 0:
					poss = 0
	if poss:
		print('YES')
	else:
		print('NO')


test_cases = int(input())
for z in range(test_cases):
	n = int(input())
	matrix = []
	for _ in range(n):
		row = input()
		matrix.append(row)
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if matrix[i][j] == '1':
				if matrix[i][j + 1] == '0':
					if matrix[i + 1][j] == '0':
						ans = 'NO'
	print(ans)


test = int(input())
for _ in range(test):
	n = int(input())
	mat = []
	for i in range(n):
		inp = input()
		k = []
		for j in range(n):
			k.append(int(inp[j]))
		mat.append(k)
	ans = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 1:
				if i != n - 1 and j != n - 1 and (mat[i][j + 1] != 1 and mat[i + 1][j] != 1):
					ans = False
					break
	if ans:
		print('YES')
	else:
		print('NO')


import math

def debug(*debugval):
	print('debug:   ', debugval)
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [[1 for i in range(n + 1)] for i in range(n + 1)]
	for i in range(n):
		str = input()
		for j in range(n):
			a[i][j] = int(str[j])
	flag = 1
	for i in range(n - 1, -1, -1):
		if flag == 0:
			break
		for j in range(n - 1, -1, -1):
			if a[i][j] == 0:
				continue
			if a[i + 1][j] == 0 and a[i][j + 1] == 0:
				flag = 0
			if flag == 0:
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	m = [input() for _ in range(n)]
	res = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if m[i][j] == '1' and m[i + 1][j] == '0' and (m[i][j + 1] == '0'):
				res = 'NO'
				break
		if res == 'NO':
			break
	print(res)


testy = int(input())
results = []

def shot(plansza, x, y):
	mozliwe = 1
	i = 0
	while i < x:
		if plansza[i][y] == 1:
			mozliwe = 0
			break
		i = i + 1
	if len(plansza) > x + 1:
		if plansza[x + 1][y] == 0:
			mozliwe = 0
	if mozliwe == 1:
		return 1
	else:
		mozliwe = 1
		i = 0
		while i < y:
			if plansza[x][i] == 1:
				mozliwe = 0
				break
			i = i + 1
		if len(plansza) > y + 1:
			if plansza[x][y + 1] == 0:
				mozliwe = 0
		return mozliwe
for i in range(testy):
	n = int(input())
	plansza = []
	for i in range(n):
		wejscie = [int(znak) for znak in input()]
		plansza.append(wejscie)
	mozliwa = 1
	for i in range(n):
		if mozliwa == 0:
			break
		for j in range(n):
			if plansza[j][i] == 1:
				if shot(plansza, j, i) == 1:
					plansza[j][i] = 0
				else:
					mozliwa = 0
	if mozliwa == 1:
		results.append('YES')
	else:
		results.append('NO')
for result in results:
	print(result)


import sys
from collections import deque

def solve():
	for i in range(n - 2, -1, -1):
		for j in range(i + 1):
			if a[j][i] == 1 and (not (a[j][i + 1] == 1 or a[j + 1][i] == 1)):
				print('NO')
				return
			if a[i][j] == 1 and (not (a[i + 1][j] == 1 or a[i][j + 1] == 1)):
				print('NO')
				return
	print('YES')
for _ in range(int(input())):
	n = int(input())
	a = [0] * n
	for i in range(n):
		a[i] = list(map(int, list(input().strip())))
	solve()


for i in range(int(input())):
	n = int(input())
	a = []
	ct = 0
	ct1 = 0
	ct2 = 0
	for i in range(n):
		x = input()
		ct += x.count('1')
		b = list(x)
		if b[-1] == '1':
			ct1 += 1
		a.append(b)
	ct -= a[-1].count('1')
	ct -= ct1
	if a[n - 1][n - 1] == '1':
		ct += 1
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i + 1][j] == '1' or a[i][j + 1] == '1':
					ct2 += 1
	if ct == ct2:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')
sys.setrecursionlimit = 5000

def dfs(node, visited, matrix, n):
	visited.add(node)
	neighbors = []
	(i, j) = node
	if (i, j - 1) not in visited and 0 <= j - 1 <= n - 1 and (matrix[i][j - 1] == '1'):
		neighbors.append((i, j - 1))
	if (i - 1, j) not in visited and 0 <= i - 1 <= n - 1 and (matrix[i - 1][j] == '1'):
		neighbors.append((i - 1, j))
	for neighbor in neighbors:
		dfs(neighbor, visited, matrix, n)

def prog():
	for _ in range(int(input().strip())):
		n = int(input().strip())
		matrix = [input().strip() for i in range(n)]
		ones = 0
		for row in matrix:
			for c in row:
				if c == '1':
					ones += 1
		visited = set()
		for j in range(n):
			if matrix[n - 1][j] == '1':
				start = (n - 1, j)
				dfs(start, visited, matrix, n)
		for i in range(n - 1):
			if matrix[i][n - 1] == '1':
				start = (i, n - 1)
				dfs(start, visited, matrix, n)
		if len(visited) == ones:
			print('YES')
		else:
			print('NO')
prog()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	arr2 = [[0 for i in range(n)] for j in range(n)]
	ones = []
	for i in range(n):
		row = list(map(int, list(input())))
		arr.append(row)
		for j in range(n):
			if row[j] == 1:
				ones.append([i, j])
	bad = False
	dirs = [[1, 0], [0, 1]]
	for (x, y) in ones:
		if x == n - 1 or y == n - 1:
			continue
		can = False
		for (a, b) in dirs:
			nx = x + a
			ny = y + b
			if nx >= 0 and nx < n and (ny >= 0) and (ny < n):
				if arr[nx][ny] == 1:
					can = True
		if not can:
			bad = True
	if not bad:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = [[] for j in range(n)]
	for j in range(n):
		arr[j] = [int(k) for k in list(input())]
	flag = True
	for j in range(n):
		for k in range(n):
			if arr[j][k] == 1:
				if j == n - 1 or k == n - 1:
					continue
				elif k < n - 1 and arr[j][k + 1] == 1:
					continue
				elif j < n - 1 and arr[j + 1][k] == 1:
					continue
				else:
					flag = False
					break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		c = []
		s = input()
		for p in s:
			if p == '0':
				c.append(0)
			else:
				c.append(1)
		a.append(c)
	f = 0
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if i < n - 1 and j < n - 1:
					if a[i + 1][j] == 1 or a[i][j + 1] == 1:
						pass
					else:
						f = 1
						break
				else:
					pass
			if f == 1:
				break
		if f == 1:
			break
	if f == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	L = [input() for _ in range(n)]
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if L[i][j] == '0':
				continue
			if i == n - 1 or j == n - 1:
				continue
			if L[i + 1][j] == '0' and L[i][j + 1] == '0':
				ans = 'NO'
				break
	print(ans)


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		n = int(input())
		target = []
		for _ in range(n):
			target.append(input() + '1')
		target.append('1' * n)
		for i in range(n):
			for j in range(n):
				if target[i][j] == '1' and target[i + 1][j] == '0' and (target[i][j + 1] == '0'):
					print('NO')
					return
		print('YES')
	q = int(input())
	for _ in range(q):
		solve()
main()


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())

def solve():
	n = ii()
	arr = [list(input()) for _ in range(n)]
	x = y = n - 2
	while x >= 0:
		a = x
		b = y
		while a >= 0:
			if arr[a][y] == '1':
				if arr[a + 1][y] != '1' and arr[a][y + 1] != '1':
					print('NO')
					return
			a -= 1
		while b >= 0:
			if arr[x][b] == '1':
				if arr[x][b + 1] != '1' and arr[x + 1][b] != '1':
					print('NO')
					return
			b -= 1
		x -= 1
		y -= 1
	print('YES')
for _ in range(ii()):
	solve()


for _ in range(int(input())):
	n = int(input())
	d = set()
	for i in range(n):
		s = input()
		for c in range(len(s)):
			if s[c] == '1':
				d.add((c, i))
	for p in d:
		if not (p[0] == n - 1 or p[1] == n - 1 or (p[0] + 1, p[1]) in d or ((p[0], p[1] + 1) in d)):
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	cond = False
	lst = []
	for i in range(n):
		mst = list(input())
		lst.append(mst)
	for i in range(n - 1):
		if cond:
			break
		for j in range(n - 1):
			if lst[i][j] == '1' and lst[i + 1][j] == '0' and (lst[i][j + 1] == '0'):
				print('NO')
				cond = True
				break
	if cond:
		continue
	else:
		print('YES')


def matrix(lista, n):
	if n <= 1:
		return True
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if lista[i][j] == '1':
				if not (lista[i][j + 1] == '1' or lista[i + 1][j] == '1'):
					return False
	return True
t = int(input())
for i in range(t):
	n = int(input())
	lista = []
	for j in range(n):
		lista1 = []
		line = input()
		for k in line:
			lista1 += [k]
		lista += [lista1]
	if matrix(lista, n):
		print('YES')
	else:
		print('NO')


n = int(input())
b = list()
v = list()
for i in range(n):
	c = 0
	a = int(input())
	for j in range(a):
		b.append(input())
	for j in range(a - 1):
		for k in range(a - 1):
			if b[j][k] == '1':
				if int(b[j + 1][k]) == 1 or int(b[j][k + 1]) == 1:
					d = 0
				else:
					c = 1
	if c == 0:
		v.append('YES')
	elif c == 1:
		v.append('NO')
	b.clear()
for i in v:
	print(i)


t = int(input())
for i in range(t):
	flag = 1
	n = int(input())
	arr = []
	for i in range(n):
		i = input()
		sub = list(map(int, i))
		arr.append(sub)
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] != 1 and arr[i][j + 1] != 1:
					flag = 0
	if flag == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	b = []
	for k in range(n):
		a = list(map(int, input()))
		b.append(a)
	ans = 1
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if b[i][j] and (not b[i + 1][j]) and (not b[i][j + 1]):
				ans = 0
	if ans == 0:
		print('NO')
	else:
		print('YES')


n = int(input())
for j in range(n):
	m = int(input())
	mat = []
	for _ in range(m):
		grid_item = input()
		mat.append(grid_item)
	flag1 = 0
	flag2 = 0
	for x in range(m):
		flag1 = 0
		flag2 = 0
		for y in range(m):
			flag1 = 0
			flag2 = 0
			if x != m - 1 and mat[x][y] == '1':
				if mat[x + 1][y] == '0':
					flag1 = 1
			if y != m - 1 and mat[x][y] == '1':
				if mat[x][y + 1] == '0':
					flag2 = 1
			if flag1 == 1 and flag2 == 1:
				break
		if flag1 == 1 and flag2 == 1:
			break
	if flag1 == 1 and flag2 == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input().strip())
	arr = list(map(str, input().strip().split()))
	flag = True
	for i in range(n - 1):
		arr += list(map(str, input().strip().split()))
	for i in range(n):
		if flag:
			for j in range(n):
				if i == n - 1 and j == n - 1:
					continue
				elif i == n - 1:
					pass
				elif j == n - 1:
					pass
				elif arr[i][j] == '1' and (arr[i + 1][j] != '1' and arr[i][j + 1] != '1'):
					print('NO')
					flag = False
					break
		else:
			break
	if flag:
		print('YES')


import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__

def go():
	n = int(input())
	expected = [False] * n
	answer = 'YES'
	for _ in range(n):
		line = input()
		for (i, c) in enumerate(line[:n]):
			if c == '0' and expected[i]:
				answer = 'NO'
			expected[i] = c == '1' and i < n - 1 and (line[i + 1] == '0')
	return answer
t = int(input())
ans = []
for _ in range(t):
	ans.append(str(go()))
print('\n'.join(ans))


t = int(input())
for _ in range(t):
	n = int(input())
	table = []
	for _ in range(n):
		s = input()
		table.append(s)
	check = False
	for i in range(n):
		for j in range(n - 1, -1, -1):
			if table[i][j] == '1':
				if j != n - 1 and i != n - 1:
					if table[i][j + 1] != '1' and table[i + 1][j] != '1':
						check = True
	if check:
		print('NO')
	else:
		print('YES')


def check(a, i, j, n):
	if i == n - 1:
		if j == n - 1:
			return True
	if i == n - 1:
		return True
	if j == n - 1:
		return True
	if a[i + 1][j] == '1' or a[i][j + 1] == '1':
		return True
	else:
		return False

def main():
	tc = int(input())
	while tc != 0:
		tc -= 1
		n = int(input())
		a = []
		ans = True
		for i in range(n):
			e = input()
			a.append(e)
		for i in range(n):
			for j in range(n):
				if a[i][j] == '1':
					ans = check(a, i, j, n)
					if ans is False:
						break
			if ans is False:
				break
		if ans is True:
			print('yes')
		else:
			print('no')
main()


import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_str():
	return readline().strip()

def slv(N, S):
	for i in range(N - 1):
		for j in range(N - 1):
			if S[i][j] == '1':
				if S[i][j + 1] == '0' and S[i + 1][j] == '0':
					return 'NO'
	return 'YES'

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		S = [read_str() for _ in range(N)]
		print(slv(N, S))
main()


T = int(input())

def fn_check(arr, n):
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '1':
				if i != n - 1 and j != n - 1:
					if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
						return 'NO'
	return 'YES'
for t in range(T):
	n = int(input())
	arr = []
	for i in range(n):
		l = list(input())
		arr.append(l)
	print(fn_check(arr, n))


def solve():
	n = int(input())
	ls = []
	for _ in range(n):
		ls.append(input())
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if ls[i][j] == '1' and (ls[i][j + 1] != '1' and ls[i + 1][j] != '1'):
				ans = False
				break
	print('YES' if ans else 'NO')
for _ in range(int(input())):
	solve()


def N():
	return int(input())

def NM():
	return map(int, input().split())

def L():
	return list(NM())

def LN(n):
	return [N() for i in range(n)]

def LL(n):
	return [L() for i in range(n)]
t = N()

def fill(l, x):
	w = len(l[0])
	return [[x] * (w + 2)] + [[x] + i + [x] for i in l] + [[x] * (w + 2)]

def f():
	n = N()
	l = fill([[int(j) for j in input()] for i in range(n)], 1)
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if l[i][j] == 1:
				if l[i + 1][j] + l[i][j + 1] == 0:
					return 'NO'
	return 'YES'
for i in range(t):
	print(f())


t = int(input())
for i in range(t):
	n = int(input())
	cells = []
	for j in range(n):
		gyou = list(map(int, list(input())))
		cells.append(gyou)
	ans = 'YES'
	for j in range(n - 1):
		for k in range(n - 1):
			if cells[j][k] == 0:
				continue
			if cells[j][k + 1] == 0 and cells[j + 1][k] == 0:
				ans = 'NO'
				break
		else:
			continue
	print(ans)


def main():
	for _ in range(int(input())):
		n = int(input())
		mat = []
		for _ in range(n):
			row = input()
			mat.append(row)
		fly = False
		for i in range(len(mat)):
			for j in range(len(mat[i])):
				if mat[i][j] == '1' and (i == len(mat) - 1 or j == len(mat[i]) - 1 or mat[i][j + 1] == '1' or (mat[i + 1][j] == '1')) or mat[i][j] == '0':
					continue
				else:
					fly = True
					break
			if fly:
				break
		if fly:
			print('NO')
		else:
			print('YES')
main()


def main():
	n = int(input())
	arr = []
	for _ in range(n):
		arr.append(list(str(input())))
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '1':
				if i != n - 1 and j != n - 1 and (arr[i + 1][j] != '1') and (arr[i][j + 1] != '1'):
					print('NO')
					return
	print('YES')
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


t = int(input())
for _ in range(t):
	n = int(input())
	P = []
	for i in range(n):
		P.append([bool(int(s)) for s in input()])
	mark = False
	for i in range(n - 1):
		for j in range(n - 1):
			if P[i][j] and (not (P[i + 1][j] or P[i][j + 1])):
				print('NO')
				mark = True
				break
		if mark:
			break
	if not mark:
		print('YES')


nb_cases = int(input())
for case in range(nb_cases):
	n = int(input())
	test_possible = 1
	polygon = [[0 for y in range(n)] for x in range(n)]
	for x in range(n):
		line = input()
		for y in range(n):
			polygon[x][y] = int(line[y])
	for x in range(n - 1):
		for y in range(n - 1):
			if polygon[x][y] == 1 and polygon[x][y + 1] == 0 and (polygon[x + 1][y] == 0):
				test_possible = 0
				break
		if test_possible == 0:
			break
	if test_possible == 1:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
for _ in range(int(fastinput())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(fastinput())
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '0':
				continue
			elif a[i][j + 1] == '1' or a[i + 1][j] == '1':
				continue
			else:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import os
import sys
from io import BytesIO, IOBase
import bisect
inp = lambda : int(input())
inps = lambda : [int(x) for x in input().split()]

def main():
	for _ in range(inp()):
		n = inp()
		mt = []
		for i in range(n):
			s = input()
			ls = [int(ch) for ch in s]
			mt.append(ls)
		ans = 0
		for i in range(n):
			for j in range(n):
				if mt[i][j] == 1:
					(f1, f2) = (0, 0)
					for r in range(j + 1, n):
						if mt[i][r] == 0:
							f1 = 1
							break
						else:
							break
					for r in range(i + 1, n):
						if mt[r][j] == 0:
							f2 = 1
							break
						else:
							break
					if f1 and f2:
						ans = 1
						break
			if ans:
				break
		if ans:
			print('NO')
		else:
			print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import math
import collections

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		row = list(input())
		matrix.append(row)
	np = 0
	for i in range(n):
		for j in range(n):
			ele = matrix[i][j]
			if ele == '1':
				if i == n - 1:
					continue
				elif j == n - 1:
					continue
				elif matrix[i + 1][j] == '0' and matrix[i][j + 1] == '0':
					np += 1
					break
	if np == 0:
		print('YES')
	else:
		print('NO')


import sys
from bisect import bisect_left, bisect_right
from collections import deque
INF = 1 << 60
t = int(input())
for _ in range(t):
	n = int(input())
	s = [[j for j in input()] for i in range(n)]
	yes = True
	for i in reversed(range(n - 1)):
		for j in reversed(range(n - 1)):
			if s[i][j] == '1' and s[i + 1][j] == '0' and (s[i][j + 1] == '0'):
				yes = False
	if yes:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(n):
		x = [int(i) for i in input()]
		a.append(x)
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if a[i][j] == 1:
				if i == n - 1 or j == n - 1:
					continue
				if a[i + 1][j] == 1 or a[i][j + 1] == 1:
					continue
				else:
					ans = 'NO'
					break
		if ans == 'NO':
			break
	print(ans)


test = int(input())
while test != 0:
	t = []
	n = int(input())
	for i in range(n):
		a = list(input())
		t.append(a)
	flag = 0
	for i in range(n):
		for j in range(n):
			if t[i][j] == '1':
				if (j != n - 1 and t[i][j + 1] == '0') and (i != n - 1 and t[i + 1][j] == '0'):
					flag = 1
					break
	if flag == 0:
		print('YES')
	else:
		print('NO')
	test -= 1


import math
t = int(input())
for _ in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	c = 0
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i == n - 1 or j == n - 1:
					pass
				elif mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
					pass
				else:
					c += 1
	if c == 0:
		print('Yes')
	else:
		print('No')


t = int(input())
try:
	while t > 0:
		n = int(input())
		a = []
		isValid = True
		for i in range(n):
			a.append(list(input()))
		for i in range(n):
			for j in range(n):
				if (i == n - 1 or j == n - 1) and a[i][j] == '1':
					continue
				if a[i][j] == '1':
					if a[i][j + 1] != '1' and a[i + 1][j] != '1':
						isValid = False
		print('YES' if isValid else 'NO')
		t -= 1
except Exception as e:
	print(e)


def isvalid(l, n):
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1' and i != n - 1 and (j != n - 1) and (l[i + 1][j] == '0') and (l[i][j + 1] == '0'):
				return 0
	return 1
t = int(input())
for you in range(t):
	n = int(input())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	if isvalid(l, n):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [input() for x in range(n)]
	notValid = False
	for r in range(n):
		for c in range(n):
			if arr[r][c] == '0':
				continue
			res = False
			if r == n - 1 or arr[r + 1][c] == '1':
				res = True
			if c == n - 1 or arr[r][c + 1] == '1':
				res = True
			if not res:
				notValid = True
				break
		if notValid:
			break
	if notValid:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	k = []
	for j in range(n):
		k.append(list(map(int, input())))
	p = 0
	for j in range(n):
		for h in range(n):
			if k[j][h] == 1:
				if j < n - 1 and h < n - 1 and (k[j + 1][h] == 0) and (k[j][h + 1] == 0):
					p += 1
					break
		if p != 0:
			break
	if p == 0:
		print('YES')
	else:
		print('NO')


import os
import sys
import io
import math
for t in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(list(map(int, input())))
	check = True
	for y in range(n - 1):
		for x in range(n - 1):
			if matrix[y][x] == 1:
				if matrix[y][x + 1] == 0 and matrix[y + 1][x] == 0:
					check = False
					break
	if check == True:
		print('YES')
	else:
		print('NO')


def print_m(a):
	for i in range(len(a)):
		for j in range(len(a)):
			print(a[i][j], end=' ')
		print()
for t in range(int(input())):
	n = int(input())
	s = [[int(i) for i in list(input())] for j in range(n)]
	c = True
	for i in range(n):
		for j in range(n):
			if i == n - 1:
				k2 = i
			else:
				k2 = i + 1
			if j == n - 1:
				k1 = j
			else:
				k1 = j + 1
			c = (s[i][k1] == 1 or s[k2][j] == 1) or s[i][j] == 0
			if not c:
				break
		if not c:
			break
	if c:
		print('YES')
	else:
		print('NO')


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
for _ in range(R2()):
	n = R2()
	M = []
	res = True
	for _ in range(n):
		M.append(input())
	if n == 1:
		print('YES')
		continue
	for i in range(n - 1):
		m = M[i]
		for j in range(n - 1):
			if m[j] == '1' and (m[j] != m[j + 1] and M[i + 1][j] != '1'):
				res = False
	if res:
		print('YES')
	else:
		print('NO')


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = []
	for i in range(n):
		a.append(input().strip())
	flag = 1
	for i in range(n):
		for j in range(n):
			if a[i][j] == '0':
				continue
			temp = 2
			if i < n - 1:
				if a[i + 1][j] == '0':
					temp -= 1
			if j < n - 1:
				if a[i][j + 1] == '0':
					temp -= 1
			if temp == 0:
				flag = 0
				break
		if flag == 0:
			break
	if flag:
		print('YES')
	else:
		print('NO')
for _ in range(I()):
	solve()


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = []
	for i in range(n):
		x = input()
		a.append(list(x))
	flag = 0
	for i in range(n):
		for j in range(n):
			x = a[i][j]
			if x == '0':
				continue
			elif x == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				else:
					print('NO')
					flag = 1
					break
		if flag:
			break
	if flag == 0:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(map(bool, list(map(int, list(input()))))))
	answer = True
	for y in reversed(range(0, n - 1)):
		for j in reversed(range(0, n - 1)):
			if a[y][j] and (not a[y + 1][j]) and (not a[y][j + 1]):
				answer = False
				break
		if not answer:
			break
	print('YES' if answer else 'NO')


t = int(input())
for _ in range(0, t):
	n = int(input())
	poly = []
	for i in range(0, n):
		s = input()
		poly.append(s)
	ans = 'YES'
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if poly[i][j] == '1' and j + 1 < n and (i + 1 < n) and (poly[i][j + 1] == '0') and (poly[i + 1][j] == '0'):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		b = list(input())
		a.append(list(map(int, b)))
	res = True
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j]:
				res &= a[i + 1][j] | a[i][j + 1]
	print(['NO', 'YES'][res])


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = [input().strip() for x in range(n)]
	r = n - 1
	for i in range(n):
		for j in range(n):
			if arr[i][j] == '1':
				ok = 0
				if i == r or j == r or arr[i][j + 1] == '1' or (arr[i + 1][j] == '1'):
					pass
				else:
					print('no')
					return
	print('yes')
tt = int(input())
for test in range(tt):
	solve()


import sys
from math import sqrt
input = sys.stdin.readline

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = []
		for __ in range(n):
			arr.append(list(input().strip()))
		is_no = False
		for i in range(n):
			for j in range(n):
				if arr[i][j] != '1':
					continue
				if i == n - 1 or j == n - 1:
					continue
				if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
					is_no = True
					break
			if is_no:
				break
		print('NO' if is_no else 'YES')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		s = input()
		y = []
		for j in s:
			y.append(int(j))
		arr.append(y)
	flag = 0
	for i in range(n):
		for j in range(n):
			if i == n - 1 or j == n - 1:
				continue
			elif arr[i][j] == 1:
				if arr[i + 1][j] == 0 and arr[i][j + 1] == 0:
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	k1 = []
	for j in range(n):
		l = list(input())
		k1.append(l)
	for w1 in range(len(l) - 1):
		for w2 in range(len(l) - 1):
			if k1[w1][w2] == '1' and (k1[w1 + 1][w2] == '0' and k1[w1][w2 + 1] == '0'):
				print('NO')
				break
		if k1[w1][w2] == '1' and (k1[w1 + 1][w2] == '0' and k1[w1][w2 + 1] == '0'):
			break
	else:
		print('YES')


def solve(a, n):
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] and (not (a[i + 1][j] or a[i][j + 1])):
				return False
	return True
t = int(input())
for i_t in range(t):
	n = int(input())
	a = [None] * n
	for i_n in range(n):
		a[i_n] = list(map(int, input()))
	print('YES' if solve(a, n) else 'NO')


ans = []
for _ in range(int(input())):
	n = int(input())
	u = []
	for i in range(n):
		u.append(list(map(int, list(input()))))
	st = []
	for i in range(n):
		if u[-1][i] == 1:
			st.append((n - 1, i))
			u[-1][i] = 2
		if u[i][-1] == 1:
			st.append((i, n - 1))
			u[i][-1] = 2
	while len(st) > 0:
		(x, y) = st[-1]
		st.pop()
		if x > 0 and u[x - 1][y] == 1:
			st.append((x - 1, y))
			u[x - 1][y] = 2
		if y > 0 and u[x][y - 1] == 1:
			st.append((x, y - 1))
			u[x][y - 1] = 2
	ok = False
	for i in range(n):
		for j in range(n):
			if u[i][j] == 1:
				ok = True
				break
		if ok:
			break
	if ok:
		ans.append('NO')
	else:
		ans.append('YES')
print('\n'.join(ans))


for _ in range(int(input())):
	n = int(input())
	arr = []
	for _ in range(n):
		arr.append(list(input()))
	flag = True
	for row in range(n - 1, -1, -1):
		for col in range(n):
			if row == n - 1:
				pass
			elif arr[row][col] == '1':
				if col != n - 1:
					if arr[row][col + 1] != '1' and arr[row + 1][col] != '1':
						flag = False
						break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	d = []
	for i in range(n):
		s = input()
		d.append(s)
	lost = False
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if d[i][j] == '1' and (not (d[i][j + 1] == '1' or d[i + 1][j] == '1')):
				lost = True
	if lost:
		print('NO')
	else:
		print('YES')


def solve(grid, n):
	flag = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if grid[i][j] == '1' and grid[i + 1][j] == '0' and (grid[i][j + 1] == '0'):
				flag = 0
				break
		if not flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')

def main():
	t = int(input())
	for _ in range(t):
		grid = []
		n = int(input())
		for _ in range(n):
			row = list(input())
			grid.append(row)
		solve(grid, n)
main()


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7
T = int(stdin.readline())

def DFSr(i, j):
	m[i][j] += 1
	if i - 1 >= 0 and m[i - 1][j] == 1:
		DFSr(i - 1, j)
	if j - 1 >= 0 and m[i][j - 1] == 1:
		DFSr(i, j - 1)
for _ in range(T):
	n = int(stdin.readline())
	m = []
	for i in range(n):
		s = stdin.readline().strip('\n')
		m.append([])
		for j in range(n):
			if s[j] == '0':
				m[i].append(0)
			else:
				m[i].append(1)
	for i in range(n):
		if m[i][n - 1] == 1:
			DFSr(i, n - 1)
		if m[n - 1][i] == 1:
			DFSr(n - 1, i)
	res = True
	for i in range(n):
		for j in range(n):
			if m[i][j] == 1:
				res = False
				break
		if not res:
			break
	if res:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		te = input().strip()
		a.append(te)
	dp = [[0 for j in range(n)] for k in range(n)]
	p = n - 1
	for j in range(n):
		if a[j][p] == '1':
			dp[j][p] = 1
		if a[p][j] == '1':
			dp[p][j] = 1
	j = n - 2
	k = n - 2
	f = 0
	while j >= 0:
		k = j
		while k >= 0:
			if a[k][j] == '1':
				if dp[k + 1][j] == 1 or dp[k][j + 1] == 1:
					dp[k][j] = 1
				else:
					f = 1
					j = -1
					break
			k = k - 1
		k = j
		while k >= 0:
			if a[j][k] == '1':
				if dp[j + 1][k] == 1 or dp[j][k + 1] == 1:
					dp[j][k] = 1
				else:
					f = 1
					j = -1
					break
			k = k - 1
		j = j - 1
	if f == 0:
		print('YES')
	else:
		print('NO')


def solve(n, field):
	if n == 1:
		return 'YES'
	i = n - 2
	while i >= 0:
		j = n - 2
		while j >= 0:
			if field[i][j] == 1 and field[i][j + 1] == 0 and (field[i + 1][j] == 0):
				return 'NO'
			j -= 1
		i -= 1
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	poly = []
	for j in range(n):
		poly.append([int(p) for p in input()])
	print(solve(n, poly))


t = int(input())
for _ in range(t):
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(input())
	if n == 1:
		print('YES')
	else:
		flag = 0
		flag2 = 0
		for j in range(n - 1, -1, -1):
			for k in range(n - 1, -1, -1):
				if ar[j][k] == '1':
					if j < n - 1 and k < n - 1:
						if ar[j + 1][k] == '0' and ar[j][k + 1] == '0':
							print('NO')
							flag = 1
							break
			if flag == 1:
				flag2 = 1
			if flag2 == 1:
				break
		if flag2 == 0:
			print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		p = []
		z = input()
		for j in z:
			p.append(int(j))
		l.append(p)
	c = 0
	for i in range(n):
		for j in range(n):
			if l[i][j] == 1 and j == n - 1:
				continue
			elif j + 1 < n and l[i][j] == 1 and (l[i][j + 1] == 1):
				continue
			elif i + 1 < n and l[i][j] == 1 and (l[i + 1][j] == 1):
				continue
			elif l[i][j] == 0:
				continue
			elif l[i][j] == 1 and i == n - 1:
				continue
			else:
				c += 1
				break
		if c == 1:
			break
	if c == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	a = []
	for i in range(n):
		b = []
		l = list(map(str, input().split()))
		for j in l:
			for p in range(len(j)):
				b.append(int(j[p]))
			a.append(b)
	for i in range(len(a)):
		for j in range(len(a[i])):
			key = 0
			if a[i][j] == 1:
				try:
					if a[i + 1][j] == 0 and a[i][j + 1] == 0:
						key = 1
				except IndexError:
					pass
			if key == 1:
				print('NO')
				break
		if key == 1:
			break
	if key == 0:
		print('YES')
	t -= 1


for i in range(int(input())):
	n = int(input())
	matrix = []
	for ii in range(n):
		l1 = list(map(int, input()))
		matrix.append(l1)
	f1 = 0
	f2 = 0
	flag = 0
	for j in range(n):
		flag = 0
		if flag == 1:
			break
		for k in range(n):
			f1 = 0
			f2 = 0
			if flag == 1:
				break
			if matrix[j][k] == 1:
				if j != n - 1:
					if matrix[j + 1][k] != 1:
						f1 = 1
				if k != n - 1:
					if matrix[j][k + 1] != 1:
						f2 = 1
			if f1 == 0 or f2 == 0:
				pass
			else:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


def solve(mat, n):
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif mat[i][j + 1] == '1' or mat[i + 1][j] == '1':
					continue
				else:
					return False
	return True
t = int(input())
while t:
	t = t - 1
	n = int(input())
	mat = []
	for i in range(n):
		s = input()
		mat.append(list(s))
	if solve(mat, n):
		print('YES')
	else:
		print('NO')


import sys
T = int(sys.stdin.readline().strip())
for t in range(T):
	n = int(sys.stdin.readline().strip())
	arr = []
	for i in range(n):
		tmp = sys.stdin.readline().strip()
		arr.append(list(tmp))
	can = True
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
					can = False
					break
		if not can:
			break
	if can:
		print('YES')
	else:
		print('NO')


import sys
from pprint import pprint

def eprint(*args, **kwargs):
	return
	print(*args, file=sys.stderr, **kwargs)

def is_reachable(x, y, mat, n, dp):
	if x == n - 1 or y == n - 1:
		return True
	key = (x, y)
	if key in dp:
		return dp[key]
	ok = False
	for (dx, dy) in [(x + 1, y), (x, y + 1)]:
		if mat[dy][dx] == 1:
			if is_reachable(dx, dy, mat, n, dp):
				ok = True
				break
	dp[key] = ok
	return ok

def solve(mat, n):
	dp = {}
	for (y, row) in enumerate(mat):
		for (x, v) in enumerate(row):
			if v == 1:
				if not is_reachable(x, y, mat, n, dp):
					eprint(x, y)
					return False
	return True

def main():
	num_test_cases = int(input())
	for t in range(num_test_cases):
		n = int(input())
		mat = []
		for _ in range(n):
			s = input()
			row = [0 if x == '0' else 1 for x in s]
			mat.append(row)
		eprint(mat)
		if solve(mat, n):
			print('YES')
		else:
			print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	A = []
	D = []
	for i in range(n):
		X = []
		for (j, x) in enumerate(input()):
			x = int(x)
			X += [x]
			if x == 1:
				D += [(i, j)]
		A += [X]
	result = 'YES'
	for (i, j) in D:
		if i < n - 1 and j < n - 1:
			if not (A[i + 1][j] or A[i][j + 1]):
				result = 'NO'
				break
	print(result)


def main():
	n = int(input())
	s = ['' for i in range(n)]
	for i in range(n):
		s[i] = input()
	for i in range(n - 1):
		for j in range(n - 1):
			if s[i][j] == '1':
				if not (s[i + 1][j] == '1' or s[i][j + 1] == '1'):
					return False
	return True
rw = int(input())
for qe in range(rw):
	t = main()
	if t:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for _ in range(n):
		arr.append(input())
	response = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] == '0' and arr[i][j + 1] == '0':
					response = 'NO'
					break
	print(response)


for _ in range(int(input())):
	N = int(input())
	arr = []
	for _ in range(N):
		arr.append(input())
	flag = 1
	for i in range(N - 1):
		for j in range(N - 1):
			if arr[i][j] == '1':
				if arr[i + 1][j] == '1' or arr[i][j + 1] == '1':
					continue
				else:
					flag = 0
					break
	if flag == 0:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		l = input()
		l = l.strip()
		mat.append(l)
	flag = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i != n - 1 and j != n - 1 and (mat[i][j + 1] != '1') and (mat[i + 1][j] != '1'):
					flag = False
	if flag == True:
		print('YES')
	else:
		print('NO')


def solve():
	n = int(input())
	l = []
	for i in range(n):
		x = input()
		x = list(x)
		l.append(x)
	for i in range(len(l)):
		for j in range(len(l[i])):
			if l[i][j] == '1' and j != n - 1 and (i != n - 1):
				if l[i][j + 1] != '1' and l[i + 1][j] != '1':
					print('NO')
					return 1
	print('YES')
for tt in range(int(input())):
	solve()


t = int(input().strip())
for _ in range(t):
	g = []
	flag = 0
	n = int(input().strip())
	for _ in range(n):
		g.append(input().strip())
	for i in range(n - 1):
		for j in range(n - 1):
			if g[i][j] == '1':
				if g[i + 1][j] != '1' and g[i][j + 1] != '1':
					flag = 1
					break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		tmp = input()
		tmp1 = []
		for val in tmp:
			tmp1.append(val)
		l.append(tmp1)
	res = True
	for i in range(n):
		for j in range(n):
			if l[i][j] == '1':
				if i == n - 1:
					continue
				elif j == n - 1:
					continue
				elif l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				else:
					res = False
					break
	if res:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	size = int(input())
	mat = []
	for i in range(size):
		mat += [input()]
	possi = True
	for i in range(size):
		for j in range(size):
			if mat[i][j] == '1':
				if i == size - 1 or j == size - 1 or mat[i + 1][j] == '1' or (mat[i][j + 1] == '1'):
					pass
				else:
					possi = False
	if possi:
		print('Yes')
	else:
		print('NO')


test_case = int(input())
for i in range(test_case):
	polygon_size = int(input())
	(j, k) = (0, 0)
	arr = [[int(j) for j in input().strip()] for i in range(polygon_size)]
	(j, k, error) = (0, 0, 0)
	for j in range(polygon_size):
		for k in range(polygon_size):
			if arr[j][k] == 1:
				if j + 1 == polygon_size or k + 1 == polygon_size:
					continue
				elif arr[j + 1][k] == 1 or arr[j][k + 1] == 1:
					continue
				else:
					error += 1
	if error == 0:
		print('YES')
	else:
		print('NO')


import math
T = int(input())
for t in range(T):
	n = int(input())
	s = []
	for i in range(n):
		s.append(input() + '1')
	s.append('1' * len(s[0]))
	flag = False
	for i in range(n):
		for j in range(len(s[0]) - 1):
			if s[i][j] == '1' and s[i][j + 1] == '0' and (s[i + 1][j] == '0'):
				flag = True
				break
		if flag:
			break
	if flag:
		print('NO')
	else:
		print('YES')


import sys
from math import ceil, sqrt, sin, pi, tan
from collections import defaultdict
read = sys.stdin.buffer.readline
import heapq
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())

def isp(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
ps = lambda x: x == pow(int(sqrt(x)), 2)
for _t in range(cin()):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	flag = False
	ans = [[0 for j in range(n)] for i in range(n)]
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1:
					ans[i][j] = 1
				elif a[i + 1][j] == '1' or a[i][j + 1] == '1':
					ans[i][j] = 1
				else:
					flag = True
			if flag:
				break
		if flag:
			break
	print(['yes', 'no'][flag])


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	grid = [list(map(int, list(input().strip('\n')))) for i in range(n)]
	ans = True
	for i in range(n):
		for j in range(n):
			if grid[i][j]:
				if i + 1 >= n:
					a = True
				else:
					a = grid[i + 1][j]
				if j + 1 >= n:
					b = True
				else:
					b = grid[i][j + 1]
				if not (a or b):
					ans = False
	if ans:
		print('YES')
	else:
		print('NO')


def solve(n, d):
	for i in range(n - 1):
		for j in range(n - 1):
			if d[i][j] == '1':
				if d[i + 1][j] != '1' and d[i][j + 1] != '1':
					return 'NO'
	return 'YES'

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		d = []
		for i in range(n):
			s = input()
			d.append([i for i in s])
		ans = solve(n, d)
		print(ans)
main()


def func1(n, arr):
	for i in range(n - 1):
		for j in range(n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] == 0 and arr[i][j + 1] == 0:
					return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		s = input()
		arr.append([int(el) for el in s])
	if func1(n, arr):
		print('YES')
	else:
		print('NO')


from math import sqrt
t = int(input())
for _ in range(t):
	n = int(input())
	mat = [list(str(input())) for _ in range(n)]
	ans = True
	for i in range(n):
		for j in range(n):
			if mat[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
					continue
				else:
					ans = False
					break
		if not ans:
			break
	print('YES' if ans else 'NO')


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		graph = []
		for _ in range(n):
			graph.append(input())
		is_possible = True
		for i in range(n - 1):
			if is_possible:
				for j in range(n - 1):
					if graph[i][j] == '1':
						if graph[i + 1][j] == '0' and graph[i][j + 1] == '0':
							is_possible = False
							break
		if is_possible:
			print('YES')
		else:
			print('NO')
main()


for _ in [0] * int(input()):
	k = int(input())
	array = []
	for x in range(k):
		array += [str(input())]

	def checker(a, n):
		for i in range(n):
			for j in range(n):
				if a[i][j] == '1':
					if j != n - 1 and i != n - 1:
						if a[i][j + 1] == '0' and a[i + 1][j] == '0':
							return 'NO'
		return 'YES'
	print(checker(array, k))


for x in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		m = list(map(int, list(input())))
		a.append(m)
	r = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] == 0 and a[i][j + 1] == 0:
					r = 'NO'
					break
	print(r)


t = int(input())
for test in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		row = input()
		delta = []
		for j in row:
			if j == '0':
				delta.append(0)
			else:
				delta.append(1)
		matrix.append(delta)
	flag = 0
	for j in range(n - 2, -1, -1):
		for i in range(n - 1):
			if matrix[i][j] == 1:
				if matrix[i + 1][j] == 1 or matrix[i][j + 1] == 1:
					continue
				else:
					flag = 1
					break
	if flag == 1:
		print('NO')
	else:
		print('YES')


import math
t = int(input())
for i in range(t):
	n = int(input())
	A = []
	for j in range(n):
		a = input()
		aa = []
		for l in a:
			aa.append(int(l))
		A.append(aa)
	ans = True
	for j in range(n - 1):
		for k in range(n - 1):
			if A[j][k] == 1:
				if A[j][k + 1] == 0 and A[j + 1][k] == 0:
					ans = False
	if ans:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for i in range(t):
	n = int(input())
	array = []
	for i in range(n):
		array.append([int(s) for s in input()])
	ans = True
	for i in range(n):
		for j in range(n):
			if array[i][j] == 1:
				if 0 <= i < n - 1 and 0 <= j < n - 1:
					if array[i + 1][j] != 1 and array[i][j + 1] != 1:
						ans = False
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin
for t in range(int(input())):
	n = int(input())
	s = [0] * n
	for i in range(n):
		s[i] = stdin.readline().strip()
	cond = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if s[i][j] == '1' and s[i + 1][j] == '0' and (s[i][j + 1] == '0'):
				cond = 0
				break
		if cond == 0:
			break
	if cond == 1:
		print('YES')
	else:
		print('NO')


def check_poly(polygon, i, j, n):
	if polygon[i][j] == 0:
		return True
	if i == n - 1 or j == n - 1:
		return True
	else:
		cell1 = polygon[i + 1][j]
		cell2 = polygon[i][j + 1]
		if cell1 == 0 and cell2 == 0:
			return False
		return True
t = int(input())
for _ in range(t):
	polygon = []
	n = int(input())
	for i in range(n):
		row = [int(x) for x in input()]
		polygon.append(row)
	ans = 'YES'
	for i in range(n):
		for j in range(n):
			if check_poly(polygon, i, j, n) == False:
				ans = 'NO'
				break
		else:
			continue
		break
	print(ans)


import math
T = int(input())
for _ in range(T):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(input()))
	flag = 0
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if arr[i][j] == '1':
				if arr[i][j + 1] == '0' and arr[i + 1][j] == '0':
					flag = 1
					break
	if flag == 1:
		print('NO')
	else:
		print('YES')


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
for _ in range(int(input())):
	n = int(input())
	l = []
	flag = 'YES'
	z = [['N' for i in range(n)] for i in range(n)]
	for i in range(n):
		l.append(list(input()))
	for i in range(n):
		if l[i][n - 1] == '1':
			z[i][n - 1] = 'Y'
		if l[n - 1][i] == '1':
			z[n - 1][i] = 'Y'
	for i in range(n - 2, -1, -1):
		(a, b) = (i, i)
		while a >= 0 and b >= 0:
			if l[a][i] == '1':
				if z[a + 1][i] == 'Y' or z[a][i + 1] == 'Y':
					z[a][i] = 'Y'
				else:
					flag = 'NO'
			if l[i][b] == '1':
				if z[i + 1][b] == 'Y' or z[i][b + 1] == 'Y':
					z[i][b] = 'Y'
				else:
					flag = 'NO'
			a -= 1
			b -= 1
	print(flag)


TT = int(input())
for loop in range(TT):
	n = int(input())
	a = []
	for i in range(n):
		aa = input()
		a.append(aa)
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1' and (a[i + 1][j] == '0' and a[i][j + 1] == '0'):
				ans = 'NO'
				break
	print(ans)


num_cases = int(input())
for _ in range(num_cases):
	n = int(input())
	arr = []
	for _ in range(n):
		l = [int(d) for d in input().strip()]
		arr.append(l)
	poss = 1
	for i in range(n - 1):
		if poss == 0:
			break
		for j in range(n - 1):
			if arr[i][j] == 1:
				if arr[i + 1][j] == 0 and arr[i][j + 1] == 0:
					poss = 0
					break
	if poss == 1:
		print('YES')
	else:
		print('NO')


def main():
	n = int(input())
	for _ in range(n):
		k = int(input())
		g = [input() + '1' for _ in range(k)]
		g += ['1' * (k + 1)]
		flg = True
		for i in range(k):
			for j in range(k):
				if g[i][j] == '1':
					if g[i + 1][j] == '0' and g[i][j + 1] == '0':
						flg = False
		print('YES' if flg else 'NO')
main()


from math import sqrt
for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	ans = 1
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '1' or l[i][j + 1] == '1':
					continue
				else:
					flag = 1
					break
			if flag:
				break
		if flag:
			break
	if flag:
		ans = 0
	if ans:
		print('YES')
	else:
		print('NO')


def existsOnes(ii, jj):
	if mat[ii + 1][jj] == '1' or mat[ii][jj + 1] == '1':
		return True
	return False
t = int(input())
for i in range(t):
	n = int(input())
	mat = []
	for j in range(n):
		row = input()
		mat.append(row + '1')
	row = ['1'] * (n + 1)
	mat.append(row)
	test = True
	for ii in range(n):
		if not test:
			break
		for jj in range(n):
			if not test:
				break
			if mat[ii][jj] == '1':
				test = existsOnes(ii, jj)
	if test:
		print('YES')
	else:
		print('NO')


t = int(input())
while t != 0:
	n = int(input())
	list1 = list()
	for i in range(n):
		temp = input()
		list1.append(list(temp))
	f1 = 0
	f2 = 0
	f3 = 0
	f4 = 0
	f5 = 0
	for i in range(n):
		for j in range(n):
			if list1[i][j] == '1':
				if i + 1 < n:
					if list1[i + 1][j] != '1':
						f1 = 1
				if j + 1 < n:
					if list1[i][j + 1] != '1':
						f4 = 1
			if f1 == 0 or f4 == 0:
				(f1, f4) = (0, 0)
			else:
				f5 = 1
				break
		if f5 == 1:
			break
	if f5 == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


for _ in range(int(input())):
	report = []
	n = int(input())
	for _ in range(n):
		report.append(input())
	ans = 'YES'
	for i in range(n - 1):
		for j in range(n - 1):
			if report[i][j] == '1':
				if report[i + 1][j] == '1' or report[i][j + 1] == '1':
					continue
				else:
					ans = 'NO'
					break
	print(ans)


t = int(input())
for _ in range(t):
	size = int(input())
	grid = []
	for i in range(size):
		row = input()
		grid.append(row)
	validate = []
	posn = []
	for i in range(size):
		temp = []
		for j in range(size):
			if grid[i][j] == '0':
				temp.append(True)
			else:
				temp.append(False)
				posn.append([i, j])
		validate.append(temp)
	validate[-1] = [True] * size
	for i in range(len(validate)):
		validate[i][-1] = True
	for i in range(len(grid) - 1):
		for j in range(len(grid) - 1):
			if grid[i][j] == '1':
				if grid[i + 1][j] == '1' or grid[i][j + 1] == '1':
					validate[i][j] = True
	counter = 0
	for i in validate:
		for j in i:
			if not j:
				counter += 1
				break
		if counter:
			break
	if counter:
		print('NO')
	else:
		print('YES')


def read_int():
	return int(input())

def read_string_array():
	return input().split()

def read_int_array():
	return [int(_) for _ in read_string_array()]

def read_line():
	return input()

def solve(mat):
	n = len(mat)
	for r in range(n - 1):
		for c in range(n - 1):
			if mat[r][c] and (not (mat[r + 1][c] or mat[r][c + 1])):
				return 'NO'
	return 'YES'
T = read_int()
for _ in range(T):
	N = read_int()
	mat = [[int(__) for __ in read_line()] for _ in range(N)]
	print(solve(mat))


import sys
input = sys.stdin.readline

def solve(n, Map):
	for i in range(n - 1):
		for j in range(n - 1):
			s = Map[i][j]
			if s == '0':
				continue
			if Map[i + 1][j] == '0' and Map[i][j + 1] == '0':
				return 'NO'
	return 'YES'

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		Map = [None] * n
		for i in range(n):
			Map[i] = list(input().strip())
		ans = solve(n, Map)
		sys.stdout.write(ans + '\n')
main()


for mda in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	ok = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1 or a[i + 1][j] == '1' or (a[i][j + 1] == '1'):
					continue
				else:
					ok = False
					break
		if ok == False:
			break
	if ok == True:
		print('YES')
	else:
		print('NO')


from __future__ import division, print_function
import itertools
import sys
import os
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def is_it_local():
	script_dir = str(os.getcwd()).split('/')
	username = 'dipta007'
	return username in script_dir

def READ(fileName):
	if is_it_local():
		sys.stdin = open(f'./{fileName}', 'r')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if not is_it_local():
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')

def input1(type=int):
	return type(input())

def input2(type=int):
	[a, b] = list(map(type, input().split()))
	return (a, b)

def input3(type=int):
	[a, b, c] = list(map(type, input().split()))
	return (a, b, c)

def input_array(type=int):
	return list(map(type, input().split()))

def input_string():
	s = input()
	return list(s)

def main():
	t = input1()
	for ci in range(t):
		n = input1()
		arr = [[0 for _ in range(n)] for _ in range(n)]
		for i in range(n):
			st = input_string()
			for j in range(n):
				if st[j] == '0':
					arr[i][j] = 0
				else:
					arr[i][j] = 1
		for i in range(n):
			arr[n - 1][i] = 2 if arr[n - 1][i] >= 1 else 0
			arr[i][n - 1] = 2 if arr[i][n - 1] >= 1 else 0
		flg = 1
		for i in range(n - 2, -1, -1):
			for j in range(n - 2, -1, -1):
				if arr[i][j] == 1:
					if arr[i][j + 1] != 2 and arr[i + 1][j] != 2:
						flg = 0
						break
					else:
						arr[i][j] = 2
			if flg == 0:
				break
		if flg:
			print('YES')
		else:
			print('NO')
	pass
READ('in.txt')
main()


import sys
input = sys.stdin.readline
from math import sqrt
t = int(input())
for tests in range(t):
	n = int(input())
	S = [input().strip() for i in range(n)]
	flag = 0
	for i in range(n):
		for j in range(n):
			if S[i][j] == '1':
				if (i < n - 1 and S[i + 1][j] == '0') and (j < n - 1 and S[i][j + 1] == '0'):
					print('NO')
					flag = 1
					break
		if flag:
			break
	if flag == 0:
		print('YES')


def canon(n, a):
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == 1:
				if a[i + 1][j] != 1 and a[i][j + 1] != 1:
					return 'NO'
	return 'YES'
T = int(input())
for i in range(T):
	n = int(input())
	arr = []
	for j in range(n):
		row = [int(k) for k in input()]
		arr.append(row)
	print(canon(n, arr))


def solve():
	n = int(input())
	m = []
	for i in range(0, n):
		s = input()
		m.append(s)
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if m[i][j] == '1':
				if m[i][j + 1] != '1' and m[i + 1][j] != '1':
					print('NO')
					return
	print('YES')

def main():
	t = int(input())
	for i in range(0, t):
		solve()
main()


t = int(input())
for i in range(t):
	n = int(input())
	matrix = []
	for j in range(n):
		s = input()
		lst = list(s)
		matrix.append(lst)
	er = 0
	for j in range(n):
		for k in range(n):
			if matrix[j][k] == '1':
				if k + 1 < n and matrix[j][k + 1] == '0':
					if j + 1 < n and matrix[j + 1][k] == '0':
						er += 1
						break
		if er > 0:
			break
	if er == 0:
		print('YES')
	else:
		print('NO')


import sys
import math
from collections import defaultdict

def solve(test):
	n = int(input())
	mat = []
	for i in range(n):
		row = list(input())
		mat.append(row)
	for i in range(n - 1):
		for j in range(n - 1):
			if mat[i][j] == '1':
				if not (mat[i + 1][j] == '1' or mat[i][j + 1] == '1'):
					print('NO')
					return
	print('YES')
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [[0] * n] * n
	for j in range(n):
		arr2 = list(map(int, input()))
		arr[j] = arr2
	flag = 0
	for k in range(n):
		for l in range(n):
			if arr[k][l] == 1:
				if k + 1 > 0 and k + 1 < n and (l + 1 >= 0) and (l + 1 < n):
					if arr[k + 1][l] == 0 and arr[k][l + 1] == 0:
						flag = 1
	if flag == 1:
		print('NO')
		continue
	print('YES')


a = int(input())
for _ in range(a):
	x = int(input())
	li = [0] * x
	for i in range(x):
		li[i] = list(map(int, input()))
	p = True
	for i in range(x):
		if p:
			for j in range(x):
				if li[i][j] != 0:
					if i == x - 1 or j == x - 1:
						continue
					elif li[i + 1][j] or li[i][j + 1]:
						continue
					else:
						print('NO')
						p = False
						break
		else:
			break
	if p:
		print('YES')


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	for case in range(int(input())):
		n = int(input())
		matrix = []
		for i in range(n):
			matrix.append([0] * n)
		for i in range(n):
			temp = input()
			for j in range(n):
				matrix[i][j] += int(temp[j])
		ans_flag = True
		for i in range(n):
			for j in range(n):
				if matrix[i][j] == 1:
					if i == n - 1 or j == n - 1:
						pass
					elif matrix[i + 1][j] == 1 or matrix[i][j + 1] == 1:
						pass
					else:
						ans_flag = False
						break
			if ans_flag == False:
				break
		if ans_flag:
			print('YES')
		else:
			print('NO')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = []
	for i in range(n):
		ch = input()
		s.append(ch)
	flag = True
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if s[i][j] == '1':
				if i + 1 < n and j + 1 == n:
					pass
				if j + 1 < n and i + 1 == n:
					pass
				if i + 1 < n and j + 1 < n:
					if s[i][j + 1] == '1' or s[i + 1][j] == '1':
						flag = True
					else:
						flag = False
						break
		if flag:
			pass
		else:
			break
	if flag:
		print('YES')
	else:
		print('NO')


def valid(matrix):
	for y in range(len(matrix)):
		for x in range(len(matrix)):
			if matrix[y][x] == 1:
				cnt = 0
				a = 0
				if x < len(matrix) - 1:
					a += 1
					if matrix[y][x + 1] == 1:
						cnt += 1
				else:
					cnt += 1
				if y < len(matrix) - 1:
					a += 1
					if matrix[y + 1][x] == 1:
						cnt += 1
				else:
					cnt += 1
				if cnt == 0 and a != 0:
					return False
	return True
t = int(input())
for i in range(t):
	matrix = []
	n = int(input())
	for _ in range(n):
		matrix.append([int(x) for x in input()])
	if valid(matrix):
		print('YES')
	else:
		print('NO')


def task(n, grid):
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if grid[i][j] == 1 and grid[i + 1][j] == 0 and (grid[i][j + 1] == 0):
				print('NO')
				return
	print('YES')
t = int(input())
for i in range(0, t):
	n = int(input())
	grid = []
	for j in range(0, n):
		row = [int(c) for c in input()]
		grid.append(row)
	task(n, grid)


for _ in range(int(input())):
	N = int(input())
	matrix = []
	for i in range(N):
		s = list(input())
		matrix.append(s)
	if N == 1:
		print('YES')
	else:
		temp = 0
		for i in range(N - 1):
			for j in range(N - 1):
				if matrix[i][j] == '1' and matrix[i][j + 1] == '0' and (matrix[i + 1][j] == '0'):
					temp = 1
					break
			if temp == 1:
				break
		if temp == 1:
			print('NO')
		else:
			print('YES')


def solve():
	n = int(input())
	polygon = [None for i in range(n)]
	for i in range(n):
		polygon[i] = list(map(int, input()))
	for i in range(n - 2, -1, -1):
		for j in range(i, -1, -1):
			if polygon[i][j] == 1:
				if polygon[i][j + 1] == 0 and polygon[i + 1][j] == 0:
					print('NO')
					return 0
		for j in range(i, -1, -1):
			if polygon[j][i] == 1:
				if polygon[j][i + 1] == 0 and polygon[j + 1][i] == 0:
					print('NO')
					return 0
	print('YES')
	return 0

def main():
	t = int(input())
	for i in range(t):
		solve()
	return 0
main()


t = int(input())
for _ in range(t):
	n = int(input())
	flag = 0
	a = []
	for _ in range(n):
		a.append(input())
	for i in range(n - 2, -1, -1):
		for j in range(n - 2, -1, -1):
			if a[i][j] == '1':
				if a[i][j + 1] != '1' and a[i + 1][j] != '1':
					print('NO')
					flag = 1
					break
		if flag == 1:
			break
	if flag == 0:
		print('YES')


t = int(input())
for i in range(t):
	f = lambda : map(int, input().split(' '))
	n = int(input())
	l = []
	for i in range(n):
		a = input()
		l.append(a)
	fl = 0
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			if l[i][j] == '1' and l[i + 1][j] == '0' and (l[i][j + 1] == '0'):
				print('NO')
				fl = 1
				break
		if fl == 1:
			break
	if fl == 0:
		print('YES')


cases = int(input())
for case in range(cases):
	n = int(input())
	a = []
	for _ in range(n):
		a.append([i for i in input()])
	ans = True
	for i in range(n):
		for j in range(n):
			if a[i][j] == '1':
				if i == n - 1 or j == n - 1:
					continue
				elif a[i + 1][j] == '1' or a[i][j + 1] == '1':
					continue
				else:
					ans = False
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin
from collections import deque

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	return prime
input = stdin.readline

def list_input():
	return list(map(int, input().split()))

def sep_input():
	return map(int, input().split())

def factors(n, k):
	l = []
	m = 1
	for i in range(1, min(k, ceil(sqrt(n))) + 1):
		if n % i == 0:
			if n // i == i:
				if i > m and i <= k:
					m = i
			else:
				if i > m and i <= k:
					m = i
				if n // i > m and n // i <= k:
					m = n // i
	return m
for _ in range(int(input())):
	n = int(input())
	l = []
	visited = [[False for i in range(n)] for j in range(n)]
	for i in range(n):
		temp = [int(i) for i in input()[:-1]]
		l.append(temp[:])
	q = deque()
	for i in range(n):
		if l[i][n - 1] == 1:
			q.append((i, n - 1))
		if l[n - 1][i] == 1:
			q.append((n - 1, i))
	while len(q) != 0:
		a = q.popleft()
		r = a[0]
		c = a[1]
		if visited[r][c]:
			continue
		else:
			visited[r][c] = True
			if r - 1 >= 0:
				if l[r - 1][c] == 1:
					q.append((r - 1, c))
			if c - 1 >= 0:
				if l[r][c - 1] == 1:
					q.append((r, c - 1))
	f = True
	for i in range(n):
		for j in range(n):
			if l[i][j] == 1 and visited[i][j] == False:
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


n = int(input())

def read(m):
	a = []
	for _ in range(m):
		row = input().strip()
		a.append(list(row))
	return a
for _ in range(n):
	m = int(input())
	arr = read(m)
	s = []
	for i in range(m):
		if arr[i][m - 1] == '1':
			s.append((i, m - 1))
		if arr[m - 1][i] == '1':
			s.append((m - 1, i))
	while s:
		(x, y) = s.pop()
		if arr[x][y] == '1':
			arr[x][y] = '0'
		else:
			continue
		for (x_d, y_d) in [(-1, 0), (0, -1)]:
			new_x = x + x_d
			new_y = y + y_d
			if 0 <= new_x < m and 0 <= new_y < m and (arr[new_x][new_y] == '1'):
				s.append((new_x, new_y))

	def check(arr):
		for i in range(m):
			for j in range(m):
				if arr[i][j] == '1':
					print('NO')
					return
		print('YES')
	check(arr)


for case in range(int(input())):
	n = int(input())
	g = []
	for i in range(n):
		g.append(list(map(int, list(input()))))
	q = []
	flg = True
	use = [[False for i in range(n)] for j in range(n)]
	for i in range(n - 1, -1, -1):
		if g[i][n - 1] == 1:
			q.append((i, n - 1))
		if g[n - 1][i] == 1:
			q.append((n - 1, i))
	while len(q):
		(x, y) = q.pop(0)
		if use[x][y]:
			continue
		use[x][y] = True
		if x - 1 >= 0 and g[x - 1][y] == 1:
			q.append((x - 1, y))
		if y - 1 >= 0 and g[x][y - 1] == 1:
			q.append((x, y - 1))
	for i in range(n):
		for j in range(n):
			if not use[i][j] and g[i][j] == 1:
				flg = False
	print('YES' if flg else 'NO')


for _ in range(int(input())):
	n = int(input())
	arr = []
	for row in range(n):
		arr.append(input())
	ans = 'YES'
	for r in range(n - 2, -1, -1):
		for c in range(n - 2, -1, -1):
			if arr[r][c] == '1' and (not (arr[r + 1][c] == '1' or arr[r][c + 1] == '1')):
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)


from math import *
t = int(input())
while t > 0:
	n = int(input())
	a = []
	for i in range(n):
		s = input()
		a.append(s)
	flag = 0
	for i in range(n - 1):
		for j in range(n - 1):
			if a[i][j] == '1':
				if a[i][j + 1] != '1' and a[i + 1][j] != '1':
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


import math
for z in range(int(input())):
	n = int(input())
	can = []
	ans = 1
	for i in range(n):
		can.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if can[i][j] == '1':
				if can[i][j + 1] != '1':
					if can[i + 1][j] != '1':
						ans = 0
						break
	if ans == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	q = []
	f = 0
	for i in range(n):
		q.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if q[i][j] == '1':
				if q[i + 1][j] == '1' or q[i][j + 1] == '1':
					f = 0
				else:
					f = 1
					break
		if f == 1:
			break
	if f == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	c = int(input())
	f = []
	for i in range(c):
		g = []
		for j in input():
			g.append(int(j))
		f.append(g)
	kl = False
	for i in range(c - 1):
		hj = True
		for j in range(c - 1):
			if f[i][j] == 1:
				if not (f[i + 1][j] or f[i][j + 1]):
					kl = True
					hj = False
					break
		if not hj:
			break
	if kl:
		print('NO')
	else:
		print('YES')


from math import *
from copy import *
from heapq import *
from string import *
from random import *
from bisect import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
from collections import defaultdict

def solve(l):
	n = len(l)
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i + 1][j] == '0' and l[i][j + 1] == '0':
					return 'NO'
	return 'YES'
T = int(input())
for _ in range(T):
	N = int(stdin.readline())
	L = []
	for i in range(N):
		L.append(list(input()))
	print(solve(L))


t = int(input())
for _ in range(t):
	n = int(input())
	gd = [input() for i in range(n)]
	ans = True
	for i in range(n - 1):
		for j in range(n - 1):
			if gd[i][j] == '1':
				if not gd[i][j + 1] == '1' and (not gd[i + 1][j] == '1'):
					ans = False
					break
		if not ans:
			break
	print('YES' if ans else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [[] for i in range(n)]
	for i in range(n):
		a[i] = list(map(int, list(input())))
	flag = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if i == 0 and j == 0:
				if a[i][j] == 1 and (not (a[i][j + 1] == 1 or a[i + 1][j] == 1)):
					flag = 0
					break
			if i == 0:
				if a[i][j] == 1 and (not (a[i][j + 1] == 1 or a[i + 1][j] == 1)):
					flag = 0
					break
			if j == 0:
				if a[i][j] == 1 and (not (a[i + 1][j] == 1 or a[i][j + 1] == 1)):
					flag = 0
					break
			elif a[i][j] == 1 and (not (a[i + 1][j] == 1 or a[i][j + 1] == 1)):
				flag = 0
				break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(input())
	fr = []
	for i in range(n):
		fr.append(['0'] * n)
	for i in range(n):
		for j in range(n):
			if ar[i][j] == '1':
				if j == n - 1 or i == n - 1:
					fr[i][j] = '1'
				elif ar[i + 1][j] == '1' or ar[i][j + 1] == '1':
					fr[i][j] = '1'
	bl = False
	for i in range(n):
		for j in range(n):
			if fr[i][j] != ar[i][j]:
				bl = True
	if bl == True:
		print('NO')
	else:
		print('YES')


import sys, math, os
from io import BytesIO, IOBase
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1000000000.0) + 7

def main():
	for t in range(int(data())):
		n = int(data())
		mat = []
		for i in range(n):
			mat.append(list(data()))
		flag = True
		for i in range(n):
			for j in range(n):
				k = 0
				if mat[i][j] == '1' and i < n - 1 and (j < n - 1):
					if mat[i + 1][j] == '1' or mat[i][j + 1] == '1':
						k = 1
					if k == 0:
						flag = False
						break
			if flag == False:
				break
		if flag == False:
			out('NO')
		else:
			out('YES')
main()


t = int(input())

def check(arr, n):
	ans = 'YES'
	for row in range(n - 1):
		for col in range(n - 1):
			if arr[row][col] == 1:
				if arr[row + 1][col] == 0 and arr[row][col + 1] == 0:
					ans = 'NO'
					return ans
	return ans
for i in range(t):
	n = int(input())
	arr = [[0 for j in range(n)] for k in range(n)]
	for p in range(n):
		arr[p] = [int(char) for char in input()]
	print(check(arr, n))


def solve(a, k):
	for i in range(k - 1):
		for j in range(k - 1):
			if a[i][j] == '1':
				if a[i][j + 1] != '1' and a[i + 1][j] != '1':
					print('NO')
					return
	print('YES')
for _ in range(int(input())):
	k = int(input())
	a = []
	for i in range(k):
		a.append(input())
	if k == 1:
		print('YES')
		continue
	solve(a, k)


test = int(input())
for t in range(test):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	status = 'Yes'
	for i in range(n):
		for j in range(n):
			if int(arr[i][j]) == 1:
				if i == n - 1 or j == n - 1 or int(arr[i][j + 1]) == 1 or (int(arr[i + 1][j]) == 1):
					continue
				else:
					status = 'NO'
	print(status)


import sys
import math
from collections import defaultdict as dd
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(input())
	l = []
	flg = 0
	for i in range(n):
		l.append(input())
	for i in range(n - 1):
		for j in range(n - 1):
			if l[i][j] == '1':
				if l[i][j + 1] != '1' and l[i + 1][j] != '1':
					flg = 1
					break
		if flg:
			break
	if flg:
		print('NO')
	else:
		print('YES')


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	n = ri()
	grid = []
	for i in range(n):
		grid.append([int(c) for c in input()])
	for row in range(n - 2, -1, -1):
		for col in range(n - 2, -1, -1):
			if grid[row][col] == 0:
				continue
			if grid[row + 1][col] == 0 and grid[row][col + 1] == 0:
				print('NO')
				return
	print('YES')
	return
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


t = int(input())
for k in range(t):
	n = int(input())
	mat = [input() for i in range(n)]
	flag = 0
	for i in range(n):
		for j in range(n):
			if int(mat[i][j]) == 0:
				if i == n - 1 and j == n - 1:
					break
				else:
					continue
			elif i == n - 1 and j == n - 1:
				break
			elif i == n - 1:
				continue
			elif j == n - 1:
				continue
			elif int(mat[i][j + 1]) == 1 or int(mat[i + 1][j]) == 1:
				continue
			else:
				flag = 1
				break
		if flag == 1:
			print('NO')
			break
		else:
			continue
	if flag == 0:
		print('YES')
		continue


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

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

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' ', end='\n'):
	print(sep.join(map(str, a)), end=end)

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
for _ in range(ii()):
	n = ii()
	d = []
	for i in range(n):
		s = input()
		line = [int(x) for x in s]
		d.append(line)
	f = 1
	for i in range(n - 1):
		for j in range(n - 1):
			if d[i][j] == 1:
				if d[i + 1][j] != 1 and d[i][j + 1] != 1:
					f = 0
					break
		if not f:
			break
	print('YES' if f else 'NO')


def solve():
	n = int(input())
	matrix = [list(map(int, input())) for i in range(n)]
	for i in range(0, n):
		for j in range(0, n):
			if matrix[i][j] == 1:
				if i != n - 1 and j != n - 1:
					if matrix[i + 1][j] != 1 and matrix[i][j + 1] != 1:
						return 'NO'
	return 'YES'
t = int(input())
for T in range(t):
	ans = solve()
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	poly = [input() for _ in range(n)]
	ans = 'YES'
	flag = False
	for i in range(n - 1):
		for j in range(n - 1):
			if poly[i][j] == '1' and poly[i][j + 1] == '0' and (poly[i + 1][j] == '0'):
				ans = 'No'
				flag = True
				break
		if flag:
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	f = []
	b = True
	for j in range(n):
		f.append(input())
	for r in range(n - 1):
		s = f[r]
		for c in range(n - 1):
			if s[c] == '1':
				if s[c + 1] != '1' and f[r + 1][c] != '1':
					b = False
					break
		if not b:
			break
	if b:
		print('YES')
	else:
		print('NO')


class Solution:

	def __init__(self):
		test = int(input())
		for i in range(0, test):
			n = int(input())
			a = [input() for i in range(0, n)]
			print(self.solve(n, a))

	def solve(self, n, a):
		q = [None for i in range(0, 60 * 60)]
		used = [[False for i in range(0, n)] for j in range(0, n)]
		l = r = 0
		p = [(0, -1), (-1, 0)]
		for i in range(0, n):
			if a[i][n - 1] == '1':
				q[r] = (i, n - 1)
				r += 1
				used[i][n - 1] = True
			if a[n - 1][i] == '1' and i < n - 1:
				q[r] = (n - 1, i)
				r += 1
				used[n - 1][i] = True
		while l < r:
			u = q[l]
			l += 1
			for i in p:
				v = (u[0] + i[0], u[1] + i[1])
				if min(v[0], v[1]) >= 0 and max(v[0], v[1]) < n and (not used[v[0]][v[1]]) and (a[v[0]][v[1]] == '1'):
					q[r] = v
					r += 1
					used[v[0]][v[1]] = True
		for i in range(0, n):
			for j in range(0, n):
				if a[i][j] == '1' and (not used[i][j]):
					return 'NO'
		return 'YES'
Solution()


def dfs(arr, visited, i, j):
	visited[i][j] = 1
	if j > 0 and arr[i][j - 1] == 1 and (visited[i][j - 1] == 0):
		dfs(arr, visited, i, j - 1)
	if i > 0 and arr[i - 1][j] == 1 and (visited[i - 1][j] == 0):
		dfs(arr, visited, i - 1, j)
for t in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		li = [int(x) for x in input()]
		arr.append(li)
	visited = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		if arr[n - 1][i] == 1 and visited[n - 1][i] == 0:
			dfs(arr, visited, n - 1, i)
	for j in range(n):
		if arr[j][n - 1] == 1 and visited[j][n - 1] == 0:
			dfs(arr, visited, j, n - 1)
	flag = False
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 1 and visited[i][j] == 0:
				flag = True
				print('NO')
				break
		if flag == True:
			break
	if flag == True:
		continue
	print('YES')