#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
const int V = N * N;
int n, m, t;
char str[N][N];
int bl[N][N];
int node = 0;
vector<int> G[V];
bool statu[V];
int size[V], dis[V];
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
void fill(int x, int y, int c) {
  bl[x][y] = c, ++size[c];
  char tmp = str[x][y];
  for (register int i = 0; i < 4; i++) {
    x += dx[i], y += dy[i];
    if (x >= 1 && x <= n && y >= 1 && y <= m)
      if (str[x][y] == tmp && !bl[x][y]) fill(x, y, c);
    x -= dx[i], y -= dy[i];
  }
}
bool inq[V];
void initdis() {
  queue<int> Q;
  for (register int i = 1; i <= node; i++)
    if (size[i] > 1) Q.push(i), inq[i] = 1;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    inq[cur] = false;
    for (register int i = 0; i < G[cur].size(); i++) {
      int nxt = G[cur][i], wei = dis[cur] + 1;
      if (wei < dis[nxt]) {
        dis[nxt] = wei;
        if (inq[nxt]) continue;
        Q.push(nxt), inq[nxt] = true;
      }
    }
  }
}
signed main() {
  scanf("%d%d%d", &n, &m, &t);
  for (register int i = 1; i <= n; i++) scanf("%s", str[i] + 1);
  for (register int i = 1; i <= n; i++)
    for (register int j = 1; j <= m; j++)
      if (bl[i][j] == 0) fill(i, j, ++node);
  for (register int i = 1; i <= n; i++)
    for (register int j = 1; j <= m; j++)
      for (register int k = 0; k < 4; k++)
        if (1 <= i + dx[k] && i + dx[k] <= n)
          if (1 <= j + dy[k] && j + dy[k] <= m)
            if (bl[i][j] != bl[i + dx[k]][j + dy[k]]) {
              G[bl[i + dx[k]][j + dy[k]]].push_back(bl[i][j]);
              G[bl[i][j]].push_back(bl[i + dx[k]][j + dy[k]]);
            }
  for (register int i = 1; i <= node; i++) {
    sort(G[i].begin(), G[i].end());
    G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
  }
  memset(dis, 0x3f, sizeof dis);
  for (register int i = 1; i <= node; i++)
    if (size[i] > 1) dis[i] = 0;
  initdis();
  while (t--) {
    int x, y;
    long long q;
    scanf("%d%d%lld", &x, &y, &q);
    if (dis[bl[x][y]] == 0x3f3f3f3f) {
      printf("%d\n", str[x][y] - '0');
      continue;
    }
    int ans = str[x][y] - '0';
    ans ^= (max(0ll, q - dis[bl[x][y]]) & 1);
    printf("%d\n", ans);
  }
  return 0;
}
