Kim has broken in to the base, but after walking in circles, perplexed by the unintelligible base design of the JSA, he has found himself in a large, empty, and pure white, room. 
The room is a grid with H∗W$H*W$ cells, divided into H$H$ rows and W$W$ columns. The cell (i,j)$(i,j)$ is at height A[i][j]$A[i][j]$. Unfortunately, his advanced sense of smell has allowed him to sense a mercury leak, probably brought in by Jishnu to end his meddling. The mercury leak has a power (which determines what height the mercury can reach before dissipating into harmless quantities) and a source cell. It spreads from cells it has already reached to other cells in the four cardinal directions: north, south, east, and west. (That is, the mercury can spread up, down, right, or left in the grid, but not diagonally.) Mercury can only spread to a cell if the cell's height is strictly less than the power value. 
Unfortunately, Kim does not exactly know the starting cell or the power value of the mercury leak. However, his impressive brain has determined that it must be one of Q$Q$ (power, starting cell) combinations. For each combination, he wants to find out how many cells are dangerous for him to go to: that is, how many cells will eventually be reached by the mercury. This will help him determine a suitable cell to stay in and slowly fix the leak from above.
Can you help Kim achieve this objective?
Note: If the starting cell's height is not less than the power level, the mercury immediately dissipates. So, in this case, output 0.

-----Input:-----
- First line will contain T$T$, number of testcases. Then the testcases follow. 
- The first line in each testcase contains three integers, H$H$, W$W$, and Q$Q$.
- On the 2$2$nd to (H+1)$(H+1)$th lines of each testcase: The (i+1)$(i+1)$th line contains W$W$ space-separated integers, representing the heights of the cells on the i$i$th row of the grid.
- On the (H+2)$(H+2)$th to (H+Q+1)$(H+Q+1)$th lines of each testcase: The (i+H+1)$(i+H+1)$th line contains 3 space-separated integers, r[i]$r[i]$, c[i]$c[i]$, and p[i]$p[i]$, which represents a (power, starting cell) combination. For this specific combination, the mercury leak originates on the cell (r[i],c[i])$(r[i],c[i])$ and has power p[i]$p[i]$.

-----Output:-----
For each testcase, output Q$Q$ lines, with each line containing one integer. The i$i$th line should output the number of dangerous cells, given that the leak originated on cell (r[i],c[i])$(r[i],c[i])$ with power p[i]$p[i]$, as defined in the input. Read the sample and sample explanation for more details.

-----Constraints-----
- 1≤T≤2$1 \leq T \leq 2$ 
- 1≤H≤1000$1 \leq H \leq 1000$
- 1≤W≤1000$1 \leq W \leq 1000$
- 1≤Q≤2∗105$1 \leq Q \leq 2*10^5$
- 1≤r[i]≤H$1 \leq r[i] \leq H$ for all i$i$
- 1≤c[i]≤W$1 \leq c[i] \leq W$ for all i$i$
- 0≤A[i][j]≤109$0 \leq A[i][j] \leq 10^9$ for all (i,j)$(i,j)$
- 0≤p[i]≤109$0 \leq p[i] \leq 10^9$ for all i$i$.

-----Subtasks-----
- 10 points : A[i][j]=$A[i][j] =$ constant k$k$ for all (i,j)$(i,j)$ (the heights of all cells are equal).   
- 20 points : H=1$H=1$, Q≤1000$Q \leq 1000$.   
- 30 points: r[i]=$r[i] =$ constant x$x$, c[i]=$c[i] =$ constant y$y$ for all i$i$ (the starting cell is fixed).   
- 40 points: No additional constraints.   

-----Sample Input:-----
1
5 5 3
4 3 9 7 2
8 6 5 2 8
1 7 3 4 3
2 2 4 5 6
9 9 9 9 9
3 4 6
3 2 5
1 4 9

-----Sample Output:-----
10
0
19

-----EXPLANATION:-----
For the first query, the cell (3,4)$(3,4)$ has height 4. The mercury can reach the following cells: (2,3)$(2,3)$, (2,4)$(2,4)$, (3,1)$(3,1)$, (3,3)$(3,3)$, (3,4)$(3,4)$, (3,5)$(3,5)$, (4,1)$(4,1)$, (4,2)$(4,2)$, (4,3)$(4,3)$, (4,4)$(4,4)$, for a total of 10. Note that it cannot reach cell (4,5)$(4,5)$ because the height (6) is equal to the power value (6).
For the second query, the cell (3,2)$(3,2)$ has height 7. Since the power value of 5 is less than or equal to the height of 7, the mercury immediately dissipates and so it reaches 0 cells.
For the third query, the mercury can reach all cells except the bottom row and the cell (1,3)$(1,3)$.
Note that (x,y)$(x,y)$ means the cell on the x$x$-th row and y$y$-th column.

Difficulty: UNKNOWN_DIFFICULTY

Starter code:

Solutions:
def solve(l, r, c, row, col, po):
	count = 0
	visited = set()
	stack = set()
	stack.add((l[row][col], row, col))
	while stack:
		ele = stack.pop()
		visited.add((ele[1], ele[2]))
		if ele[0] < po:
			count += 1
			if ele[1] - 1 >= 0 and (ele[1] - 1, ele[2]) not in visited:
				if l[ele[1] - 1][ele[2]] < po:
					stack.add((l[ele[1] - 1][ele[2]], ele[1] - 1, ele[2]))
			if ele[1] + 1 < r and (ele[1] + 1, ele[2]) not in visited:
				if l[ele[1] + 1][ele[2]] < po:
					stack.add((l[ele[1] + 1][ele[2]], ele[1] + 1, ele[2]))
			if ele[2] - 1 >= 0 and (ele[1], ele[2] - 1) not in visited:
				if l[ele[1]][ele[2] - 1] < po:
					stack.add((l[ele[1]][ele[2] - 1], ele[1], ele[2] - 1))
			if ele[2] + 1 < c and (ele[1], ele[2] + 1) not in visited:
				if l[ele[1]][ele[2] + 1] < po:
					stack.add((l[ele[1]][ele[2] + 1], ele[1], ele[2] + 1))
	return count
for _ in range(int(input())):
	(r, c, q) = map(int, input().split())
	l = []
	for i in range(r):
		a = list(map(int, input().split()))
		l.append(a)
	for i in range(q):
		(row, col, po) = map(int, input().split())
		print(solve(l, r, c, row - 1, col - 1, po))


def traverse(l, h, w, i, j, p):
	c = 0
	queue = [(i, j)]
	visited = [[False for i in range(w)] for i in range(h)]
	visited[i][j] = True
	while queue:
		newq = []
		c += len(queue)
		for i in range(len(queue)):
			d = queue[i]
			(x, y) = (d[0], d[1])
			if x + 1 < h and l[x + 1][y] < p and (not visited[x + 1][y]):
				newq.append((x + 1, y))
				visited[x + 1][y] = True
			if x - 1 >= 0 and l[x - 1][y] < p and (not visited[x - 1][y]):
				newq.append((x - 1, y))
				visited[x - 1][y] = True
			if y + 1 < w and l[x][y + 1] < p and (not visited[x][y + 1]):
				newq.append((x, y + 1))
				visited[x][y + 1] = True
			if y - 1 > -1 and l[x][y - 1] < p and (not visited[x][y - 1]):
				newq.append((x, y - 1))
				visited[x][y - 1] = True
		queue = newq
	return c
for i1 in range(int(input())):
	(h, w, q) = map(int, input().split())
	l = []
	for i in range(h):
		l1 = list(map(int, input().split()))
		l += [l1]
	for i in range(q):
		(i, j, p) = map(int, input().split())
		i -= 1
		j -= 1
		if l[i][j] >= p:
			print(0)
		else:
			b = traverse(l, h, w, i, j, p)
			print(b)


from collections import deque

def traverse(l, h, w, i, j, p):
	c = 0
	queue = deque([(i, j)])
	visited = [[False for i in range(w)] for i in range(h)]
	visited[i][j] = True
	while queue:
		d = queue.popleft()
		(x, y) = (d[0], d[1])
		c += 1
		if x + 1 < h and l[x + 1][y] < p and (not visited[x + 1][y]):
			queue.append((x + 1, y))
			visited[x + 1][y] = True
		if x - 1 >= 0 and l[x - 1][y] < p and (not visited[x - 1][y]):
			queue.append((x - 1, y))
			visited[x - 1][y] = True
		if y + 1 < w and l[x][y + 1] < p and (not visited[x][y + 1]):
			queue.append((x, y + 1))
			visited[x][y + 1] = True
		if y - 1 > -1 and l[x][y - 1] < p and (not visited[x][y - 1]):
			queue.append((x, y - 1))
			visited[x][y - 1] = True
	return c
for i1 in range(int(input())):
	(h, w, q) = map(int, input().split())
	l = []
	for i in range(h):
		l1 = list(map(int, input().split()))
		l += [l1]
	for i in range(q):
		(i, j, p) = map(int, input().split())
		i -= 1
		j -= 1
		if l[i][j] >= p:
			print(0)
		else:
			b = traverse(l, h, w, i, j, p)
			print(b)


import numpy as np

def solve(l, r, c, row, col, po):
	count = 0
	visited = set()
	stack = set()
	stack.add((l[row][col], row, col))
	while stack:
		ele = stack.pop()
		visited.add((ele[1], ele[2]))
		if ele[0] < po:
			count += 1
			if ele[1] - 1 >= 0 and (ele[1] - 1, ele[2]) not in visited:
				if l[ele[1] - 1][ele[2]] < po:
					stack.add((l[ele[1] - 1][ele[2]], ele[1] - 1, ele[2]))
			if ele[1] + 1 < r and (ele[1] + 1, ele[2]) not in visited:
				if l[ele[1] + 1][ele[2]] < po:
					stack.add((l[ele[1] + 1][ele[2]], ele[1] + 1, ele[2]))
			if ele[2] - 1 >= 0 and (ele[1], ele[2] - 1) not in visited:
				if l[ele[1]][ele[2] - 1] < po:
					stack.add((l[ele[1]][ele[2] - 1], ele[1], ele[2] - 1))
			if ele[2] + 1 < c and (ele[1], ele[2] + 1) not in visited:
				if l[ele[1]][ele[2] + 1] < po:
					stack.add((l[ele[1]][ele[2] + 1], ele[1], ele[2] + 1))
	return count
for _ in range(int(input())):
	(r, c, q) = map(int, input().split())
	l = []
	for i in range(r):
		a = list(map(int, input().split()))
		l.append(a)
	for i in range(q):
		(row, col, po) = map(int, input().split())
		if len(np.unique(l)) == 1:
			if int(np.unique(l)) < po:
				print(r * c)
			else:
				print('0')
			continue
		print(solve(l, r, c, row - 1, col - 1, po))