#include <bits/stdc++.h>
using namespace std;
int n, m, T, a[1010][1010], dis[1010][1010], dx[4] = {1, -1, 0, 0},
                                             dy[4] = {0, 0, 1, -1}, INF = 1e7;
char c[1010];
struct dd {
  int x, y;
};
queue<dd> q;
int main() {
  scanf("%d%d%d", &n, &m, &T);
  for (int i = 1; i <= n; i++) {
    scanf("%s", c + 1);
    for (int j = 1; j <= m; j++) a[i][j] = c[j] - '0', dis[i][j] = INF;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int fl = 0;
      for (int k = 0; k < 4; k++) {
        int xc = dx[k] + i, yc = dy[k] + j;
        if (xc >= 1 && xc <= n && yc >= 1 && yc <= m && a[xc][yc] == a[i][j])
          fl = 1;
      }
      if (fl) dis[i][j] = 0, q.push((dd){i, j});
    }
  while (!q.empty()) {
    dd wer = q.front();
    q.pop();
    int x = wer.x, y = wer.y;
    for (int i = 0; i < 4; i++) {
      int xc = x + dx[i], yc = y + dy[i];
      if (xc >= 1 && xc <= n && yc >= 1 && yc <= m && dis[xc][yc] == INF) {
        dis[xc][yc] = dis[x][y] + 1;
        q.push((dd){xc, yc});
      }
    }
  }
  long long t;
  for (int i = 0, x, y; i < T; i++) {
    scanf("%d%d%lld", &x, &y, &t);
    if (dis[x][y] == INF || dis[x][y] > t)
      printf("%d\n", a[x][y]);
    else
      printf("%d\n", ((t - dis[x][y]) & 1) ^ a[x][y]);
  }
  return 0;
}
