#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 10;
long long n, m, t, tot, inf;
long long a[N][N], id[N][N], tim[N * N], bl[N * N], sz[N * N];
char s[N][N];
long long di1[4] = {-1, 0, 1, 0}, di2[4] = {0, 1, 0, -1};
inline long long qx(long long p) { return (p % m == 0) ? p / m : p / m + 1; }
inline long long qy(long long p) { return (p % m == 0) ? m : p % m; }
queue<long long> q;
void dfs(long long x, long long y, long long tag) {
  bl[id[x][y]] = tot, sz[tot]++;
  for (long long i = 0; i < 4; i++) {
    long long fx = x + di1[i], fy = y + di2[i];
    if (id[fx][fy] && !bl[id[fx][fy]] && a[fx][fy] == tag) dfs(fx, fy, tag);
  }
}
void bfs() {
  memset(tim, 0x3f, sizeof(tim));
  inf = tim[0];
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++)
      if (sz[bl[id[i][j]]] >= 2) tim[id[i][j]] = 0, q.push(id[i][j]);
  while (!q.empty()) {
    long long rt = q.front(), fx = qx(rt), fy = qy(rt);
    q.pop();
    for (long long i = 0; i < 4; i++) {
      long long dx = fx + di1[i], dy = fy + di2[i];
      if (id[dx][dy] && tim[id[dx][dy]] > tim[id[fx][fy]] + 1ll)
        tim[id[dx][dy]] = tim[id[fx][fy]] + 1ll, q.push(id[dx][dy]);
    }
  }
}
signed main() {
  scanf("%lld%lld%lld", &n, &m, &t);
  for (long long i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++)
      a[i][j] = (s[i][j] - '0'), id[i][j] = (i - 1) * m + j;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++)
      if (!bl[id[i][j]]) ++tot, dfs(i, j, a[i][j]);
  bfs();
  while (t--) {
    long long x, y, p;
    scanf("%lld%lld%lld", &x, &y, &p);
    if (p <= tim[id[x][y]])
      printf("%lld\n", a[x][y]);
    else
      printf("%lld\n", a[x][y] ^ ((p - tim[id[x][y]]) & 1));
  }
}
