#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
char s[maxn][maxn];
int dis[maxn][maxn];
int n, m;
struct node {
  int x, y, step;
  node() {}
  node(int _x, int _y, int _step) {
    x = _x;
    y = _y;
    step = _step;
  }
};
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool check(int x, int y) {
  if (x > n || x < 1 || y > m || y < 1) return false;
  if (dis[x][y] != -1) return false;
  return true;
}
bool judge(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int nx = x + dir[i][0];
    int ny = y + dir[i][1];
    if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && s[x][y] == s[nx][ny])
      return true;
  }
  return false;
}
void initial() {
  memset(dis, -1, sizeof(dis));
  queue<struct node> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (judge(i, j)) {
        dis[i][j] = 0;
        q.push(node(i, j, dis[i][j]));
      }
    }
  }
  while (!q.empty()) {
    struct node TT = q.front();
    q.pop();
    int x = TT.x;
    int y = TT.y;
    int step = TT.step;
    for (int i = 0; i < 4; i++) {
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      int nstep = step + 1;
      if (check(nx, ny)) {
        dis[nx][ny] = nstep;
        q.push(node(nx, ny, dis[nx][ny]));
      }
    }
  }
}
int main(void) {
  int T;
  scanf("%d%d%d", &n, &m, &T);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  initial();
  while (T--) {
    int i, j;
    long long p;
    scanf("%d%d%I64d", &i, &j, &p);
    int res = s[i][j] - '0';
    if (dis[i][j] >= 0 && p - dis[i][j] >= 0) {
      if ((p - dis[i][j]) & 1) res = res ^ 1;
    }
    printf("%d\n", res);
  }
  return 0;
}
