#include <bits/stdc++.h>
int n, m, t;
int a[1111][1111], T[1111][1111];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
struct state {
  int x, y;
  state(int X = 0, int Y = 0) {
    x = X;
    y = Y;
  }
};
std::queue<state> q;
void dfs(int x, int y) {
  T[x][y] = 0;
  q.push(state(x, y));
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
    if (T[nx][ny] ^ -1) continue;
    if (a[x][y] ^ a[nx][ny]) continue;
    dfs(nx, ny);
  }
}
void ff() {
  while (!q.empty()) {
    state t = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int x = t.x + dx[i];
      int y = t.y + dy[i];
      if (x < 1 || x > n || y < 1 || y > m) continue;
      if (T[x][y] == -1) {
        T[x][y] = T[t.x][t.y] + 1;
        q.push(state(x, y));
      }
    }
  }
}
int main() {
  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]);
      T[i][j] = -1;
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (T[i][j] ^ -1) continue;
      int cnt = 0;
      for (int k = 0; k < 4; ++k) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (x < 1 || x > n || y < 1 || y > m) continue;
        if (a[x][y] ^ a[i][j]) continue;
        ++cnt;
      }
      if (cnt) dfs(i, j);
    }
  ff();
  while (t--) {
    int x, y;
    long long Time;
    scanf("%d%d%lld", &x, &y, &Time);
    if (Time < T[x][y] || T[x][y] == -1)
      printf("%d\n", a[x][y]);
    else
      printf("%d\n", a[x][y] ^ Time - T[x][y] & 1);
  }
  return 0;
}
