#include <bits/stdc++.h>
using namespace std;
long long rd() {
  long long k = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    k = (k << 1) + (k << 3) + c - 48;
    c = getchar();
  }
  return k * f;
}
void wr(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) wr(x / 10);
  putchar(x % 10 + '0');
}
int n, m, q;
char s[1010][1010];
int bel[1010][1010];
int tim[1010][1010];
struct Node {
  int x, y;
  Node(int x = 0, int y = 0) : x(x), y(y) {}
};
queue<Node> Q;
inline int chk(Node x) { return 1 <= x.x && x.x <= n && 1 <= x.y && x.y <= m; }
int main() {
  memset(bel, 0xff, sizeof(bel));
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int tmp = 0;
      tmp |= s[i][j] == s[i - 1][j];
      tmp |= s[i][j] == s[i + 1][j];
      tmp |= s[i][j] == s[i][j - 1];
      tmp |= s[i][j] == s[i][j + 1];
      if (tmp) {
        bel[i][j] = s[i][j] - 48;
        Q.push(Node(i, j));
      }
    }
  while (!Q.empty()) {
    Node x = Q.front();
    Q.pop();
    Node t;
    t = Node(x.x + 1, x.y);
    if (chk(t) && bel[t.x][t.y] == -1)
      Q.push(t), bel[t.x][t.y] = bel[x.x][x.y],
                 tim[t.x][t.y] = tim[x.x][x.y] + 1;
    t = Node(x.x - 1, x.y);
    if (chk(t) && bel[t.x][t.y] == -1)
      Q.push(t), bel[t.x][t.y] = bel[x.x][x.y],
                 tim[t.x][t.y] = tim[x.x][x.y] + 1;
    t = Node(x.x, x.y + 1);
    if (chk(t) && bel[t.x][t.y] == -1)
      Q.push(t), bel[t.x][t.y] = bel[x.x][x.y],
                 tim[t.x][t.y] = tim[x.x][x.y] + 1;
    t = Node(x.x, x.y - 1);
    if (chk(t) && bel[t.x][t.y] == -1)
      Q.push(t), bel[t.x][t.y] = bel[x.x][x.y],
                 tim[t.x][t.y] = tim[x.x][x.y] + 1;
  }
  while (q--) {
    long long x = rd(), y = rd();
    long long k = rd();
    if (bel[x][y] == -1 || k < tim[x][y])
      putchar(s[x][y]);
    else
      putchar(48 | (bel[x][y] ^ (k & 1)));
    putchar('\n');
  }
  return 0;
}
