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: interview

Starter code:

Solutions:
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) 

# cook your dish here
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)


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
 else:
  if j==0 or j==c-1:
   return 3
  else:
   return 4
   
# cook your dish here
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")

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")
   
   
   


# cook your dish here
import math
t=int(input())
#t=1
#res=[]
for i in range(t):
 #n=int(input())
 r,c=list(map(int,input().split()))
 #l=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")


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))

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')

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")

# cook your dish here
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
   else:
    if a[i][j] >3:
     cont = False
     break
  if cont == False:
   print('Unstable')
   break
 else:
  print('Stable')