#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
int n, m, t;
char mz[1010][1010];
int dx[]{0, 0, -1, 1};
int dy[]{-1, 1, 0, 0};
int d[1010][1010];
queue<pair<int, int> > q;
int leg(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
void bfs() {
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (d[nx][ny] != -1) continue;
      if (leg(nx, ny)) {
        d[nx][ny] = d[x][y] + 1;
        q.push(pair<int, int>(nx, ny));
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%s", mz[i] + 1);
  }
  memset(d, -1, sizeof(d));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        if (mz[i + dx[k]][j + dy[k]] == mz[i][j]) {
          q.push(pair<int, int>(i, j));
          d[i][j] = 0;
          break;
        }
      }
    }
  }
  bfs();
  while (t--) {
    int x, y;
    long long q;
    scanf("%d%d%lld", &x, &y, &q);
    if (q <= d[x][y] || d[x][y] == -1) {
      printf("%c\n", mz[x][y]);
      continue;
    }
    q -= d[x][y];
    if (q & 1) {
      printf("%d\n", ((mz[x][y] - '0') ^ 1));
    } else
      printf("%c\n", mz[x][y]);
  }
  return 0;
}
