#include <bits/stdc++.h>
int a[1005][1005];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int f[1005][1005];
struct Element {
  int r, c;
  Element(int r = 0, int c = 0) : r(r), c(c) {}
};
Element queue[1000005];
void dfs(int r, int c, int n, int m) {
  f[r][c] = 1;
  for (int i = 0; i < 4; i++) {
    int rr = r + dx[i], cc = c + dy[i];
    if (rr >= 1 && rr <= n && cc >= 1 && cc <= m && a[rr][cc] == a[r][c] &&
        f[rr][cc] == 0) {
      dfs(rr, cc, n, m);
    }
  }
}
int main() {
  int n, m, t;
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) scanf("%1d", &a[i][j]);
  }
  int head = 0, tail = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (f[i][j] == 0) {
        int find = 0;
        for (int k = 0; k < 4; k++) {
          int r = i + dx[k], c = j + dy[k];
          if (r >= 1 && r <= n && c >= 1 && c <= m && a[r][c] == a[i][j]) {
            find = 1;
          }
        }
        if (find) {
          dfs(i, j, n, m);
          queue[++tail] = Element(i, j);
        }
      } else
        queue[++tail] = Element(i, j);
    }
  }
  while (head <= tail) {
    Element u = queue[head++];
    for (int i = 0; i < 4; i++) {
      int r = u.r + dx[i], c = u.c + dy[i];
      if (r >= 1 && r <= n && c >= 1 && c <= m && f[r][c] == 0) {
        f[r][c] = f[u.r][u.c] + 1;
        queue[++tail] = Element(r, c);
      }
    }
  }
  while (t--) {
    int r, c;
    long long p;
    scanf("%d%d%lld", &r, &c, &p);
    if (f[r][c] == 0 || p < f[r][c])
      printf("%d\n", a[r][c]);
    else {
      long long change = (p - f[r][c] + 1) % 2;
      if (change)
        printf("%d\n", !a[r][c]);
      else
        printf("%d\n", a[r][c]);
    }
  }
  return 0;
}
