colour-queries

NOTE: All quotes are for clarity

You are given one n x m grid and q queries. Each cell of grid will have a value assigned to it. Each query will be of type "x y d". Read below on how you have to process them.

For each query, the grid is initially plain white. Now, for query "x y d" the cell (x, y) will be colored black. Your job, now, is to color each cell in the grid to black, such that at least one of its adjacent cell is colored black and the absolute difference of value between current cell and adjacent colored cell is ≤ d. (Adjacent cells are right, left, up and down to current cell). You have to keep coloring as long as you can find such cells. You have to tell after each query, how many cells are colored black.

INPUT

Each test file will have a single test case. The first line on input will have three integers n, m, and q.
Next n lines, each having m integers will be given. The j^th  integer on i^th  line will indicate the value of cell (i, j).
Then q lines of type "x y d" follow, each denoting a single query, as explained in problem statement.

OUTPUT

Output for each query a single integer on a separate line, denoting the number of colored cells for that particular query.

CONSTRAINTS

1 ≤ n,m ≤ 100

1 ≤ q ≤ 100

1 ≤ x ≤ n

1 ≤ y ≤ m

1 ≤ d ≤ 100000

-100000 ≤ The value of each cell of grid ≤ 100000

SAMPLE INPUT
4 4 3
0 0 1 0
1 2 1 0
0 0 1 0
1 0 1 0
4 4 0
4 3 1
2 2 2

SAMPLE OUTPUT
4
16
16

Difficulty: EASY

Starter code:

Solutions:
'''
4 4 3
0 0 1 0
1 2 1 0
0 0 1 0
1 0 1 0
4 4 0
4 3 1
2 2 2

ans
4
16
16

'''


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


def input_number_grid(n):
	return [input_number_line() for i in range(0, n)]


def calculate_blacks(x, y, d, n, m, g, v):
	if v[x][y] == 1:
		return 0
	else:
		v[x][y] = 1
	ra, la, ua, da = 0, 0, 0, 0
	if (x < (n-1)) and (abs(g[x+1][y] - g[x][y]) <= d):
		ra = calculate_blacks(x+1, y, d, n, m, g, v)
	if (x > 0) and (abs(g[x-1][y] - g[x][y]) <= d):
		la = calculate_blacks(x-1, y, d, n, m, g, v)

	if (y < (m-1)) and (abs(g[x][y+1] - g[x][y]) <= d):
		ua = calculate_blacks(x, y+1, d, n, m, g, v)
		
	if (y > 0) and (abs(g[x][y-1] - g[x][y]) <= d):
		da = calculate_blacks(x, y-1, d, n, m, g, v)
	return 1 + la + ra + ua + da


def main():
	n, m, q = input_number_line()
	# print n, m, q
	g = input_number_grid(n)
	# print g[2][3]
	for i in range(0, q):
		x, y, d = input_number_line()
		v = [[0 for i in range(0, m)] for i in range(0, n)]
		ans = calculate_blacks(x-1, y-1, d, n, m, g, v)
		print(ans)

main()


n, m, q = [int(x) for x in input().split()]
totl=0
grid=[]
visit=[]
for i in range(0,n):#rows
	grid.append([int(y) for y in input().split()])
def loop(x,y,dif,mx,my,count):
	cval=grid[x][y];
	count=1
	visit[x][y]+=1
	if(x>0 and visit[x-1][y]==0):
		val=grid[x-1][y]
		if(abs(cval-val)<=dif):
			count+=1
			loop(x-1,y,dif,mx,my,count)

	if(x<(mx-1) and visit[x+1][y]==0):
		val=grid[x+1][y]
		if(abs(cval-val)<=dif):
			count+=1
			loop(x+1,y,dif,mx,my,count)
	if(y>0 and visit[x][y-1]==0):
		val=grid[x][y-1]
		if(abs(cval-val)<=dif):
			count+=1
			loop(x,y-1,dif,mx,my,count)
	if(y<(my-1) and visit[x][y+1]==0):
		val=grid[x][y+1]
		if(abs(cval-val)<=dif):
			count+=1
			loop(x,y+1,dif,mx,my,count)
	else:
		pass

for y in range(0,q):
	for k in range(0,n):
		visit.insert(k,[0 for y in range(0,m)])
	x, y, d = [int(x) for x in input().split()]
	x, y = x-1, y-1
	totl=0
	loop(x,y,d,n,m,totl)
	for r in visit:
		for c in r:
			if(c==1):
				totl+=1
	print(totl)
	visit=[]


def dis(arr, x, y, x1, y1):
	return abs(arr[x][y] - arr[x1][y1])


total_count = 0
def fun(arr, x, y, d, aux, n, m,visited):
	global total_count
	aux[x][y]=1
	visited[x][y]=1
	if (x >= 1 and x < n and y >= 0 and y < m):
		if (dis(arr, x, y, x - 1, y) <= d):
			if aux[x - 1][y] == 0:
				total_count +=1
				#aux[x-1][y]=1
			if visited[x-1][y] == 0:
				fun(arr, x - 1, y, d, aux, n, m,visited)


	if (x >= 0 and x < n and y >= 1 and y < m):
		if (dis(arr, x, y, x, y - 1) <= d):
			if aux[x][y - 1] ==0:
				#aux[x][y - 1] = 1
				total_count +=1
			if visited[x][y-1] == 0:
				fun(arr, x, y - 1, d, aux, n, m,visited)

	if (x < n - 1 and x >= 0 and y >= 0 and y < m):
		if (dis(arr, x, y, x + 1, y) <= d):
			if aux[x + 1][y] ==0:
				#aux[x + 1][y] = 1
				total_count +=1
			if visited[x+1][y] == 0:
				fun(arr, x + 1, y, d, aux, n, m,visited)

	if (x >= 0 and x < n and y < m - 1 and y>=0):
		if (dis(arr, x, y, x, y + 1) <= d):
			if aux[x][y + 1] ==0:
				#aux[x][y + 1] = 1
				total_count +=1
			if visited[x][y+1] == 0:
				fun(arr, x, y + 1, d, aux, n, m,visited)
	return


def count(arr, n, m):
	i = 0
	cnt = 0
	while i < n:
		j = 0
		while j < m:
			if arr[i][j] == 1:
				cnt += 1
			j += 1
		i += 1
	return cnt


n, m, q = input().split()
n = int(n)
m = int(m)
q = int(q)
mat = []
N = n
M = m
Q = q
while n > 0:
	n -= 1
	row = input().split()
	row = list(map(int, row))
	mat.append(row)
while q > 0:
	q -= 1
	total_count = 0
	n = N
	m = M
	aux = [[0 for x in range(m)] for x in range(n)]
	visited = [[0 for x in range(m)] for x in range(n)]
	x, y, d = input().split()
	x = int(x)
	y = int(y)
	d = int(d)
	fun(mat, x-1, y-1, d, aux, n, m,visited)
	print(total_count+1)




n,m,q = list(map(int,input().split()))

a = [[0 for i in range(m)] for j in range(n) ]

r1 = a

def ff(a,r,x,y,d,t):
	xx=[]
	yy=[]
	
	if(isinstance(x,int)):
		xx.append(x)
		x=xx
		yy.append(y)
		y=yy
	x1 =[]
	y1 =[]
	for ind1 in range(len(x)):
		i=x[ind1]
		j=y[ind1]
		if(r[i][j]==1):
			if(i>0):
				if(abs(a[i-1][j] - a[i][j])<=d and r[i-1][j]==0):
					t+=1
					r[i-1][j]=1
					x1.append(i-1)
					y1.append(j)
			if(j>0):
				if(abs(a[i][j-1] - a[i][j])<=d and r[i][j-1]==0):
					t+=1
					r[i][j-1]=1
					x1.append(i)
					y1.append(j-1)
			if(i<n-1):		
				if(abs(a[i+1][j] - a[i][j])<=d and r[i+1][j]==0):
					t+=1
					r[i+1][j]=1
					x1.append(i+1)
					y1.append(j)
			if(j<m-1):
				if(abs(a[i][j+1] - a[i][j])<=d and r[i][j+1]==0):
					t+=1
					r[i][j+1]=1
					x1.append(i)
					y1.append(j+1)
	#print r,x1,y1,t
	
	if not x1:
		return r,x1,y1,t	
	else:
		return ff(a,r,x1,y1,d,t)
		
		
# func ends		

for i in range(n):
	a[i][:] = list(map(int,input().split())) 
	
for i in range(q):
	r=[[0 for i in range(m)] for j in range(n) ]
	x,y,d = list(map(int,input().split()))
	x=x-1
	y=y-1
	#print x,y,d
	r[x][y] = 1
	#ff(a,r,x,y,d,1)
	rr,xxx,yyy,tt = ff(a,r,x,y,d,1)
	print(tt)
	
	
	



import sys

n,m,q = list(map(int,sys.stdin.readline().split()))

grid = [list(map(int,sys.stdin.readline().split())) for i in range(n)]

def solve(x,y,d):
	total = 0
	if x-1>=0 and mat[x-1][y] == 0 and abs(grid[x][y]-grid[x-1][y])<=d:
		mat[x-1][y] = 1
		total += 1 + solve(x-1,y,d)
	
	if x+1<n and mat[x+1][y] == 0 and abs(grid[x][y]-grid[x+1][y])<=d:
		mat[x+1][y] = 1
		total += 1 + solve(x+1,y,d)

	if y-1>=0 and mat[x][y-1] == 0 and abs(grid[x][y]-grid[x][y-1])<=d:
		mat[x][y-1] = 1
		total += 1 + solve(x,y-1,d)

	if y+1<m and mat[x][y+1] == 0 and abs(grid[x][y]-grid[x][y+1])<=d:
		mat[x][y+1] = 1
		total += 1 + solve(x,y+1,d)
	return total
	

for i in range(q):
	x,y,d = list(map(int,sys.stdin.readline().split()))
	mat = [[0 for j in range(m)]    for i in range(n)]
	mat[x-1][y-1] = 1
	print(1 + solve(x-1,y-1,d))

	


def color(n, m, d, x, y, c, v, p, ans):
	 if x not in list(range(0, n)) or y not in list(range(0, m)):
	 	return
	 if c[x][y]==0 and abs(d[x][y] - p)<=v:
	 	c[x][y]=1
	 	ans[0]+=1
	 	color(n, m, d, x+1, y, c, v, d[x][y], ans)
	 	color(n, m, d, x-1, y, c, v, d[x][y], ans)
	 	color(n, m, d, x, y+1, c, v, d[x][y], ans)
	 	color(n, m, d, x, y-1, c, v, d[x][y], ans)
	 else:
	 	return
n, m, q=[int(e) for e in input().split()]
d=[]
for i in range(n):
	row=[int(e) for e in input().split()]
	d.append(row)
for i in range(q):
	x, y, v=[int(e) for e in input().split()]
	x, y=x-1, y-1
	ans=[0]
	c=[]
	for i in range(n):
	c.append([0]*m)
	color(n, m, d, x, y, c, v, d[x][y], ans)
	print(ans[0])


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
n, m, q=[int(e) for e in input().split()]
d=[]
for i in range(n):
	row=[int(e) for e in input().split()]
	d.append(row)
def color(n, m, d, x, y, c, v, p, ans):
	 if x not in list(range(0, n)) or y not in list(range(0, m)):
	 	return
	 if c[x][y]==0 and abs(d[x][y] - p)<=v:
	 	c[x][y]=1
	 	ans[0]+=1
	 	color(n, m, d, x+1, y, c, v, d[x][y], ans)
	 	color(n, m, d, x-1, y, c, v, d[x][y], ans)
	 	color(n, m, d, x, y+1, c, v, d[x][y], ans)
	 	color(n, m, d, x, y-1, c, v, d[x][y], ans)
	 else:
	 	return
for i in range(q):
	x, y, v=[int(e) for e in input().split()]
	x, y=x-1, y-1
	ans=[0]
	c=[]
	for i in range(n):
		c.append([0]*m)
	color(n, m, d, x, y, c, v, d[x][y], ans)
	print(ans[0])
		
	
		


# your code goes here
def mod(i):
	if(i<0):
		return -1*i
	else:
		return i

def func(grid,i,j,n,m,d):
	global visited
	sum = 0
	if(i>=n or i<0 or j>=m or j<0 or visited[i][j]):
		return 0
	else:
		visited[i][j] = True
		if(i+1<n and visited[i+1][j]==False and mod(grid[i][j]-grid[i+1][j])<=d):
			sum = sum + func(grid,i+1,j,n,m,d)
		if(j+1<m and visited[i][j+1]==False and mod(grid[i][j]-grid[i][j+1])<=d):
			sum = sum + func(grid,i,j+1,n,m,d)
		if(i-1>=0 and visited[i-1][j]==False  and mod(grid[i][j]-grid[i-1][j])<=d):
			sum = sum + func(grid,i-1,j,n,m,d)
		if(j-1>=0 and visited[i][j-1]==False  and mod(grid[i][j]-grid[i][j-1])<=d):
			sum = sum + func(grid,i,j-1,n,m,d)
		
	return sum+1
		
def cleanVisited():
	global visited
	visited = []
	for x in range(n):
		vrow = [False for y in range(m)]
		visited.append(vrow)

n,m,q = list(map(int, input().split()))
val = []
visited = []
queries = []
for x in range(n):
	row = list(map(int, input().split()))
	val.append(row)

	
for x in range(q):
	que = list(map(int, input().split()))
	queries.append(que)
	
for x in range(q):
	cleanVisited()
	print((func(val,queries[x][0]-1,queries[x][1]-1,n,m,queries[x][2])))


queue = []
N, M, Q = list(map(int, input().split()))
grid = []

def n_m_zero_array(N, M):
	visited = []
	for n in range(N):
		arr = [0]*M
		visited.append(arr)
	return visited

for n in range(N):
	arr = list(map(int, input().split()))
	grid.append(arr)
	
for q in range(Q):
	count = 0
	X, Y, D = list(map(int, input().split()))
	visited = n_m_zero_array(N,M)
	count_set = set()
	queue.append((X-1,Y-1))
	while(len(queue) != 0):
		
		# incrementing count of black
		
		
		ele = queue[0]
		x, y = ele
		visited[x][y] = 1
		del queue[0]

		# get adjacent elements
		if x-1 >= 0 and abs(grid[x-1][y]-grid[x][y]) <= D:
			if visited[x-1][y] != 1:
				queue.append((x-1,y))
				count += 1
				count_set.add((x-1,y))
		if x+1 < N and abs(grid[x+1][y]-grid[x][y]) <= D:
			if visited[x+1][y] != 1:
				queue.append((x+1,y))
				count += 1
				count_set.add((x+1,y))

		if y-1 >= 0 and abs(grid[x][y-1]-grid[x][y]) <= D:
			if visited[x][y-1] != 1:
				queue.append((x,y-1))
				count += 1
				count_set.add((x,y-1))
		if y+1 < M and abs(grid[x][y+1]-grid[x][y]) <= D:
			if visited[x][y+1] != 1:
				queue.append((x,y+1))
				count += 1
				count_set.add((x,y+1))
		
	print(len(count_set)+1)




from math import *
def back_track(x,y,d):
	xdir=[0,1,-1,0]
	ydir=[1,0,0,-1]
	if matrix[x][y]==1:
		return
	matrix[x][y]=1
	for i in range(4):
		xx=x+xdir[i]
		yy=y+ydir[i]
		if xx<0 or xx>=n or yy<0 or yy>=m:
			pass
		elif abs(l[xx][yy]-l[x][y])<=d and matrix[xx][yy]==0:
			back_track(xx,yy,d)

n,m,q=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(n)]

while q:
	result=0
	x,y,d=list(map(int,input().split()))
	matrix= [[0 for j in range(m)] for i in range(n)] 
	back_track(x-1,y-1,d)
	for i in range(n):
		for j in range(m):
			if matrix[i][j]==1:
				result+=1

	print(result)
	q-=1




'''
4 4 3
0 0 1 0
1 2 1 0
0 0 1 0
1 0 1 0
4 4 0
4 3 1
2 2 2

ans
4
16
16

'''


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


def input_number_grid(n):
	return [input_number_line() for i in range(0, n)]


def calculate_blacks(x, y, d, n, m, g, v):
	if v[x][y] == 1:
		return 0
	else:
		v[x][y] = 1
	ra, la, ua, da = 0, 0, 0, 0
	if (x < (n-1)) and (abs(g[x+1][y] - g[x][y]) <= d):
		ra = calculate_blacks(x+1, y, d, n, m, g, v)
	if (x > 0) and (abs(g[x-1][y] - g[x][y]) <= d):
		la = calculate_blacks(x-1, y, d, n, m, g, v)
	if (y < (m-1)) and (abs(g[x][y+1] - g[x][y]) <= d):
		ua = calculate_blacks(x, y+1, d, n, m, g, v)
	if (y > 0) and (abs(g[x][y-1] - g[x][y]) <= d):
		da = calculate_blacks(x, y-1, d, n, m, g, v)
	return 1 + la + ra + ua + da


def main():
	n, m, q = input_number_line()
	# print n, m, q
	g = input_number_grid(n)
	# print g[2][3]
	for i in range(0, q):
		x, y, d = input_number_line()
		# que = Queue()
		x = x - 1
		y = y - 1
		v = [[0 for i in range(0, m)] for i in range(0, n)]
		# print v
		ans = calculate_blacks(x, y, d, n, m, g, v)
		print(ans)

main()


def dfs(grid,cell,ver_i,ver_j,n,m,d):
	x =ver_i+1
	y=ver_j
	val = grid[ver_i][ver_j]
	if((x>=0 and x<n) and (y>=0 and y<m)):
		if(cell[x][y]==0 and abs(val-grid[x][y])<=d):
			cell[x][y]=1
			dfs(grid,cell,x,y,n,m,d)
	x =ver_i-1
	y=ver_j
	if((x>=0 and x<n) and (y>=0 and y<m)):
		if(cell[x][y]==0 and abs(val-grid[x][y])<=d):
			cell[x][y]=1
			dfs(grid,cell,x,y,n,m,d)
	x =ver_i
	y=ver_j-1
	if((x>=0 and x<n) and (y>=0 and y<m)):
		if(cell[x][y]==0 and abs(val-grid[x][y])<=d):
			cell[x][y]=1
			dfs(grid,cell,x,y,n,m,d)
	x =ver_i
	y=ver_j+1
	if((x>=0 and x<n) and (y>=0 and y<m)):
		if(cell[x][y]==0 and abs(val-grid[x][y])<=d):
			cell[x][y]=1
			dfs(grid,cell,x,y,n,m,d)
n,m,q = list(map(int,input().split()))
grid = []
for _  in range(n):
	temp = [int(i) for i in input().split()]
	grid += [temp]
for _ in range(q):
	x,y,d = list(map(int,input().split()))
	cell = []
	for i in range(n):
		cell += [[0]*m]
	cell[x-1][y-1]=1
	dfs(grid,cell,x-1,y-1,n,m,d)
	xy = 0
	for i in cell:
		xy += i.count(1)
	print(xy)
	


#!/usr/bin/python
class Hello:
	def __init__(self):
		ini = input()
		self.rows, self.cols, self.qs = tuple(ini.split())
		self.rows = int(self.rows)
		self.cols = int(self.cols)
		self.qs = int(self.qs)

	def iterate(self, x, y):
		_direct = {'u':(x-1,y), 'd':(x+1,y), 'l':(x,y-1), 'r':(x,y+1)}
		if x > 0 and self.flag[x-1].get(y) is None:
			_flag = self.mark(x, y, _direct['u'])
			if _flag:
				self.iterate(x-1, y)
		if x < (self.rows-1) and self.flag[x+1].get(y) is None:
			_flag = self.mark(x, y, _direct['d'])
			if _flag:
				self.iterate(x+1, y)
		if y > 0 and self.flag[x].get(y-1) is None:
			_flag = self.mark(x, y, _direct['l'])
			if _flag:
				self.iterate(x, y-1)
		if y < (self.cols-1) and self.flag[x].get(y+1) is None:
			_flag = self.mark(x, y, _direct['r'])
			if _flag:
				self.iterate(x, y+1)

	def mark(self, x, y, direct):
		tx, ty = direct
		if self.flag[tx].get(ty) is not None:
			return False
		if abs(self.grid[x][y] - self.grid[tx][ty]) <= self.d:
			self.flag[tx][ty] = True
			return True
		return False

	def run(self):
		self.grid = {}
		for row in list(range(self.rows)):
			self.grid[row] = {}
			inp = input()
			inp = inp.split()
			for col in list(range(self.cols)):
				self.grid[row][col] = int(inp[col])

		for q in list(range(self.qs)):
			_inp = input()
			_inp = _inp.split()
			row, col, self.d = tuple(_inp)
			row = int(row)
			col = int(col)
			if row>self.rows or col>self.cols:
				return 1
			self.d = int(self.d)
			self.flag = {}
			for i in list(range(self.rows)):
				self.flag[i] = {}
			self.flag[row-1][col-1] = True
			self.iterate(row-1, col-1)
			count = 0
			for i in list(range(self.rows)):
				count += list(self.flag[i].values()).count(True)
			print(count)
		return 0

obj = Hello()
ret = obj.run()


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
import sys
import numpy as np

line = input()

params = line.split(' ')

n = int(params[0])
m = int(params[1])
q = int(params[2])

a = np.zeros((n,m))

for i in range(n):
	new = input().split(' ')
	for j in range(m):
		a[i][j] = int(new[j])
		
for p in range(q):
	new = input().split(' ')
	i = int(new[0]) - 1
	j = int(new[1]) - 1
	d = int(new[2])
	count = 1
	black_index = [(i,j)]
	for new_index in black_index:
		i = new_index[0]
		j = new_index[1]
		if j+1 <= m-1 and (i,j+1) not in black_index:
			if abs(a[i][j] - a[i][j+1]) <= d:
				black_index.append((i,j+1))
				count = count + 1
		if j-1 >= 0 and (i,j-1) not in black_index:
			if abs(a[i][j] - a[i][j-1]) <= d:
				black_index.append((i,j-1))
				count = count + 1
		if i+1 <= n-1 and (i+1,j) not in black_index:
			if abs(a[i][j] - a[i+1][j]) <= d:
				black_index.append((i+1,j))
				count = count + 1
		if i-1 >= 0 and (i-1,j) not in black_index:
			if abs(a[i][j] - a[i-1][j]) <= d:
				black_index.append((i-1,j))
				count = count + 1
	print(count)


n, m, q=[int(e) for e in input().split()]
d=[]
for i in range(n):
	row=[int(e) for e in input().split()]
	d.append(row)
def color(n, m, d, x, y, c, v, p, ans):
	 if x not in list(range(0, n)) or y not in list(range(0, m)):
	 	return
	 if c[x][y]==0 and abs(d[x][y] - p)<=v:
	 	c[x][y]=1
	 	ans[0]+=1
	 	color(n, m, d, x+1, y, c, v, d[x][y], ans)
	 	color(n, m, d, x-1, y, c, v, d[x][y], ans)
	 	color(n, m, d, x, y+1, c, v, d[x][y], ans)
	 	color(n, m, d, x, y-1, c, v, d[x][y], ans)
	 else:
	 	return
for i in range(q):
	x, y, v=[int(e) for e in input().split()]
	x, y=x-1, y-1
	ans=[0]
	c=[]
	for i in range(n):
		c.append([0]*m)
	color(n, m, d, x, y, c, v, d[x][y], ans)
	print(ans[0])


nmq = input()
nmq = nmq.split()
nmq = list(map(int, nmq))
matrix = []
for i in range(nmq[0]):
	line = input()
	line = line.split()
	line = list(map(int, line))
	matrix.append(line)
for i in range(nmq[2]):
	colour = [['w']*nmq[1] for _ in range(nmq[0])]
	xyd = input()
	xyd = xyd.split()
	xyd = list(map(int, xyd))
	colour[xyd[0]-1][xyd[1]-1] = 'b'
	count = 1
	stack = [[xyd[0]-1, xyd[1]-1]]
	while len(stack) > 0:
		current = stack.pop()
		if current[0]-1 >= 0:
			top = [current[0]-1,current[1]]
			if (abs(matrix[top[0]][top[1]] - matrix[current[0]][current[1]]) <= xyd[2]) and colour[top[0]][top[1]] == 'w':
				stack.append(top)
				count += 1
				colour[top[0]][top[1]] = 'b'
		if current[1]+1 < int(nmq[1]):
			right = [current[0], current[1]+1]
			if (abs(matrix[right[0]][right[1]] - matrix[current[0]][current[1]]) <= xyd[2]) and colour[right[0]][right[1]] == 'w':
				stack.append(right)
				count += 1
				colour[right[0]][right[1]] = 'b'
		if current[0]+1 < int(nmq[0]):
			bottom = [current[0]+1, current[1]]
			if (abs(matrix[bottom[0]][bottom[1]] - matrix[current[0]][current[1]]) <= xyd[2]) and colour[bottom[0]][bottom[1]] == 'w':
				stack.append(bottom)
				count += 1
				colour[bottom[0]][bottom[1]] = 'b'
		if current[1]-1 >= 0:
			left = [current[0], current[1]-1]
			if (abs(matrix[left[0]][left[1]] - matrix[current[0]][current[1]]) <= xyd[2]) and colour[left[0]][left[1]] == 'w':
				stack.append(left)
				count += 1
				colour[left[0]][left[1]] = 'b'
	print(count)


nmq = input()
nmq_list = list(map(int,nmq.split()))
n = nmq_list[0]
m = nmq_list[1]
q = nmq_list[2]
matrix =[]


class cordinates():
	def __init__(self,x,y):
		self.x = x
		self.y = y    
cordinate_list = []
nomore =True	#binary flag to limit search space
control_pointer = 0
if not(1<=n<=100):
	n=0
if not(1<=m<=100):
	m=0
if not(1<=q<=100):
	q=0
for row in range(0,n):
	column = input()
	matrix.append(list(map(int, column.split())))



while q > 0:
	xyd = input()
	xyd_list = list(map(int, xyd.split()))
	x = xyd_list[0]	
	y = xyd_list[1]	
	d = xyd_list[2]

	if not(1<=x<=n) and not(1<=y<=m) and not(1<=d<=100000):
		nomore =False
 

#colouring
	cordinate_list.append(cordinates(x-1,y-1))
	
	while nomore:
		if control_pointer < len(cordinate_list):
			temp_cord = cordinate_list[control_pointer]
			
		else :
			nomore =False
			continue
		#up
		if temp_cord.x-1>=0 and not(any(temp.x == temp_cord.x-1 and temp.y == temp_cord.y for temp in cordinate_list)) and (abs(matrix[temp_cord.x][temp_cord.y] -
			 matrix[temp_cord.x-1][temp_cord.y])) <= d:
			cordinate_list.append(cordinates(temp_cord.x-1,temp_cord.y))
			#print "up"
		#down
		if temp_cord.x+1<n and not(any(temp.x == temp_cord.x+1 and temp.y == temp_cord.y for temp in cordinate_list)) and (abs(matrix[temp_cord.x][temp_cord.y] -
									 matrix[temp_cord.x+1][temp_cord.y])) <= d:
			cordinate_list.append(cordinates(temp_cord.x+1,temp_cord.y))
			#print "down"
		#right
		if temp_cord.y+1<m and not(any(temp.x == temp_cord.x and temp.y == temp_cord.y+1 for temp in cordinate_list)) and (abs(matrix[temp_cord.x][temp_cord.y] -
									 matrix[temp_cord.x][temp_cord.y+1])) <= d:
			cordinate_list.append(cordinates(temp_cord.x,temp_cord.y+1))
			#print "right"
		#left
		if temp_cord.y-1>=0 and not(any(temp.x == temp_cord.x and temp.y == temp_cord.y-1 for temp in cordinate_list))and (abs(matrix[temp_cord.x][temp_cord.y] -
									 matrix[temp_cord.x][temp_cord.y-1])) <= d:
			cordinate_list.append(cordinates(temp_cord.x,temp_cord.y-1))
			#print "left"
		control_pointer+=1
	print(len(cordinate_list))
	q-=1
	cordinate_list =[]
	nomore =1
	control_pointer = 0
	
	

	


'''
4 4 3
0 0 1 0
1 2 1 0
0 0 1 0
1 0 1 0
4 4 0
4 3 1
2 2 2

ans
4
16
16

'''


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


def input_number_grid(n):
	return [input_number_line() for i in range(0, n)]


def calculate_blacks(x, y, d, n, m, g, v):
	if v[x][y] == 1:
		return 0
	else:
		v[x][y] = 1
	ra, la, ua, da = 0, 0, 0, 0
	if (x < (n-1)) and (abs(g[x+1][y] - g[x][y]) <= d):
		ra = calculate_blacks(x+1, y, d, n, m, g, v)
	if (x > 0) and (abs(g[x-1][y] - g[x][y]) <= d):
		la = calculate_blacks(x-1, y, d, n, m, g, v)
	if (y < (m-1)) and (abs(g[x][y+1] - g[x][y]) <= d):
		ua = calculate_blacks(x, y+1, d, n, m, g, v)
	if (y > 0) and (abs(g[x][y-1] - g[x][y]) <= d):
		da = calculate_blacks(x, y-1, d, n, m, g, v)
	return 1 + la + ra + ua + da


def main():
	n, m, q = input_number_line()
	# print n, m, q
	g = input_number_grid(n)
	# print g[2][3]
	for i in range(0, q):
		x, y, d = input_number_line()
		# que = Queue()
		x = x - 1
		y = y - 1
		v = [[0 for i in range(0, m)] for i in range(0, n)]
		# print v
		ans = calculate_blacks(x, y, d, n, m, g, v)
		print(ans)

main()