You are given a grid with $R$ rows (numbered $1$ through $R$) and $C$ columns (numbered $1$ through $C$). A cell in row $r$ and column $c$ is denoted by $(r, c)$. Two cells in the grid are adjacent if they have a common side. For each valid $i$ and $j$, there is a value $a_{i, j}$ written in cell $a_{i, j}$.
A cell in the grid is stable if the number of cells in the grid which are adjacent to this cell is strictly greater than the value written in this cell. The whole grid is stable if all cells in the grid are stable.
Can you determine whether the grid is stable?

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $R$ and $C$.
- $R$ lines follow. For each $i$ ($1 \le i \le R$), the $i$-th of these lines contains $C$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, C}$.

-----Output-----
For each test case, print a single line containing the string "Stable" if the grid is stable or "Unstable" if it is unstable (without quotes).

-----Constraints-----
- $1 \le T \le 3,000$
- $3 \le R, C \le 10$
- $0 \le a_{i, j} \le 4$ for each valid $i, j$

-----Example Input-----
2
3 3
1 2 1
2 3 2
1 2 1
3 4
0 0 0 0
0 0 0 0
0 0 4 0

-----Example Output-----
Stable
Unstable

-----Explanation-----
Example case 1: Each cell of the grid is stable, so the grid is stable.
Example case 2: The cell in row $3$ and column $3$ is unstable since the number of cells adjacent to this cell is $3$.

Difficulty: EASY

Solutions:
for i in range(int(input())):
	rc = list(map(int, input().split()))
	matrix = []
	for i in range(rc[0]):
		row = list(map(int, input().split()))
		matrix.append(row)
		x = 0
		y = 0
		counter = 0
	for i in range(rc[0] * rc[1]):
		cell = matrix[y][x]
		if y == 0 and x == 0 or (y == 0 and x == rc[1] - 1) or (y == rc[0] - 1 and x == 0) or (y == rc[0] - 1 and x == rc[1] - 1):
			if cell < 2:
				counter += 1
		if y == 0 and x < rc[1] - 1 and (x > 0) or (y > 0 and y < rc[0] - 1 and (x == 0)) or (x == rc[1] - 1 and y > 0 and (y < rc[0] - 1)) or (y == rc[0] - 1 and x > 0 and (x < rc[1] - 1)):
			if cell < 3:
				counter += 1
		if y > 0 and y < rc[0] - 1 and (x > 0) and (x < rc[1] - 1):
			if cell < 4:
				counter += 1
		x += 1
		if x == rc[1]:
			x = 0
			y += 1
	if counter == rc[0] * rc[1]:
		print('Stable')
	else:
		print('Unstable')


import numpy as np
t = int(input())
for t in range(0, t):
	q = list(map(int, input().rstrip().split()))
	r = q[0]
	c = q[1]
	sum = 0
	a = np.array([[int(x) for x in input().split()] for i in range(r)])
	if a[0][0] < 2 and a[0][c - 1] < 2 and (a[r - 1][0] < 2) and (a[r - 1][c - 1] < 2):
		sum += 4
	for j in range(1, c - 1):
		if a[0][j] < 3 and a[r - 1][j] < 3:
			sum = sum + 2
	for i in range(1, r - 1):
		if a[i][0] < 3 and a[i][c - 1] < 3:
			sum = sum + 2
	for j in range(1, r - 1):
		for i in range(1, c - 1):
			if a[j][i] < 4:
				sum = sum + 1
	if sum == r * c:
		print('Stable')
	else:
		print('Unstable')


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	a = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	cont = True
	for i in range(r):
		for j in range(c):
			if i == 0 and j == c - 1 or (i == r - 1 and j == 0) or (i == r - 1 and j == c - 1) or (i == 0 and j == 0):
				if a[i][j] > 1:
					cont = False
					break
			elif i == 0 or i == r - 1 or j == 0 or (j == c - 1):
				if a[i][j] > 2:
					cont = False
					break
			elif a[i][j] > 3:
				cont = False
				break
		if cont == False:
			print('Unstable')
			break
	else:
		print('Stable')


def check(r, c, a):
	for i in range(r):
		for j in range(c):
			adj = 4
			if i == 0 or i == r - 1:
				adj -= 1
			if j == 0 or j == c - 1:
				adj -= 1
			if a[i][j] >= adj:
				return 'Unstable'
	return 'Stable'
for _ in range(int(input())):
	d = input().split()
	r = int(d[0])
	c = int(d[1])
	a = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	print(check(r, c, a))


t = int(input())
for _ in range(t):
	(m, n) = map(int, input().split())
	a = [[0] * n] * m
	s = [0] * n
	s1 = [0] * n
	for j in range(n):
		if j == 0 or j == n - 1:
			s[j] = 2
			s1[j] = 3
		else:
			s[j] = 3
			s1[j] = 4
	for i in range(m):
		if i == 0 or i == m - 1:
			a[i] = s
		else:
			a[i] = s1
	flag = True
	inp = [[0] * n] * m
	for i in range(m):
		inp[i] = list(map(int, input().split()))
	for i in range(m):
		for j in range(n):
			if a[i][j] <= inp[i][j]:
				flag = False
				break
	if flag:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	arr = []
	for _ in range(n):
		a = list(map(int, input().split()))
		arr.append(a)
	f = 0
	for i in range(n):
		for j in range(m):
			if i == 0 and j == 0 or (i == 0 and j == m - 1) or (i == n - 1 and j == 0) or (i == n - 1 and j == m - 1):
				if arr[i][j] < 2:
					f = 0
				else:
					f = 1
					break
			elif i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				if arr[i][j] < 3:
					f = 0
				else:
					f = 1
					break
			elif arr[i][j] < 4:
				f = 0
			else:
				f = 1
				break
		if f == 1:
			break
	if f == 1:
		print('Unstable')
	else:
		print('Stable')


t = int(input())
for kk in range(0, t):
	(r, c) = map(int, input().split())
	grid = []
	condition = True
	for jj in range(0, r):
		grid = list(map(int, input().split()))
		if condition:
			if jj == 0 or jj == r - 1:
				if grid[0] > 1 or grid[c - 1] > 1:
					condition = False
				for j in range(1, c - 1):
					if grid[j] > 2:
						condition = False
			else:
				if grid[0] > 2 or grid[c - 1] > 2:
					condition = False
				for j in range(1, c - 1):
					if grid[j] > 3:
						condition = False
	if condition:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for ni in range(t):
	(r, c) = map(int, input().split())
	l = list()
	for nj in range(r):
		a = list(map(int, input().split()))
		l.append(a)
	count = 0
	adj = 3
	for i in range(r):
		for j in range(c):
			if i == 0 and j == 0 or (i == 0 and j == c - 1) or (j == 0 and i == r - 1) or (i == r - 1 and j == c - 1):
				adj = 2
			elif i == 0 or i == r - 1 or j == 0 or (j == c - 1):
				adj = 3
			else:
				adj = 4
			if l[i][j] >= adj:
				count = 1
				break
	if count == 0:
		print('Stable')
	else:
		print('Unstable')


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	l = []
	flag = True
	for i in range(r):
		a = list(map(int, input().split()))
		l.append(a)
	for i in range(r):
		for j in range(c):
			count = 4
			if i == 0:
				count -= 1
			if j == 0:
				count -= 1
			if i == r - 1:
				count -= 1
			if j == c - 1:
				count -= 1
			if count <= l[i][j]:
				flag = False
				break
	if flag:
		print('Stable')
	else:
		print('Unstable')


def readints():
	s = input()
	return list(map(int, s.split()))
T = int(input())
for i in range(T):
	(r, c) = readints()
	m = [readints() for i in range(r)]
	b = True
	for i in range(r):
		for j in range(c):
			nbs = 0
			if i + 1 < r:
				nbs += 1
			if i - 1 >= 0:
				nbs += 1
			if j + 1 < c:
				nbs += 1
			if j - 1 >= 0:
				nbs += 1
			b = b and m[i][j] < nbs
	if b:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for i in range(t):
	matrix = list()
	(r, c) = list(map(int, input().split()))
	for j in range(r):
		row = list(map(int, input().split()))
		matrix.append(row)
	count = 0
	for j in range(r):
		for k in range(c):
			if j == 0 and k in [0, c - 1] or (j == r - 1 and k in [0, c - 1]):
				if matrix[j][k] < 2:
					count += 1
			elif 0 < k < c - 1 and j in [0, r - 1] or (0 < j < r - 1 and k in [0, c - 1]):
				if matrix[j][k] < 3:
					count += 1
			elif matrix[j][k] < 4:
				count += 1
	if count == r * c:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for _ in range(t):
	(r, c) = map(int, input().split())
	a = []
	for i in range(r):
		temp = list(map(int, input().split()))
		a.append(temp)
	flag = True
	for i in range(r):
		for j in range(c):
			count = 0
			if i + 1 < r:
				count += 1
			if j + 1 < c:
				count += 1
			if i - 1 >= 0:
				count += 1
			if j - 1 >= 0:
				count += 1
			if count > a[i][j]:
				pass
			else:
				flag = False
	if flag:
		ans = 'Stable'
	else:
		ans = 'Unstable'
	print(ans)


def adjacent(i, j, r, c):
	if i == 0 or i == r - 1:
		if j == 0 or j == c - 1:
			return 2
		else:
			return 3
	elif j == 0 or j == c - 1:
		return 3
	else:
		return 4
for _ in range(int(input())):
	(r, c) = map(int, input().split(' '))
	grid = []
	flag = 0
	for i in range(r):
		grid.append(list(map(int, input().split(' '))))
	for i in range(r):
		for j in range(c):
			if adjacent(i, j, r, c) <= grid[i][j]:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('Unstable')
	else:
		print('Stable')


T = int(input())
for _ in range(T):
	(ROWS, COLS) = map(int, input().split(' '))
	grid = []
	for r in range(ROWS):
		rr = map(int, input().split(' '))
		grid.append(list(rr))
	stable = True
	for r in range(ROWS):
		for c in range(COLS):
			n_adj = 0
			if r + 1 in range(ROWS):
				n_adj += 1
			if r - 1 in range(ROWS):
				n_adj += 1
			if c + 1 in range(COLS):
				n_adj += 1
			if c - 1 in range(COLS):
				n_adj += 1
			if grid[r][c] >= n_adj:
				stable = False
				break
	if stable:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for t in range(t):
	(r, c) = [int(i) for i in input().split()]
	arr = []
	for i in range(r):
		arr.append([int(i) for i in input().split()])
	flag = 1
	for i in range(r):
		for j in range(c):
			if (i < r - 1 and i > 0) and (j < c - 1 and j > 0):
				if arr[i][j] > 3:
					flag = 0
					break
			elif i == 0 and (j == 0 or j == c - 1) or (i == r - 1 and (j == 0 or j == c - 1)):
				if arr[i][j] > 1:
					flag = 0
					break
			elif arr[i][j] > 2:
				flag = 0
				break
	if flag == 1:
		print('Stable')
	else:
		print('Unstable')


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	a = []
	flag = False
	for i in range(r):
		a.append([int(i) for i in input().split()])
	for i in [0, r - 1]:
		for j in range(c):
			if j in [0, c - 1]:
				if a[i][j] > 1:
					flag = True
					break
			elif a[i][j] > 2:
				flag = True
				break
		if flag:
			break
	if flag:
		print('Unstable')
	else:
		for i in range(1, r - 1):
			for j in range(c):
				if j in [0, c - 1]:
					if a[i][j] > 2:
						flag = True
						break
				elif a[i][j] > 3:
					flag = True
					break
			if flag:
				break
		if flag:
			print('Unstable')
		else:
			print('Stable')


t = int(input())
while t != 0:
	(r, c) = map(int, input().split())
	l = []
	f = 0
	for x in range(r):
		ls = list(map(int, input().split()))
		l.append(ls)
	for i in range(r):
		for j in range(c):
			if i == 0 or i == r - 1 or j == 0 or (j == c - 1):
				if (i == 0 or i == r - 1) and (j == 0 or j == c - 1):
					if l[i][j] > 1:
						f = 1
						break
				elif l[i][j] > 2:
					f = 1
					break
			elif l[i][j] > 3:
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('Stable')
	else:
		print('Unstable')
	t -= 1


check = [2, 3, 4]
for _ in range(int(input())):
	(m, n) = map(int, input().split())
	ans = 'Stable'
	for j in range(m):
		a = [int(x) for x in input().split()]
		if ans == 'Stable':
			if j in {0, m - 1}:
				(b, c) = (check[0], check[1])
			else:
				(b, c) = (check[1], check[2])
			for i in range(n):
				if i in {0, n - 1}:
					if a[i] >= b:
						ans = 'Unstable'
						break
				elif a[i] >= c:
					ans = 'Unstable'
					break
	print(ans)


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	p = 0
	ar = list(map(int, input().split()))
	for i in range(1, c - 1):
		if ar[0] > 1 or ar[c - 1] > 1:
			p = 1
		elif ar[i] > 2:
			p = 1
	for j in range(1, r - 1):
		ar = list(map(int, input().split()))
		if ar[0] > 2 or ar[c - 1] > 2:
			p = 1
		for k in range(1, c - 1):
			if ar[k] > 3:
				p = 1
	ar = list(map(int, input().split()))
	for l in range(1, c - 1):
		if ar[l] > 2:
			p = 1
			break
	if ar[0] > 1 or ar[c - 1] > 1:
		p = 1
	if p == 1:
		print('Unstable')
	else:
		print('Stable')


for tc in range(int(input())):
	(r, c) = map(int, input().split())
	grid = []
	for i in range(r):
		grid.append(list(map(int, input().split())))
	ans = 'Stable'
	for i in range(1, r + 1):
		for j in range(1, c + 1):
			adj = 2 - (j == 1) - (j == c) + 2 - (i == 1) - (i == r)
			if grid[i - 1][j - 1] >= adj:
				ans = 'Unstable'
				break
	print(ans)


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	a = []
	for i in range(r):
		a += [list(map(int, input().split()))]
	b = 0
	for i in range(r):
		for j in range(c):
			if i == 0 and j == 0 or (i == 0 and j == c - 1) or (i == r - 1 and j == 0) or (i == r - 1 and j == c - 1):
				if a[i][j] > 1:
					b = 1
					break
			elif i == 0 or j == 0 or i == r - 1 or (j == c - 1):
				if a[i][j] > 2:
					b = 1
					break
			elif a[i][j] > 3:
				b = 1
				break
		if b == 1:
			print('Unstable')
			break
	else:
		print('Stable')


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	a = []
	for x in range(r):
		temp = list(map(int, input().split()))
		a.append(temp)
	ans = True
	for i in range(r):
		for j in range(c):
			count = 0
			if i + 1 < r:
				count += 1
			if j + 1 < c:
				count += 1
			if i - 1 >= 0:
				count += 1
			if j - 1 >= 0:
				count += 1
			if count > a[i][j]:
				continue
			else:
				ans = False
				break
	if ans:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for _ in range(t):
	(r, c) = map(int, input().split())
	ar = []
	for i in range(r):
		l = list(map(int, input().split()))
		ar.append(l)
	cond = True
	for i in range(r):
		for j in range(c):
			n = 0
			if i + 1 in range(r):
				n += 1
			if i - 1 in range(r):
				n += 1
			if j + 1 in range(c):
				n += 1
			if j - 1 in range(c):
				n += 1
			if ar[i][j] >= n:
				cond = False
				break
	if cond:
		print('Stable')
	else:
		print('Unstable')


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	l = []
	for k in range(r):
		a = list(map(int, input().split()))
		l.append(a)
	ans = 'Stable'
	for i in range(r):
		for j in range(c):
			p = l[i][j]
			count = 0
			if i - 1 >= 0 and j >= 0:
				count += 1
			if i >= 0 and j - 1 >= 0:
				count += 1
			if i + 1 <= r - 1 and j <= c - 1:
				count += 1
			if i <= r - 1 and j + 1 <= c - 1:
				count += 1
			if count <= p:
				ans = 'Unstable'
				break
	print(ans)


T = int(input())
for i in range(T):
	(n, m) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(map(int, input().split())))
	flag = 0
	for i in range(n):
		for j in range(m):
			c = 0
			if i - 1 >= 0 and i - 1 < n:
				c = c + 1
			if i + 1 > 0 and i + 1 < n:
				c = c + 1
			if j - 1 >= 0 and j - 1 < m:
				c = c + 1
			if j + 1 > +0 and j + 1 < m:
				c = c + 1
			if c <= mat[i][j]:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('Unstable')
	else:
		print('Stable')


import math
t = int(input())
for i in range(t):
	(r, c) = list(map(int, input().split()))
	res = []
	for j in range(r):
		l = list(map(int, input().split()))
		res.append(l)
	flag = 1
	for a in range(r):
		for b in range(c):
			adj = 4
			if a == 0 or a == r - 1:
				adj -= 1
			if b == 0 or b == c - 1:
				adj -= 1
			if res[a][b] >= adj:
				flag = 0
				break
	if flag:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
for case in range(t):
	(r, c) = [int(i) for i in input().split()]
	mat = []
	for i in range(r):
		mat.append([int(i) for i in input().split()])
	unstable = False
	for row in range(r):
		if unstable:
			break
		for col in range(c):
			if row == 0 and col == 0 or (row == 0 and col == c - 1) or (row == r - 1 and col == 0) or (row == r - 1 and col == c - 1):
				if mat[row][col] > 1:
					unstable = True
					print('Unstable')
					break
			elif row == 0 or row == r - 1 or col == 0 or (col == c - 1):
				if mat[row][col] > 2:
					unstable = True
					print('Unstable')
					break
			elif mat[row][col] > 3:
				unstable = True
				print('Unstable')
				break
	if not unstable:
		print('Stable')


t = int(input())

def numOfCordinates(arr, raw_arr, r, c):
	num = 0
	possibleCords = [[r - 1, c], [r + 1, c], [r, c + 1], [r, c - 1]]
	for cords in possibleCords:
		if cords in raw_arr:
			num += 1
	return num
for _ in range(1, t + 1):
	(r, c) = map(int, input().split(' '))
	arr = []
	for i in range(r):
		ri = list(map(int, input().split(' ')))[:c]
		arr.append(ri)
	raw_arr = []
	for ri in range(r):
		for ci in range(c):
			raw_arr.append([ri, ci])
	for r in range(len(arr)):
		for c in range(len(arr[r])):
			no_cords = numOfCordinates(arr, raw_arr, r, c)
			if no_cords == -1:
				arr[r][c] = -1
			elif arr[r][c] < no_cords:
				arr[r][c] = 0
			else:
				arr[r][c] = -1
	s = 0
	for ls in arr:
		s += sum(ls)
	if s == 0:
		print('Stable')
	else:
		print('Unstable')


def fun(a, r, c):
	s = 'Unstable'
	for i in range(r):
		for j in range(c):
			if i == 0 and (j == c - 1 or j == 0):
				if a[i][j] > 1:
					return s
				else:
					continue
			elif i == 0:
				if a[i][j] > 2:
					return s
				else:
					continue
			elif i == r - 1 and (j == c - 1 or j == 0):
				if a[i][j] > 1:
					return s
				else:
					continue
			elif i == r - 1:
				if a[i][j] > 2:
					return s
				else:
					continue
			elif j == 0 or j == c - 1:
				if a[i][j] > 2:
					return s
				else:
					continue
			elif a[i][j] > 3:
				return s
	s = 'Stable'
	return s
for i in range(int(input())):
	(r, c) = [int(x) for x in input().split()]
	a = []
	for i in range(r):
		b = [int(x) for x in input().split()]
		a.append(b)
	print(fun(a, r, c))


import pandas as pd
subtask = int(input())
for _ in range(subtask):
	data = []
	(R, C) = [int(i) for i in input().split()]
	for i in range(R):
		data.append([int(i) for i in input().split()])
	F = 0
	for i in [[0, 0], [0, C - 1], [R - 1, 0], [R - 1, C - 1]]:
		if data[i[0]][i[1]] > 1:
			F = 1
			break
	if F == 0:
		for i in [0, R - 1]:
			for j in range(1, C):
				if data[i][j] > 2:
					F = 1
					break
	if F == 0:
		for i in [0, C - 1]:
			for j in range(1, R):
				if data[j][i] > 2:
					F = 1
					break
	if F == 0:
		for i in range(1, R):
			for j in range(1, C):
				if data[i][j] > 3:
					F = 1
					break
	if F == 0:
		print('Stable')
	else:
		print('Unstable')


for _ in range(int(input())):
	(R, C) = map(int, input().split())
	mx = []
	for i in range(R):
		mx.append(list(map(int, input().split())))
	f = 1
	for r in range(R):
		for c in range(C):
			if r - 1 < 0 and c - 1 < 0 or (r - 1 < 0 and c + 1 == C) or (r + 1 == R and c - 1 < 0) or (r + 1 == R and c + 1 == C):
				if mx[r][c] >= 2:
					f = 0
			elif r - 1 < 0 or r + 1 == R or c - 1 < 0 or (c + 1 == C):
				if mx[r][c] >= 3:
					f = 0
			elif mx[r][c] >= 4:
				f = 0
	if f:
		print('Stable')
	else:
		print('Unstable')


def answer():
	for i in range(n):
		for j in range(m):
			adj = 0
			if i - 1 >= 0:
				adj += 1
			if i + 1 < n:
				adj += 1
			if j - 1 >= 0:
				adj += 1
			if j + 1 < m:
				adj += 1
			if a[i][j] >= adj:
				return 'Unstable'
	return 'Stable'
for T in range(int(input())):
	(n, m) = map(int, input().split())
	a = [list(map(int, input().split())) for i in range(n)]
	print(answer())


def checkStability(inp_arr, R, C, i, j):
	cell_cnt = 0
	for coordinates in ((i, j - 1), (i, j + 1), (i - 1, j), (i + 1, j)):
		(r, c) = coordinates
		if r >= 0 and c >= 0 and (r < R) and (c < C):
			cell_cnt += 1
	if inp_arr[i][j] >= cell_cnt:
		return False
	return True
for _ in range(int(input())):
	(R, C) = (int(i) for i in input().strip(' ').split(' '))
	inp_arr = []
	stableFlag = True
	printStr = 'Stable'
	for i in range(R):
		inp_arr.append(tuple((int(i) for i in input().strip(' ').split(' '))))
	for i in range(R):
		if stableFlag == True:
			for j in range(C):
				stableFlag = checkStability(inp_arr, R, C, i, j)
				if stableFlag == False:
					printStr = 'Unstable'
					break
	print(printStr)


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	L = []
	for i in range(N):
		L.append(list(map(int, input().split())))
	flag = True
	for i in range(N):
		for j in range(M):
			k = 0
			if i - 1 >= 0:
				k += 1
			if i + 1 < N:
				k += 1
			if j - 1 >= 0:
				k += 1
			if j + 1 < M:
				k += 1
			if k <= L[i][j]:
				flag = False
				break
		if not flag:
			break
	print('Stable' if flag else 'Unstable')


def isStable(l):
	for i in range(r):
		for j in range(c):
			cnt = 0
			if i + 1 < r:
				cnt += 1
			if i - 1 >= 0:
				cnt += 1
			if j + 1 < c:
				cnt += 1
			if j - 1 >= 0:
				cnt += 1
			if l[i][j] >= cnt:
				return False
	return True
t = int(input())
while t:
	(r, c) = [int(i) for i in input().split()]
	l = []
	for j in range(r):
		l.append([int(i) for i in input().split()])
	print('Stable') if isStable(l) else print('Unstable')
	t -= 1


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	l = []
	for i in range(a):
		l.append(list(map(int, input().split())))
	for i in range(a):
		for j in range(b):
			if i == 0 or i == a - 1 or j == 0 or (j == b - 1):
				if (i == 0 or i == a - 1) and j == 0 or ((i == 0 or i == a - 1) and j == b - 1):
					k = 2
				else:
					k = 3
			else:
				k = 4
			if l[i][j] >= k:
				k = -1
				break
		if k == -1:
			print('Unstable')
			break
	else:
		print('Stable')


T = int(input())
for tj in range(T):
	(R, C) = map(int, input().split())
	grid = []
	for i in range(R):
		grid.append(list(map(int, input().split())))
	flag = True
	for i in range(R):
		for j in range(C):
			if (i == 0 or i == R - 1) and (j == 0 or j == C - 1):
				if grid[i][j] >= 2:
					flag = False
					break
			elif i == 0 or i == R - 1 or j == 0 or (j == C - 1):
				if grid[i][j] >= 3:
					flag = False
					break
			elif grid[i][j] >= 4:
				flag = False
				break
	if flag:
		print('Stable')
	else:
		print('Unstable')


t = int(input())
while t:
	f = 1
	(r, c) = map(int, input().split())
	mat = list()
	for i in range(r):
		temp = list(map(int, input().split()))
		mat.append(temp)
	for i in range(r):
		for j in range(c):
			Count = 4
			if i == 0:
				Count -= 1
			if j == 0:
				Count -= 1
			if i == r - 1:
				Count -= 1
			if j == c - 1:
				Count -= 1
			if Count <= mat[i][j]:
				f = 0
				break
		if j != c - 1:
			break
	if f:
		print('Stable')
	else:
		print('Unstable')
	t -= 1


def isvalid(i, j, r, c):
	if i >= 0 and i < r and (j >= 0) and (j < c):
		return True
	return False
t = int(input())
while t > 0:
	t -= 1
	(r, c) = [int(x) for x in input().split()]
	(matrix, flag) = ([], False)
	for i in range(r):
		matrix.append([int(x) for x in input().split()])
	for i in range(r):
		for j in range(c):
			count = 0
			if isvalid(i - 1, j, r, c):
				count += 1
			if isvalid(i, j - 1, r, c):
				count += 1
			if isvalid(i + 1, j, r, c):
				count += 1
			if isvalid(i, j + 1, r, c):
				count += 1
			if matrix[i][j] >= count:
				flag = True
				break
		if flag:
			break
	print('Unstable') if flag else print('Stable')


from math import *
import sys

def input():
	return sys.stdin.readline().replace('\n', '').strip()
sys.setrecursionlimit(10 ** 9)
for _ in range(int(input())):
	(r, c) = list(map(int, input().split()))
	l1 = []
	l2 = []
	for i in range(r):
		l = list(map(int, input().split()))
		l1.append(l)
		if i == 0 or i == r - 1:
			l2.append([2] + [3] * (c - 2) + [2])
		else:
			l2.append([3] + [4] * (c - 2) + [3])
	proceed = True
	for i in range(r):
		for j in range(c):
			if l1[i][j] >= l2[i][j]:
				proceed = False
	if proceed == False:
		print('Unstable')
	else:
		print('Stable')


from collections import Counter
for _ in range(int(input())):
	(m, n) = map(int, input().split())
	a = [list(map(int, input().split())) for i in range(m)]
	if max(a[0][0], a[0][n - 1], a[m - 1][0], a[m - 1][n - 1]) < 2:
		if max([a[0][i] for i in range(1, n)] + [a[m - 1][i] for i in range(1, n)] + [a[i][0] for i in range(1, m)] + [a[i][n - 1] for i in range(1, m)]) < 3:
			if max((a[i][j] for i in range(1, m) for j in range(1, n))) < 4:
				print('Stable')
			else:
				print('Unstable')
		else:
			print('Unstable')
	else:
		print('Unstable')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(list(map(int, input().split())))
	flag = 0
	for i in range(n):
		for j in range(m):
			c = 0
			if i - 1 >= 0:
				c += 1
			if i + 1 < n:
				c += 1
			if j - 1 >= 0:
				c += 1
			if j + 1 < m:
				c += 1
			if l[i][j] >= c:
				flag = 1
				break
		if flag:
			break
	if flag:
		print('Unstable')
	else:
		print('Stable')


t = int(input())
for _ in range(t):
	(r, c) = [int(x) for x in input().split()]
	a = []
	for i in range(r):
		temp = [int(x) for x in input().split()]
		a.append(temp)
	b = []
	for i in range(r):
		temp = [0] * c
		b.append(temp)
	for i in range(r):
		for j in range(c):
			if i == 0 or i == r - 1 or j == 0 or (j == c - 1):
				b[i][j] = 3
	b[0][0] -= 1
	b[0][c - 1] -= 1
	b[r - 1][c - 1] -= 1
	b[r - 1][0] -= 1
	for i in range(r):
		for j in range(c):
			if b[i][j] == 0:
				b[i][j] = 4
	f = 0
	for i in range(r):
		for j in range(c):
			if a[i][j] >= b[i][j]:
				f = 1
				break
		if f == 1:
			break
	if f == 1:
		print('Unstable')
	else:
		print('Stable')


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	ans = 'Stable'
	for j in range(n):
		l = list(map(int, input().split()))
		if j == n - 1 or j == 0:
			for z in range(k):
				if z == k - 1 or z == 0:
					if l[z] >= 2:
						ans = 'Unstable'
						break
				elif l[z] >= 3:
					ans = 'Unstable'
					break
		else:
			for x in range(k):
				if x == 0 or x == k - 1:
					if l[x] >= 3:
						ans = 'Unstable'
						break
				elif l[x] >= 4:
					ans = 'Unstable'
					break
	print(ans)


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	ans = 'Stable'
	for i in range(r):
		x = list(map(int, input().split()))
		if i == 0 or i == r - 1:
			for j in range(c):
				if j == 0 or j == c - 1:
					if x[j] >= 2:
						ans = 'Unstable'
						break
				elif x[j] >= 3:
					ans = 'Unstable'
					break
		else:
			for j in range(c):
				if j == 0 or j == c - 1:
					if x[j] >= 3:
						ans = 'Unstable'
						break
				elif x[j] >= 4:
					ans = 'Unstable'
					break
	print(ans)


from itertools import chain
tc = int(input())

def solve(arr, row, column):
	if arr[0][0] >= 2 or arr[0][column - 1] >= 2 or arr[row - 1][0] >= 2 or (arr[row - 1][column - 1] >= 2):
		return 'Unstable'
	elif max(arr[0]) >= 3 or max(arr[-1]) >= 3 or max((arr[n][0] for n in range(row))) >= 3 or (max((arr[n][-1] for n in range(row))) >= 3):
		return 'Unstable'
	elif max(chain(*arr)) >= 4:
		return 'Unstable'
	return 'Stable'
for _ in range(tc):
	(r, c) = map(int, input().split())
	arr = [list(map(int, input().split())) for _ in range(r)]
	print(solve(arr, r, c))


for _ in range(int(input())):
	(R, C) = map(int, input().split())
	A = []
	S = []
	for _ in range(R):
		S = list(map(int, input().split()))
		A.append(S)
	F = True
	for i in range(R):
		for j in range(C):
			if (i == 0 or i == R - 1) and (j == 0 or j == C - 1) and (A[i][j] > 1):
				F = False
				break
			elif (i == 0 or i == R - 1) and 0 < j < C - 1 and (A[i][j] > 2):
				F = False
				break
			elif (j == 0 or j == C - 1) and 0 < i < R - 1 and (A[i][j] > 2):
				F = False
				break
			elif 0 < j < C - 1 and 0 < i < R - 1 and (A[i][j] > 3):
				F = False
				break
	if F:
		print('Stable')
	else:
		print('Unstable')


def readints():
	s = input()
	return list(map(int, s.split()))
T = int(input())
for i in range(T):
	(r, c) = readints()
	m = [readints() for i in range(r)]
	b = m[0][0] < 2 and m[0][c - 1] < 2 and (m[r - 1][0] < 2) and (m[r - 1][c - 1] < 2)
	for j in range(1, c - 1):
		b = b and m[0][j] < 3
		b = b and m[r - 1][j] < 3
	for i in range(1, r - 1):
		b = b and m[i][0] < 3
		b = b and m[i][c - 1] < 3
	for i in range(1, r - 1):
		for j in range(1, c - 1):
			b = b and m[i][j] < 4
	if b:
		print('Stable')
	else:
		print('Unstable')