import sys
import collections
def main():
    input=lambda: sys.stdin.readline().rstrip()
    n,m,t=map(int,input().split())
    T=[input() for _ in range(n)]
    C=[0]*(n*m)
    d=((1,0),(0,1),(-1,0),(0,-1))
    Q=collections.deque()
    for ij in range(n*m):
      i,j=ij//m,ij%m
      for dx,dy in d:
        if 0<=i+dx<n and 0<=j+dy<m and T[i][j]==T[i+dx][j+dy]:
          C[ij]=1
          Q.append(ij)
          break
      else:
        continue
    while Q:
      q=Q.popleft()
      i,j=q//m,q%m
      for dx,dy in d:
        if 0<=i+dx<n and 0<=j+dy<m and C[(i+dx)*m+j+dy]==0:
          C[(i+dx)*m+j+dy]=C[q]+1
          Q.append((i+dx)*m+j+dy)
    for _ in range(t):
      i,j,p=map(int,input().split())
      i-=1
      j-=1
      if C[i*m+j]>0 and max(0,p+1-C[i*m+j])%2==1:
        if T[i][j]=="0":
          print(1)
        else:
          print(0)
      else:
        if T[i][j]=="0":
          print(0)
        else:
          print(1)
main()


import sys;input=sys.stdin.readline
N, M, T = map(int, input().split())
X = [[0]*M for _ in range(N)]
S = []
for _ in range(N):
    s = input().strip()
    S.append(s)

f = False
stack = []
for i in range(N):
    for j in range(M):
        c = S[i][j]
        for a, b in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
            if a < 0 or a >= N:
                continue
            if b < 0 or b >= M:
                continue
            if S[a][b] == c:
                X[i][j] = 1
                stack.append((i, j))
                f = True
                break
ss = 1
while stack:
    tmp = []
    ss += 1
    while stack:
        i, j = stack.pop()
        for a, b in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
            if a < 0 or a >= N:
                continue
            if b < 0 or b >= M:
                continue
            if X[a][b] == 0:
                X[a][b] = ss
                tmp.append((a, b))
    stack = tmp

if not f:
    for _ in range(T):
        i, j, p = map(int, input().split())
        print(S[i-1][j-1])
else:
    for _ in range(T):
        i, j, p = map(int, input().split())
        i, j = i-1, j-1
#        print(i, j, X[i][j])
        f = max(0, p-X[i][j]+1)
#        print(f)
        if S[i][j] == "1":
            print(1-(f%2))
        else:
            print(f%2)


import sys
input=lambda: sys.stdin.readline().rstrip()
import collections
n,m,t=map(int,input().split())
T=[input() for _ in range(n)]
C=[0]*(n*m)
d=((1,0),(0,1),(-1,0),(0,-1))
Q=collections.deque()
for ij in range(n*m):
  i,j=ij//m,ij%m
  for dx,dy in d:
    if 0<=i+dx<n and 0<=j+dy<m and T[i][j]==T[i+dx][j+dy]:
      C[ij]=1
      Q.append(ij)
      break
  else:
    continue
while Q:
  q=Q.popleft()
  i,j=q//m,q%m
  for dx,dy in d:
    if 0<=i+dx<n and 0<=j+dy<m and C[(i+dx)*m+j+dy]==0:
      C[(i+dx)*m+j+dy]=C[q]+1
      Q.append((i+dx)*m+j+dy)
for _ in range(t):
  i,j,p=map(int,input().split())
  i-=1
  j-=1
  if max(0,p+1-C[i*m+j])%2==1:
    if T[i][j]=="0":
      print(1)
    else:
      print(0)
  else:
    if T[i][j]=="0":
      print(0)
    else:
      print(1)


import sys
input=lambda: sys.stdin.readline().rstrip()
import collections
n,m,t=map(int,input().split())
T=[input() for _ in range(n)]
C=[0]*(n*m)
d=((1,0),(0,1),(-1,0),(0,-1))
Q=collections.deque()
for ij in range(n*m):
  i,j=ij//m,ij%m
  for dx,dy in d:
    if 0<=i+dx<n and 0<=j+dy<m and T[i][j]==T[i+dx][j+dy]:
      C[ij]=1
      Q.append(ij)
      break
  else:
    continue
while Q:
  q=Q.popleft()
  i,j=q//m,q%m
  for dx,dy in d:
    if 0<=i+dx<n and 0<=j+dy<m and C[(i+dx)*m+j+dy]==0:
      C[(i+dx)*m+j+dy]=C[q]+1
      Q.append((i+dx)*m+j+dy)
for _ in range(t):
  i,j,p=map(int,input().split())
  i-=1
  j-=1
  if C[i*m+j]>0 and max(0,p+1-C[i*m+j])%2==1:
    if T[i][j]=="0":
      print(1)
    else:
      print(0)
  else:
    if T[i][j]=="0":
      print(0)
    else:
      print(1)


N, M, T = map(int, input().split())

G = [list(input()) for _ in range(N)]
FF = [[-1] * M for _ in range(N)]

mov = [(1, 0), (-1, 0), (0, 1), (0, -1)]
Q = []

missing = N * M

for y, l in enumerate(G):
	for x, c in enumerate(l):
		for dx, dy in mov:
			nx = x + dx
			ny = y + dy
			if nx < 0 or ny < 0 or nx >= M or ny >= N: continue
			if c == G[ny][nx]:
				FF[y][x] = 1
				Q.append((x, y))
				break

while Q:
	NQ = []
	for x, y in Q:
		mf = FF[y][x]
		for dx, dy in mov:
			nx = x + dx
			ny = y + dy
			if nx < 0 or ny < 0 or nx >= M or ny >= N: continue
			if FF[ny][nx] == -1:
				FF[ny][nx] = mf + 1
				NQ.append((nx, ny))

	Q = NQ

def opp(c):
	return '1' if c == '0' else '0'

for _ in range(T):
	i, j, p = map(int, input().split())
	i -= 1
	j -= 1
	if p < FF[i][j] or FF[i][j] == -1:
		print(G[i][j])
	else:
		print(G[i][j] if (p - FF[i][j]) % 2 == 1 else opp(G[i][j]))


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = sys.stdin.readline

import random

def main():
    n, m, t = map(int, input().split())
    # n, m, t = 1000, 1000, 0
    A = []
    T = []
    C = []
    INF = 2**64
    for _ in range(n):
        A.append(input().strip())
        # A.append(list(map(str, [random.randint(0,1) for _ in range(m)])))
        T.append([INF] * m)
        C.append([0] * m)

    stack = []
    for i in range(n):
        for j in range(m):
            for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
                if ii < 0 or jj < 0 or ii >= n or jj >= m:
                    continue
                if A[i][j] == A[ii][jj]:
                    T[i][j] = 0
                    stack.append( (i, j) )
    c = 0
    while stack:
        i, j = stack.pop(0)
        for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            if ii < 0 or jj < 0 or ii >= n or jj >= m:
                continue
            if T[ii][jj] != INF:
                continue
            T[ii][jj] = T[i][j] + 1
            stack.append( (ii, jj) )
    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        if p > T[i][j]:
            p -= T[i][j]
            print( (p + ord(A[i][j]) - zero)  % 2 )
        else:
            print(A[i][j])
    # for i in T:
    #     print(''.join(map(str, i)))
    # print()

    # for p in range(10):
    #     for i in range(n):
    #         tmp = []
    #         for j in range(m):
    #             if p > T[i][j]:
    #                 tmp.append( (p-T[i][j] + ord(A[i][j]) - zero)  % 2 )
    #             else:
    #                 tmp.append(A[i][j])
    #         print(''.join(map(str, tmp)))
    #     print()

main()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = sys.stdin.readline

import random

def main():
    n, m, t = map(int, input().split())
    # n, m, t = 1000, 1000, 0
    A = []
    T = []
    C = []
    INF = 2**64
    for _ in range(n):
        A.append(input().strip())
        # A.append(list(map(str, [random.randint(0,1) for _ in range(m)])))
        T.append([INF] * m)
        C.append([0] * m)

    stack = []
    for i in range(n):
        for j in range(m):
            # for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            #     if ii < 0 or jj < 0 or ii >= n or jj >= m:
            #         continue
            for ii, jj in [(i+1, j), (i, j+1)]:
                if ii >= n or jj >= m:
                    continue
                if A[i][j] == A[ii][jj]:
                    T[i][j] = 0
                    T[ii][jj] = 0
                    stack.append( (i, j) )
                    stack.append( (ii, jj) )
    c = 0
    while stack:
        i, j = stack.pop(0)
        for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            if ii < 0 or jj < 0 or ii >= n or jj >= m:
                continue
            if T[ii][jj] != INF:
                continue
            T[ii][jj] = T[i][j] + 1
            stack.append( (ii, jj) )
    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        if p > T[i][j]:
            p -= T[i][j]
            print( (p + ord(A[i][j]) - zero)  % 2 )
        else:
            print(A[i][j])
    # for i in T:
    #     print(''.join(map(str, i)))
    # print()

    # for p in range(10):
    #     for i in range(n):
    #         tmp = []
    #         for j in range(m):
    #             if p > T[i][j]:
    #                 tmp.append( (p-T[i][j] + ord(A[i][j]) - zero)  % 2 )
    #             else:
    #                 tmp.append(A[i][j])
    #         print(''.join(map(str, tmp)))
    #     print()

main()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = sys.stdin.readline

import random

def main():
    n, m, t = map(int, input().split())
    # n, m, t = 1000, 1000, 0
    A = []
    T = []
    C = []
    INF = 2**64
    for _ in range(n):
        A.append(list(map(int(list(input().strip())))))
        # A.append(list(map(str, [random.randint(0,1) for _ in range(m)])))
        T.append([INF] * m)

    stack = []
    for i in range(n):
        for j in range(m):
            # for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            #     if ii < 0 or jj < 0 or ii >= n or jj >= m:
            #         continue
            for ii, jj in [(i+1, j), (i, j+1)]:
                if ii >= n or jj >= m:
                    continue
                if A[i][j] == A[ii][jj]:
                    T[i][j] = 0
                    T[ii][jj] = 0
                    stack.append( (i, j) )
                    stack.append( (ii, jj) )
    c = 0
    while stack:
        i, j = stack.pop(0)
        for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            if ii < 0 or jj < 0 or ii >= n or jj >= m:
                continue
            if T[ii][jj] != INF:
                continue
            T[ii][jj] = T[i][j] + 1
            stack.append( (ii, jj) )
    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        if p > T[i][j]:
            p -= T[i][j]
            print( (p + ord(A[i][j]) - zero)  % 2 )
        else:
            print(A[i][j])
    # for i in T:
    #     print(''.join(map(str, i)))
    # print()

    # for p in range(10):
    #     for i in range(n):
    #         tmp = []
    #         for j in range(m):
    #             if p > T[i][j]:
    #                 tmp.append( (p-T[i][j] + ord(A[i][j]) - zero)  % 2 )
    #             else:
    #                 tmp.append(A[i][j])
    #         print(''.join(map(str, tmp)))
    #     print()

main()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = sys.stdin.readline

def main():
    n, m, t = map(int, input().split())
    A = []
    T = []
    C = []
    INF = 2**64
    for _ in range(n):
        A.append(input().strip())
        T.append([INF] * m)
        C.append([0] * m)

    stack = [(0, 0)]
    C[0][0] = 1
    while stack:
        i, j = stack[-1]
        if C[i][j] == 1:
            for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
                if ii < 0 or jj < 0 or ii >= n or jj >= m:
                    continue
                # print((i, j), '->', (ii, jj))
                if A[i][j] == A[ii][jj]:
                    T[i][j] = 0
                    T[ii][jj] = 0

                if C[ii][jj] == 0:
                    C[ii][jj] = 1
                    stack.append( (ii, jj) )
                else:
                    T[i][j] = min(T[i][j], T[ii][jj] + 1)
            C[i][j] = 2
        elif C[i][j] == 2:
            for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
                if ii < 0 or jj < 0 or ii >= n or jj >= m:
                    continue
                T[i][j] = min(T[i][j], T[ii][jj] + 1)
            stack.pop()
    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        if p > T[i][j]:
            p -= T[i][j]
            print( (p + ord(A[i][j]) - zero)  % 2 )
        else:
            print(A[i][j])
    # print(T)
main()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = sys.stdin.readline

import random

def main():
    n, m, t = map(int, input().split())
    # n, m, t = 1000, 1000, 0
    A = []
    T = []
    C = []
    INF = 2**64
    for _ in range(n):
        A.append(list(map(int, list(input().strip()))))
        # A.append(list(map(str, [random.randint(0,1) for _ in range(m)])))
        T.append([INF] * m)

    stack = []
    for i in range(n):
        for j in range(m):
            # for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            #     if ii < 0 or jj < 0 or ii >= n or jj >= m:
            #         continue
            for ii, jj in [(i+1, j), (i, j+1)]:
                if ii >= n or jj >= m:
                    continue
                if A[i][j] == A[ii][jj]:
                    T[i][j] = 0
                    T[ii][jj] = 0
                    stack.append( (i, j) )
                    stack.append( (ii, jj) )
    c = 0
    while stack:
        i, j = stack.pop(0)
        for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            if ii < 0 or jj < 0 or ii >= n or jj >= m:
                continue
            if T[ii][jj] != INF:
                continue
            T[ii][jj] = T[i][j] + 1
            stack.append( (ii, jj) )
    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        if p > T[i][j]:
            p -= T[i][j]
            print( (p + ord(A[i][j]) - zero)  % 2 )
        else:
            print(A[i][j])
    # for i in T:
    #     print(''.join(map(str, i)))
    # print()

    # for p in range(10):
    #     for i in range(n):
    #         tmp = []
    #         for j in range(m):
    #             if p > T[i][j]:
    #                 tmp.append( (p-T[i][j] + ord(A[i][j]) - zero)  % 2 )
    #             else:
    #                 tmp.append(A[i][j])
    #         print(''.join(map(str, tmp)))
    #     print()

main()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = sys.stdin.readline

import random

def main():
    n, m, t = map(int, input().split())
    # n, m, t = 1000, 1000, 0
    A = []
    T = []
    C = []
    INF = 2**64
    for _ in range(n):
        A.append(list(map(int, list(input().strip()))))
        # A.append(list(map(str, [random.randint(0,1) for _ in range(m)])))
        T.append([INF] * m)

    stack = []
    for i in range(n):
        for j in range(m):
            # for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            #     if ii < 0 or jj < 0 or ii >= n or jj >= m:
            #         continue
            for ii, jj in [(i+1, j), (i, j+1)]:
                if ii >= n or jj >= m:
                    continue
                if A[i][j] == A[ii][jj]:
                    T[i][j] = 0
                    T[ii][jj] = 0
                    stack.append( (i, j) )
                    stack.append( (ii, jj) )
    c = 0
    while stack:
        i, j = stack.pop(0)
        for ii, jj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            if ii < 0 or jj < 0 or ii >= n or jj >= m:
                continue
            if T[ii][jj] != INF:
                continue
            T[ii][jj] = T[i][j] + 1
            stack.append( (ii, jj) )
    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        if p > T[i][j]:
            p -= T[i][j]
            # print( (p + ord(A[i][j]) - zero)  % 2 )
            print( (p + A[i][j] - zero)  % 2 )
        else:
            print(A[i][j])
    # for i in T:
    #     print(''.join(map(str, i)))
    # print()

    # for p in range(10):
    #     for i in range(n):
    #         tmp = []
    #         for j in range(m):
    #             if p > T[i][j]:
    #                 tmp.append( (p-T[i][j] + ord(A[i][j]) - zero)  % 2 )
    #             else:
    #                 tmp.append(A[i][j])
    #         print(''.join(map(str, tmp)))
    #     print()

main()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
    f = iter(open('E.txt').readlines())
    def input():
        return next(f)
else:
    input = lambda: sys.stdin.readline().rstrip()

from collections import deque

def main():
    n, m, t = map(int, input().split())
    A = [input() for _ in range(n)]
    T = [0]*(n*m)

    d = ((1,0), (0,1), (-1, 0), (0, -1))
    stack = deque()
    for ij in range(n*m):
        i, j = ij // m, ij % m
        for dx, dy in d:
            if 0 <= i+dx < n and 0 <= j+dy < m and A[i][j] == A[i+dx][j+dy]:
                T[ij] = 1
                stack.append(ij)
    while stack:
        ij = stack.popleft()
        i, j = ij // m, ij % m
        for dx, dy in d:
            k = (i+dx)*m+j+dy
            if 0 <= i+dx < n and 0 <= j+dy < m and T[k] == 0:
                T[k] = T[ij] + 1
                stack.append(k)

    zero = ord('0')
    for _ in range(t):
        i, j, p = map(int, input().split())
        i -= 1
        j -= 1
        ij = i*m+j
        if T[ij] == 0:
            print(A[i][j])
        else:
            if p > T[ij]-1:
                p -= (T[ij]-1)
                print( (p + ord(A[i][j]) - zero)  % 2 )
            else:
                print(A[i][j])
    # for i in T:
    #     print(''.join(map(str, i)))
    # print()

    # for p in range(10):
    #     for i in range(n):
    #         tmp = []
    #         for j in range(m):
    #             if p > T[i][j]:
    #                 tmp.append( (p-T[i][j] + ord(A[i][j]) - zero)  % 2 )
    #             else:
    #                 tmp.append(A[i][j])
    #         print(''.join(map(str, tmp)))
    #     print()

main()


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        if isinstance(arg[0], collections.abc.Iterable):
            self.a = [list(row) for row in arg[0]]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        else:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

sys.exit(0)

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


import sys
input = iter(sys.stdin.readlines()).__next__  # pylint: disable= redefined-builtin
import collections
import copy

n,m,t = map(int, input().split())
grid = [[int(c) for c in input().strip()] for _ in range(n)]

class Query:
    def __init__(self, index):
        self.index = index
        i,j,p = map(int, input().split())
        self.i = i-1
        self.j = j-1
        self.p = p
queries = [Query(index) for index in range(t)]

queries_by_p = collections.defaultdict(list)
for q in queries:
    queries_by_p[q.p].append(q)

answers = [None]*t

def is_same(i, j, x):
    if not (0 <= i < n and 0 <= j < m): return False
    return grid[i][j] == x

def any_neighbors(i, j):
    for dy, dx in ((1,0), (-1,0), (0,1), (0,-1)):
        if is_same(i+dy, j+dx, grid[i][j]): return True
    return False

ult_grid = None
penult_grid = None
for time in range(max(q.p for q in queries)+1):
    if time in queries_by_p:
        for q in queries_by_p[time]:
            answers[q.index] = grid[q.i][q.j]

    next_grid = copy.deepcopy(grid)
    for i in range(n):
        for j in range(m):
            if any_neighbors(i, j):
                next_grid[i][j] ^= 1

    penult_grid = ult_grid
    ult_grid = grid
    grid = next_grid

    if grid == penult_grid:
        time += 1
        break

for q in queries:
    if answers[q.index] is None:
        if (q.p - time)&1:
            a = ult_grid[q.i][q.j]
        else:
            a = grid[q.i][q.j]

        answers[q.index] = a

print(*answers, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        try:
            iterable = iter(arg[0])
            self.a = [list(row) for row in iterable]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]

        except TypeError:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        still_life[u] = 0
        queue.append(u)

while queue:
    u = queue.popleft()
    if u in still_life: continue

    for v in grid.neighbors(u):
        if v not in still_life:
            still_life[v] = still_life[u]+1
            queue.append(v)

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        if isinstance(arg[0], collections.abc.Iterable):
            self.a = [list(row) for row in arg[0]]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        else:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

sys.exit(0)

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        try:
            iterable = iter(arg[0])
            self.a = [list(row) for row in iterable]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]

        except TypeError:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        still_life[u] = 0
        queue.append(u)

while queue:
    u = queue.popleft()

    for v in grid.neighbors(u):
        if v not in still_life:
            still_life[v] = still_life[u]+1
            queue.append(v)

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        if isinstance(arg[0], collections.abc.Iterable):
            self.a = [list(row) for row in arg[0]]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        else:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
sys.exit()
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        if isinstance(arg[0], collections.abc.Iterable):
            self.a = [list(row) for row in arg[0]]
            self.dim = len(self.a), len(self.a[0])
            sys.exit()
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        else:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        try:
            iterable = iter(arg[0])
            self.a = [list(row) for row in iterable]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        except TypeError:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        if isinstance(arg[0], collections.abc.Iterable):
            self.a = [list(row) for row in arg[0]]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        else:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

sys.exit()

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


#region bcf
# code section by Barry Fam
#
# pylint: disable= reimported
#region mapping2d.py
import collections

class Mapping2D:
    """
    A convenience class for accessing a 2d array with map-like methods

    Mapping2D(n: int, m: int) -> new empty mapping of size n x m
    Mapping2D(iterable of iterables) -> initialized

    >>> x = Mapping2D([[9, 8, 7], [6, 5, 4]])
    >>> x[1,1]
    5
    >>> (0,1) in x
    True

    >>> del x[1,0]
    >>> (1,0) in x
    False
    >>> x[1,0]
    Traceback (most recent call last):
        ...
    KeyError: (1, 0)
    >>> x.get((1,0), 'empty')
    'empty'

    >>> x[0,0] = 42
    >>> list(x.items())
    [((0, 0), 42), ((0, 1), 8), ((0, 2), 7), ((1, 1), 5), ((1, 2), 4)]

    Also has methods for treating the array as "walkable" with adjacent cells connected:

    >>> x.inbounds((99,99))
    False

    >>> list(x.neighbors((1,1)))  # note empty cells are included
    [(0, 1), (1, 0), (1, 2)]
    """

    def __init__(self, *arg):
        self.none = object()

        if isinstance(arg[0], collections.abc.Iterable):
            self.a = [list(row) for row in arg[0]]
            self.dim = len(self.a), len(self.a[0])
            assert all(len(row) == self.dim[1] for row in self.a)
            self.len = self.dim[0] * self.dim[1]
        else:
            self.dim = arg
            self.a = [[self.none]*self.dim[1] for _ in range(self.dim[0])]
            self.len = 0

    def __repr__(self):
        return "Mapping2D({})".format(repr(self.a))

    def __len__(self):
        return self.len

    def inbounds(self, u):
        return 0 <= u[0] < self.dim[0] and 0 <= u[1] < self.dim[1]

    def __contains__(self, u):
        return self.inbounds(u) and self.a[u[0]][u[1]] is not self.none

    def __getitem__(self, u):
        if u not in self: raise KeyError(u)
        return self.a[u[0]][u[1]]

    def __setitem__(self, u, x):
        if not self.inbounds(u): raise KeyError(u)
        if u not in self: self.len += 1
        self.a[u[0]][u[1]] = x

    def __delitem__(self, u):
        if not self.inbounds(u): raise KeyError(u)
        if u in self:
            self.len -= 1
            self.a[u[0]][u[1]] = self.none

    def get(self, u, default=None):
        if u in self: return self[u]
        else: return default

    def update(self, iterable):
        for u, x in iterable:
            self[u] = x

    def clear(self):
        self.__init__(self.dim[0], self.dim[1])

    def items(self):
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                x = self.a[i][j]
                if x is self.none: continue
                yield (i, j), x

    def keys(self):
        return (k for k, _ in self.items())

    def values(self):
        return (v for _, v in self.items())

    def __iter__(self):
        return self.keys()

    def __eq__(self, other):
        return isinstance(other, Mapping2D) and self.a == other.a

    @staticmethod
    def _dmoves(u):
        D_PAD = ((-1,0), (0,-1), (0,1), (1,0))
        return ((u[0]+d[0], u[1]+d[1]) for d in D_PAD)

    def neighbors(self, u):
        return (v for v in self._dmoves(u) if self.inbounds(v))

# if __name__ == "__main__":
if False:  # pylint: disable= using-constant-test
    import doctest
    doctest.testmod()

#endregion mapping2d.py
#endregion bcf
###############################################################################

import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import itertools
import math

n,m,t = map(int, input().split())
grid = Mapping2D((int(c) for c in input()) for _ in range(n))

def active(u):
    return any(grid[v] == grid[u] for v in grid.neighbors(u))

still_life = Mapping2D(n, m)
queue = collections.deque()
for u in itertools.product(range(n), range(m)):
    if active(u):
        queue.append((0, u))

while queue:
    time, u = queue.popleft()
    if u in still_life: continue
    still_life[u] = time

    for v in grid.neighbors(u):
        if v not in still_life:
            queue.append((time+1, v))

def status(u, p):
    s = still_life.get(u, math.inf)
    if p <= s: return grid[u]
    else: return grid[u] ^ (p-s)&1

output = []
for _ in range(t):
    i,j,p = map(int, input().split())
    output.append(status((i-1, j-1), p))

print(*output, sep='\n')


from collections import deque
import sys

input=sys.stdin.readline

n,m,t=map(int,input().split())
a=[]
for i in range(n):
    a.append(input())

move=[(0,1),(0,-1),(1,0),(-1,0)]
connect=deque([])
dist=[0]*(n*m)
used=[False]*(n*m)
for i in range(n*m):
    h=i//m
    w=i%m
    for x,y in move:
        nh=h+x;nw=w+y
        if n>nh>-1 and m>nw>-1 and a[h][w]==a[nh][nw]:
            connect.append((i,0))
            used[i]=True
            break

flag=(not connect)
while connect:
    v,d=connect.popleft()
    if v//m!=0:
        if not used[v-m]:
            used[v-m]=True
            dist[v-m]=d+1
            connect.append((v-m,d+1))
    if v//m!=n-1:
        if not used[v+m]:
            used[v+m]=True
            dist[v+m]=d+1
            connect.append((v+m,d+1))
    if v%m!=0:
        if not used[v-1]:
            used[v-1]=True
            dist[v-1]=d+1
            connect.append((v-1,d+1))
    if v%m!=m-1:
        if not used[v+1]:
            used[v+1]=True
            dist[v+1]=d+1
            connect.append((v+1,d+1))

for i in range(t):
    i,j,p=map(int,input().split())
    v=m*(i-1)+j-1
    if not flag:
        if dist[v]>=p:
            print(a[i-1][j-1])
        else:
            print((int(a[i-1][j-1])+dist[v]-p)%2)
    else:
        print(a[i-1][j-1])


printn = lambda x: print(x,end='')
import sys
input = sys.stdin.readline
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7
from collections import deque

def ddprint(x):
  if DBG:
    print(x)

class Unionfind:

  def __init__(s,n):
    s.sz = [1] * n
    s.ances = [i for i in range(n)]

  def root(s,x):
    a = []
    y = x
    while s.ances[y] != y:
      a.append(y)
      y = s.ances[y]
    for z in a:
      s.ances[z] = y
    return y

  def conn(s,x,y):
    i = s.root(x)
    j = s.root(y)
    if i==j:
      return
    #k  = [i,j].min
    k  = j if (s.sz[i]<s.sz[j]) else i
    if k==j:
      s.ances[i] = j
    else:
      s.ances[j] = i
    s.sz[k] = s.sz[i] + s.sz[j]

n,m,t = inm()
a = []
for i in range(n):
    a.append(list(map(int,list(ins()))))
pijt = []
for tt in range(t):
    i,j,p = inm()
    pijt.append((p,i-1,j-1,tt))

uf = Unionfind(n*m)
for i in range(n):
    for j in range(m):
        if j<m-1 and a[i][j]==a[i][j+1]:
            uf.conn(i*m+j,i*m+j+1)
        if i<n-1 and a[i][j]==a[i+1][j]:
            uf.conn(i*m+j,(i+1)*m+j)
h = {}
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in h:
            h[r] = 0
        h[r] += 1
singles = {}
nosingle = False
for r in h:
    if h[r]==1:
        singles[r] = 1
    else:
        nosingle = True

if not nosingle:
    for tt in range(t):
        p,i,j,ttt = pijt[tt]
        print(a[i][j])
    exit()

future = [[None]*m for i in range(n)]
q = deque([])
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in singles:
            future[i][j] = (0,r)
            q.append((i,j))
while len(q)>0:
    i,j = q.popleft()
    d,r = future[i][j]
    for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
        x = i+dir[0]
        y = j+dir[1]
        if 0<=x<n and 0<=y<m and future[x][y] is None:
            future[x][y] = (d+1,r)
            q.append((x,y))
            if False:
              for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
                xx = x+dir[0]
                yy = y+dir[1]
                if 0<=xx<n and 0<=yy<m and future[xx][yy] is None and a[x][y] != a[xx][yy]:
                    future[xx][yy] = (d+2,r)
                    q.append((xx,yy))

if DBG:
    for i in range(n):
        print(future[i])

for tt in range(t):
    p,i,j,ttt = pijt[tt]
    d,r = future[i][j]
    if p<d:
        print(a[i][j])
    else:
        ri = r//m
        rj = r%m
        print(a[ri][rj] if p%2==0 else 1-a[ri][rj])


printn = lambda x: print(x,end='')
import sys
input = sys.stdin.readline
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7
from collections import deque

def ddprint(x):
  if DBG:
    print(x)

class Unionfind:

  def __init__(s,n):
    s.sz = [1] * n
    s.ances = [i for i in range(n)]

  def root(s,x):
    a = []
    y = x
    while s.ances[y] != y:
      a.append(y)
      y = s.ances[y]
    for z in a:
      s.ances[z] = y
    return y

  def conn(s,x,y):
    i = s.root(x)
    j = s.root(y)
    if i==j:
      return
    #k  = [i,j].min
    k  = j if (s.sz[i]<s.sz[j]) else i
    if k==j:
      s.ances[i] = j
    else:
      s.ances[j] = i
    s.sz[k] = s.sz[i] + s.sz[j]

n,m,t = inm()
a = []
for i in range(n):
    a.append(ins())
    #a.append(list(map(int,list(ins()))))
pijt = []
for tt in range(t):
    i,j,p = inm()
    pijt.append((p,i-1,j-1,tt))

uf = Unionfind(n*m)
for i in range(n):
    for j in range(m):
        if j<m-1 and a[i][j]==a[i][j+1]:
            uf.conn(i*m+j,i*m+j+1)
        if i<n-1 and a[i][j]==a[i+1][j]:
            uf.conn(i*m+j,(i+1)*m+j)
h = {}
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in h:
            h[r] = 0
        h[r] += 1
singles = {}
nosingle = False
for r in h:
    if h[r]==1:
        singles[r] = 1
    else:
        nosingle = True

if not nosingle:
    for tt in range(t):
        p,i,j,ttt = pijt[tt]
        print(a[i][j])
    exit()

future = [[None]*m for i in range(n)]
q = deque([])
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in singles:
            future[i][j] = (0,r)
            q.append((i,j))
while len(q)>0:
    i,j = q.popleft()
    d,r = future[i][j]
    for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
        x = i+dir[0]
        y = j+dir[1]
        if 0<=x<n and 0<=y<m and future[x][y] is None:
            future[x][y] = (d+1,r)
            q.append((x,y))
            if False:
              for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
                xx = x+dir[0]
                yy = y+dir[1]
                if 0<=xx<n and 0<=yy<m and future[xx][yy] is None and a[x][y] != a[xx][yy]:
                    future[xx][yy] = (d+2,r)
                    q.append((xx,yy))

if DBG:
    for i in range(n):
        print(future[i])

for tt in range(t):
    p,i,j,ttt = pijt[tt]
    d,r = future[i][j]
    if p<d:
        print(a[i][j])
    else:
        ri = r//m
        rj = r%m
        print(a[ri][rj] if p%2==0 else 1-int(a[ri][rj]))


printn = lambda x: print(x,end='')
import sys
input = sys.stdin.readline
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7
from collections import deque

def ddprint(x):
  if DBG:
    print(x)

class Unionfind:

  def __init__(s,n):
    s.sz = [1] * n
    s.ances = [i for i in range(n)]

  def root(s,x):
    a = []
    y = x
    while s.ances[y] != y:
      a.append(y)
      y = s.ances[y]
    for z in a:
      s.ances[z] = y
    return y

  def conn(s,x,y):
    i = s.root(x)
    j = s.root(y)
    if i==j:
      return
    #k  = [i,j].min
    k  = j if (s.sz[i]<s.sz[j]) else i
    if k==j:
      s.ances[i] = j
    else:
      s.ances[j] = i
    s.sz[k] = s.sz[i] + s.sz[j]

n,m,t = inm()
a = []
for i in range(n):
    a.append(ins())
    #a.append(list(map(int,list(ins()))))
#pijt = []
#for tt in range(t):
#    i,j,p = inm()
#    pijt.append((p,i-1,j-1,tt))

uf = Unionfind(n*m)
for i in range(n):
    for j in range(m):
        if j<m-1 and a[i][j]==a[i][j+1]:
            uf.conn(i*m+j,i*m+j+1)
        if i<n-1 and a[i][j]==a[i+1][j]:
            uf.conn(i*m+j,(i+1)*m+j)
h = {}
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in h:
            h[r] = 0
        h[r] += 1
singles = {}
nosingle = False
for r in h:
    if h[r]==1:
        singles[r] = 1
    else:
        nosingle = True

if not nosingle:
    for tt in range(t):
        #p,i,j,ttt = pijt[tt]
        i,j,p = inm()
        print(a[i-1][j-1])
    exit()

future = [[None]*m for i in range(n)]
q = deque([])
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in singles:
            future[i][j] = (0,r)
            q.append((i,j))
while len(q)>0:
    i,j = q.popleft()
    d,r = future[i][j]
    for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
        x = i+dir[0]
        y = j+dir[1]
        if 0<=x<n and 0<=y<m and future[x][y] is None:
            future[x][y] = (d+1,r)
            q.append((x,y))
            if False:
              for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
                xx = x+dir[0]
                yy = y+dir[1]
                if 0<=xx<n and 0<=yy<m and future[xx][yy] is None and a[x][y] != a[xx][yy]:
                    future[xx][yy] = (d+2,r)
                    q.append((xx,yy))

if DBG:
    for i in range(n):
        print(future[i])

for tt in range(t):
    #p,i,j,ttt = pijt[tt]
    i,j,p = inm()
    i -= 1
    j -= 1
    d,r = future[i][j]
    if p<d:
        print(a[i][j])
    else:
        ri = r//m
        rj = r%m
        print(a[ri][rj] if p%2==0 else 1-int(a[ri][rj]))


printn = lambda x: print(x,end='')
import sys
input = sys.stdin.readline
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7
from collections import deque

def ddprint(x):
  if DBG:
    print(x)

class Unionfind:

  def __init__(s,n):
    s.sz = [1] * n
    s.ances = [i for i in range(n)]

  def root(s,x):
    a = []
    y = x
    while s.ances[y] != y:
      a.append(y)
      y = s.ances[y]
    for z in a:
      s.ances[z] = y
    return y

  def conn(s,x,y):
    i = s.root(x)
    j = s.root(y)
    if i==j:
      return
    #k  = [i,j].min
    k  = j if (s.sz[i]<s.sz[j]) else i
    if k==j:
      s.ances[i] = j
    else:
      s.ances[j] = i
    s.sz[k] = s.sz[i] + s.sz[j]

n,m,t = inm()
a = []
for i in range(n):
    a.append(ins())
    #a.append(list(map(int,list(ins()))))
#pijt = []
#for tt in range(t):
#    i,j,p = inm()
#    pijt.append((p,i-1,j-1,tt))

uf = Unionfind(n*m)
for i in range(n):
    for j in range(m):
        if j<m-1 and a[i][j]==a[i][j+1]:
            uf.conn(i*m+j,i*m+j+1)
        if i<n-1 and a[i][j]==a[i+1][j]:
            uf.conn(i*m+j,(i+1)*m+j)
h = [0]*(n*m)
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        h[r] += 1
nsingles = h.count(1)
if nsingles==n*m:
    for tt in range(t):
        #p,i,j,ttt = pijt[tt]
        i,j,p = inm()
        print(a[i-1][j-1])
    exit()

future = [[None]*m for i in range(n)]
q = deque([])
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if h[r]>1:
            future[i][j] = (0,r)
            q.append((i,j))

while len(q)>0:
    i,j = q.popleft()
    d,r = future[i][j]
    for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
        x = i+dir[0]
        y = j+dir[1]
        if 0<=x<n and 0<=y<m and future[x][y] is None:
            future[x][y] = (d+1,r)
            q.append((x,y))
            if False:
              for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
                xx = x+dir[0]
                yy = y+dir[1]
                if 0<=xx<n and 0<=yy<m and future[xx][yy] is None and a[x][y] != a[xx][yy]:
                    future[xx][yy] = (d+2,r)
                    q.append((xx,yy))

if DBG:
    for i in range(n):
        print(future[i])

for tt in range(t):
    #p,i,j,ttt = pijt[tt]
    i,j,p = inm()
    i -= 1
    j -= 1
    d,r = future[i][j]
    if p<d:
        print(a[i][j])
    else:
        ri = r//m
        rj = r%m
        print(a[ri][rj] if p%2==0 else 1-int(a[ri][rj]))


printn = lambda x: print(x,end='')
import sys
input = sys.stdin.readline
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7
from collections import deque

def ddprint(x):
  if DBG:
    print(x)

class Unionfind:

  def __init__(s,n):
    s.sz = [1] * n
    s.ances = [i for i in range(n)]

  def root(s,x):
    a = []
    y = x
    while s.ances[y] != y:
      a.append(y)
      y = s.ances[y]
    for z in a:
      s.ances[z] = y
    return y

  def conn(s,x,y):
    i = s.root(x)
    j = s.root(y)
    if i==j:
      return
    #k  = [i,j].min
    k  = j if (s.sz[i]<s.sz[j]) else i
    if k==j:
      s.ances[i] = j
    else:
      s.ances[j] = i
    s.sz[k] = s.sz[i] + s.sz[j]

n,m,t = inm()
a = []
for i in range(n):
    a.append(list(map(int,list(ins()))))
pijt = []
for tt in range(t):
    i,j,p = inm()
    pijt.append((p,i-1,j-1,tt))

uf = Unionfind(n*m)
for i in range(n):
    for j in range(m):
        if j<m-1 and a[i][j]==a[i][j+1]:
            uf.conn(i*m+j,i*m+j+1)
        if i<n-1 and a[i][j]==a[i+1][j]:
            uf.conn(i*m+j,(i+1)*m+j)
h = {}
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in h:
            h[r] = 0
        h[r] += 1
singles = {}
nosingle = False
for r in h:
    if h[r]==1:
        singles[r] = 1
    else:
        nosingle = True

if not nosingle:
    for tt in range(t):
        p,i,j,ttt = pijt[tt]
        print(a[i][j])
    exit()

future = [[None]*m for i in range(n)]
done = [[False]*m for i in range(n)]
q = deque([])
for i in range(n):
    for j in range(m):
        r = uf.root(i*m+j)
        if r not in singles:
            future[i][j] = (0,r)
            q.append((i,j))
while len(q)>0:
    i,j = q.popleft()
    d,r = future[i][j]
    for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
        x = i+dir[0]
        y = j+dir[1]
        if 0<=x<n and 0<=y<m and future[x][y] is None:
            future[x][y] = (d+1,r)
            q.append((x,y))
            if False:
              for dir in [(0,1),(0,-1),(1,0),(-1,0)]:
                xx = x+dir[0]
                yy = y+dir[1]
                if 0<=xx<n and 0<=yy<m and future[xx][yy] is None and a[x][y] != a[xx][yy]:
                    future[xx][yy] = (d+2,r)
                    q.append((xx,yy))

if DBG:
    for i in range(n):
        print(future[i])

for tt in range(t):
    p,i,j,ttt = pijt[tt]
    d,r = future[i][j]
    if p<d:
        print(a[i][j])
    else:
        ri = r//m
        rj = r%m
        print(a[ri][rj] if p%2==0 else 1-a[ri][rj])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
# vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	# vis.append([0]*m)
from collections import deque
q1=deque()
q2=deque()

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j

if(x!=-1):
	# vis[x][y]=1
	while(len(q1)!=0):
		x=q1.popleft()
		y=q2.popleft()
		# vis[x][y]=1
		if(x>0 and dp[x-1][y]==inf):
			dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
			q1.append(x-1)
			q2.append(y)
			# vis[x-1][y]=1
		if(x<n-1 and dp[x+1][y]==inf):
			dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
			q1.append(x+1)
			q2.append(y)
			# vis[x+1][y]=1
		if(y>0 and dp[x][y-1]==inf):
			dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
			q1.append(x)
			q2.append(y-1)
			# vis[x][y-1]=1
		if(y<m-1 and dp[x][y+1]==inf):
			dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
			q1.append(x)
			q2.append(y+1)
			# vis[x][y+1]=1

	for i in range(t):
		a,b,c=map(int,input().split())
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print('0')
				else:
					print('1')
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
		# print("\n")
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])
		# print("\n")


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
# input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j

from queue import Queue
if(x!=-1):
	q1=Queue(maxsize=10000000)
	q2=Queue(maxsize=10000000)
	q1.put(x)
	q2.put(y)
	# print(x,y)
	vis[x][y]=1
	while(q1.qsize()!=0):
		x=q1.get()
		y=q2.get()
		# vis[x][y]=1
		if(x>0):
			if(dp[x-1][y]>dp[x][y]+1 or vis[x-1][y]==0):
				dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
				q1.put(x-1)
				q2.put(y)
				vis[x-1][y]=1
		if(x<n-1):
			if(dp[x+1][y]>1+dp[x][y] or vis[x+1][y]==0):
				dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
				q1.put(x+1)
				q2.put(y)
				vis[x+1][y]=1
		if(y>0):
			if(dp[x][y-1]>1+dp[x][y]+1 or vis[x][y-1]==0):
				dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
				q1.put(x)
				q2.put(y-1)
				vis[x][y-1]=1
		if(y<m-1):
			if(dp[x][y+1]>dp[x][y]+1 or vis[x][y+1]==0):
				dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
				q1.put(x)
				q2.put(y+1)
				vis[x][y+1]=1
	# for i in range(n):
		# print(*dp[i])
	for i in range(t):
		a,b,c=map(int,input().split())
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print(0)
				else:
					print(1)
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
# input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j

from queue import Queue
if(x!=-1):
	q1=Queue(maxsize=10000)
	q2=Queue(maxsize=10000)
	q1.put(x)
	q2.put(y)
	# print(x,y)
	while(q1.qsize()!=0):
		x=q1.get()
		y=q2.get()
		vis[x][y]=1
		if(x>0 and vis[x-1][y]==0):
			dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
			q1.put(x-1)
			q2.put(y)
		if(x<n-1 and vis[x+1][y]==0):
			dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
			q1.put(x+1)
			q2.put(y)
		if(y>0 and vis[x][y-1]==0):
			dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
			q1.put(x)
			q2.put(y-1)
		if(y<m-1 and vis[x][y+1]==0):
			dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
			q1.put(x)
			q2.put(y+1)
	# print(dp)
	for i in range(t):
		a,b,c=map(int,input().split())
		if(i==62):
			print(a)
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print(0)
				else:
					print(1)
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
# input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j

from queue import Queue
if(x!=-1):
	q1=Queue(maxsize=10000000)
	q2=Queue(maxsize=10000000)
	q1.put(x)
	q2.put(y)
	# print(x,y)
	while(q1.qsize()!=0):
		x=q1.get()
		y=q2.get()
		vis[x][y]=1
		if(x>0):
			if(dp[x-1][y]>dp[x][y]+1 or vis[x-1][y]==0):
				dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
				q1.put(x-1)
				q2.put(y)
		if(x<n-1):
			if(dp[x+1][y]>1+dp[x][y] or vis[x+1][y]==0):
				dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
				q1.put(x+1)
				q2.put(y)
		if(y>0):
			if(dp[x][y-1]>1+dp[x][y]+1 or vis[x][y-1]==0):
				dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
				q1.put(x)
				q2.put(y-1)
		if(y<m-1):
			if(dp[x][y+1]>dp[x][y]+1 or vis[x][y+1]==0):
				dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
				q1.put(x)
				q2.put(y+1)
	# for i in range(n):
		# print(*dp[i])
	for i in range(t):
		a,b,c=map(int,input().split())
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print(0)
				else:
					print(1)
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
# input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j

from queue import Queue
if(x!=-1):
	q1=Queue(maxsize=10000)
	q2=Queue(maxsize=10000)
	q1.put(x)
	q2.put(y)
	# print(x,y)
	while(q1.qsize()!=0):
		x=q1.get()
		y=q2.get()
		vis[x][y]=1
		if(x>0 and vis[x-1][y]==0):
			dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
			q1.put(x-1)
			q2.put(y)
		if(x<n-1 and vis[x+1][y]==0):
			dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
			q1.put(x+1)
			q2.put(y)
		if(y>0 and vis[x][y-1]==0):
			dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
			q1.put(x)
			q2.put(y-1)
		if(y<m-1 and vis[x][y+1]==0):
			dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
			q1.put(x)
			q2.put(y+1)
	# print(dp)
	for i in range(t):
		a,b,c=map(int,input().split())
		if(i==62):
			print(b)
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print(0)
				else:
					print(1)
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
# input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j

from queue import Queue
if(x!=-1):
	q1=Queue(maxsize=10000)
	q2=Queue(maxsize=10000)
	q1.put(x)
	q2.put(y)
	# print(x,y)
	while(q1.qsize()!=0):
		x=q1.get()
		y=q2.get()
		vis[x][y]=1
		if(x>0):
			if(dp[x-1][y]>dp[x][y]+1 or vis[x-1][y]==0):
				dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
				q1.put(x-1)
				q2.put(y)
		if(x<n-1):
			if(dp[x+1][y]>1+dp[x][y] or vis[x+1][y]==0):
				dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
				q1.put(x+1)
				q2.put(y)
		if(y>0):
			if(dp[x][y-1]>1+dp[x][y]+1 or vis[x][y-1]==0):
				dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
				q1.put(x)
				q2.put(y-1)
		if(y<m-1):
			if(dp[x][y+1]>dp[x][y]+1 or vis[x][y+1]==0):
				dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
				q1.put(x)
				q2.put(y+1)
	# for i in range(n):
		# print(*dp[i])
	for i in range(t):
		a,b,c=map(int,input().split())
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print(0)
				else:
					print(1)
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
# input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				if(x==-1):
					x,y=i,j

from queue import Queue
if(x!=-1):
	q1=Queue(maxsize=10000)
	q2=Queue(maxsize=10000)
	q1.put(x)
	q2.put(y)
	# print(x,y)
	while(q1.qsize()!=0):
		x=q1.get()
		y=q2.get()
		vis[x][y]=1
		if(x>0 and vis[x-1][y]==0):
			dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
			q1.put(x-1)
			q2.put(y)
		if(x<n-1 and vis[x+1][y]==0):
			dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
			q1.put(x+1)
			q2.put(y)
		if(y>0 and vis[x][y-1]==0):
			dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
			q1.put(x)
			q2.put(y-1)
		if(y<m-1 and vis[x][y+1]==0):
			dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
			q1.put(x)
			q2.put(y+1)
	# print(dp)
	for i in range(t):
		a,b,c=map(int,input().split())
		if(i==62):
			print(c)
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print(0)
				else:
					print(1)
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(int(ar[a][b])^1)
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])


# from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
# sys.setrecursionlimit(10**6)
input = stdin.readline
# print = stdout.write

# @lru_cache()

n,m,t=map(int,input().split())
ar=[]
dp=[]
x=-1
y=-1
vis=[]
for i in range(n):
	ar.append(input())
	dp.append([inf]*m)
	vis.append([0]*m)
from collections import deque
q1=deque()
q2=deque()

for i in range(n):
	for j in range(m):
		if(j>0):
			if(ar[i][j]==ar[i][j-1]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j
		if(j<m-1):
			if(ar[i][j]==ar[i][j+1]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j
		if(i>0):
			if(ar[i-1][j]==ar[i][j]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j
		if(i<n-1):
			if(ar[i+1][j]==ar[i][j]):
				dp[i][j]=0
				q1.append(i)
				q2.append(j)
				if(x==-1):
					x,y=i,j

if(x!=-1):
	vis[x][y]=1
	while(len(q1)!=0):
		x=q1.popleft()
		y=q2.popleft()
		vis[x][y]=1
		if(x>0 and vis[x-1][y]==0):
			dp[x-1][y]=min(dp[x-1][y],1+dp[x][y])
			q1.append(x-1)
			q2.append(y)
			vis[x-1][y]=1
		if(x<n-1 and vis[x+1][y]==0):
			dp[x+1][y]=min(dp[x+1][y],1+dp[x][y])
			q1.append(x+1)
			q2.append(y)
			vis[x+1][y]=1
		if(y>0 and vis[x][y-1]==0):
			dp[x][y-1]=min(dp[x][y-1],1+dp[x][y])
			q1.append(x)
			q2.append(y-1)
			vis[x][y-1]=1
		if(y<m-1 and vis[x][y+1]==0):
			dp[x][y+1]=min(dp[x][y+1],1+dp[x][y])
			q1.append(x)
			q2.append(y+1)
			vis[x][y+1]=1
	# for i in range(n):
		# print(*dp[i])
	for i in range(t):
		a,b,c=map(int,input().split())
		a-=1
		b-=1
		if(dp[a][b]==0):
			if(c%2==0):
				print(ar[a][b])
			else:
				if(ar[a][b]=='1'):
					print('0')
				else:
					print('1')
		else:
			if(dp[a][b]+1>c):
				print(ar[a][b])
			else:
				if(dp[a][b]%2==1):
					if(c%2==1):
						print(ar[a][b])
					else:
						print(str(int(ar[a][b])^1))
				else:
					if(c%2==0):
						print(ar[a][b])
					else:
						print(str(int(ar[a][b])^1))
		# print("\n")
else:
	for i in range(t):
		a,b,c=map(int,input().split())
		print(ar[a-1][b-1])
		# print("\n")


import sys
input = sys.stdin.readline
n,m,t=map(int,input().split())
board=[0]*n
change=[0]*n
for i in range(n):
    board[i]=[0]*m
    change[i]=[-1]*m
    s=input()
    for j in range(m):
        if s[j]=="1":
            board[i][j]=1
tod=[]
for i in range(n):
    for j in range(m):
        if change[i][j]==-1:
            c=-1
            if i>0:
                if board[i-1][j]==board[i][j]:
                    c=0
            if i<n-1:
                if board[i+1][j]==board[i][j]:
                    c=0
            if j>0:
                if board[i][j-1]==board[i][j]:
                    c=0
            if j<m-1:
                if board[i][j+1]==board[i][j]:
                    c=0
            if c==0:
                change[i][j]=0
                tod.append([i,j])
c=0
while len(tod)>0:
    tod2=[]
    for ij in tod:
        i=ij[0]
        j=ij[1]
        if True:
            change[i][j]=c
            if i>0:
                if change[i-1][j]==-1:
                    tod2.append([i-1,j])
            if i<n-1:
                if change[i+1][j]==-1:
                    tod2.append([i+1,j])
            if j>0:
                if change[i][j-1]==-1:
                    tod2.append([i,j-1])
            if j<m-1:
                if change[i][j+1]==-1:
                    tod2.append([i,j+1])
    c+=1
    tod=tod2
for f in range(t):
    i,j,p=map(int,input().split())
    i-=1
    j-=1
    if change[i][j]==-1 or change[i][j]>=p:
        print(board[i][j])
    else:
        p-=change[i][j]
        p%=2
        print((p+board[i][j])%2)


import sys
input = sys.stdin.readline
n,m,t=map(int,input().split())
board=[0]*n
change=[0]*n
for i in range(n):
    board[i]=[0]*m
    change[i]=[-1]*m
    s=input()
    for j in range(m):
        if s[j]=="1":
            board[i][j]=1
tod=[]
for i in range(n):
    for j in range(m):
        if change[i][j]==-1:
            c=-1
            if i>0:
                if board[i-1][j]==board[i][j]:
                    c=0
            if i<n-1:
                if board[i+1][j]==board[i][j]:
                    c=0
            if j>0:
                if board[i][j-1]==board[i][j]:
                    c=0
            if j<m-1:
                if board[i][j+1]==board[i][j]:
                    c=0
            if c==0:
                change[i][j]=0
                tod.append([i,j])
c=0
while len(tod)>0:
    tod2=[]
    for ij in tod:
        i=ij[0]
        j=ij[1]
        if change[i][j]==-1 or c==0:
            change[i][j]=c
            if i>0:
                if change[i-1][j]==-1:
                    tod2.append([i-1,j])
            if i<n-1:
                if change[i+1][j]==-1:
                    tod2.append([i+1,j])
            if j>0:
                if change[i][j-1]==-1:
                    tod2.append([i,j-1])
            if j<m-1:
                if change[i][j+1]==-1:
                    tod2.append([i,j+1])
    c+=1
    tod=tod2
for f in range(t):
    i,j,p=map(int,input().split())
    i-=1
    j-=1
    if change[i][j]==-1 or change[i][j]>p:
        print(board[i][j])
    else:
        p-=change[i][j]
        p+=board[i][j]
        p%=2
        print(p)


def main():

    import sys, math
    #from bisect import bisect_left as bl, bisect_right as br, insort
    from heapq import heapify, heappush, heappop
    from collections import defaultdict as dd, deque, Counter
    #from itertools import permutations,combinations
    def data(): return sys.stdin.readline().strip()
    def mdata(): return list(map(int, data().split()))
    def out(*var, end="\n"): sys.stdout.write('\n'.join(map(str, var))+end)
    # sys.setrecursionlimit(100000)
    INF = float('inf')
    mod = int(1e9)+7

    n, m, t = mdata()
    mat = []
    vis = [[INF] * m for i in range(n)]
    q = deque()
    steps = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    for i in range(n):
        mat.append(list(map(int, list(data()))))
    for i in range(n):
        for j in range(m):
            for k, l in steps:
                if n > i + k >= 0 and m > j + l >= 0 and mat[i][j] == mat[i + k][j + l]:
                        q.append([i, j])
                        vis[i][j] = 0
                        break
    while q:
        x, y = q.popleft()
        for k, l in steps:
            if n > x + k >= 0 and m > y + l >= 0 and vis[x][y] + 1 < vis[x + k][y + l]:
                q.append([x + k, y + l])
                vis[x + k][y + l] = vis[x][y] + 1
    ans = []
    for i in range(t):
        i, j, p = mdata()
        i -= 1
        j -= 1
        if vis[i][j] >= p:
            ans.append(mat[i][j])
        else:
            k = p - vis[i][j]
            if k % 2 == 0:
                ans.append(mat[i][j])
            else:
                ans.append(1 - mat[i][j])
    out(*ans)

if __name__ == '__main__':
    main()


def main():

    import sys, math
    #from bisect import bisect_left as bl, bisect_right as br, insort
    from heapq import heapify, heappush, heappop
    from collections import defaultdict as dd, deque, Counter
    #from itertools import permutations,combinations
    def data(): return sys.stdin.readline().strip()
    def mdata(): return list(map(int, data().split()))
    def out(*var, end="\n"): sys.stdout.write('\n'.join(map(str, var))+end)
    # sys.setrecursionlimit(100000)
    INF = int(1e9)
    mod = int(1e9)+7

    n, m, t = mdata()
    mat = []
    vis = [[INF] * m for i in range(n)]
    q = deque()
    steps = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    for i in range(n):
        mat.append(list(map(int, list(data()))))
    for i in range(n):
        for j in range(m):
            for k, l in steps:
                if n > i + k >= 0 and m > j + l >= 0 and mat[i][j] == mat[i + k][j + l]:
                        q.append([i, j])
                        vis[i][j] = 0
                        break
    while q:
        x, y = q.popleft()
        for k, l in steps:
            if n > x + k >= 0 and m > y + l >= 0 and vis[x][y] + 1 < vis[x + k][y + l]:
                q.append([x + k, y + l])
                vis[x + k][y + l] = vis[x][y] + 1
    ans = []
    for i in range(t):
        i, j, p = mdata()
        i -= 1
        j -= 1
        if vis[i][j] >= p:
            ans.append(mat[i][j])
        else:
            k = p - vis[i][j]
            if k % 2 == 0:
                ans.append(mat[i][j])
            else:
                ans.append(1 - mat[i][j])
    out(*ans)

if __name__ == '__main__':
    main()


from collections import deque
n, m, t = map(int, input().split())
s = []
for _ in range(n):
    s.append(input())
query = []
for _ in range(t):
    i, j, p = map(int, input().split())
    query.append((i-1, j-1, p))

dist = [[-1]*m for _ in range(n)]
q = deque()
dxdy = [(-1, 0), (0, -1), (0, 1), (1, 0)]
for y in range(n):
    for x in range(m):
        for dx, dy in dxdy:
            if not (0<=x+dx<m and 0<=y+dy<n):
                continue
            if s[y][x]==s[y+dy][x+dx]:
                dist[y][x] = 0
                q.append((y, x))
                break

while q:
    y, x = q.popleft()
    for dx, dy in dxdy:
        if not (0<=x+dx<m and 0<=y+dy<n):
            continue
        if dist[y+dy][x+dx]>=0:
            continue
        dist[y+dy][x+dx] = dist[y][x] + 1
        q.append((y+dy, x+dx))

for y, x, p in query:
    if dist[y][x]<0 or p-dist[y][x]<0:
        print(s[y][x])
    else:
        print((int(s[y][x])+p-dist[y][x])%2)


import sys

input = sys.stdin.readline

n, m, t = map(int, input().split())
cell = [list(input().rstrip()) for _ in range(n)]
cnt = [[False] * m for _ in range(n)]
for i in range(n):
    for j in range(m):
        if i - 1 >= 0 and cell[i - 1][j] == cell[i][j]:
            cnt[i][j] = True
        if i + 1 < n and cell[i + 1][j] == cell[i][j]:
            cnt[i][j] = True
        if j - 1 >= 0 and cell[i][j - 1] == cell[i][j]:
            cnt[i][j] = True
        if j + 1 < m and cell[i][j + 1] == cell[i][j]:
            cnt[i][j] = True

num = 0
cur = []
dp = [None] * n*m
for i in range(n):
    for j in range(m):
        if cnt[i][j]:
            dp[i + n * j] = 0
            cur.append(i + n * j)
            num += 1

if num > 0:
    while None in dp:
        temp = cur
        cur = []
        for x in temp:
            i, j = x % n, x // n
            if i - 1 >= 0 and dp[x - 1] is None:
                dp[x - 1] = dp[x] + 1
                cur.append(x - 1)
            if i + 1 < n and dp[x + 1] is None:
                dp[x + 1] = dp[x] + 1
                cur.append(x + 1)
            if j - 1 >= 0 and dp[x - n] is None:
                dp[x - n] = dp[x] + 1
                cur.append(x - n)
            if j + 1 < m and dp[x + n] is None:
                dp[x + n] = dp[x] + 1
                cur.append(x + n)

for _ in range(t):
    i, j, p = map(int, input().split())
    i -= 1
    j -= 1
    if cnt[i][j]:
        print((int(cell[i][j]) + p) % 2)
    else:
        if num == 0:
            print(cell[i][j])
            continue
        if dp[i + n * j] > p:
            print(cell[i][j])
        else:
            p -= dp[i + n * j]
            print((int(cell[i][j]) + p) % 2)