#include <bits/stdc++.h>
using namespace std;
long long t1, n, m, scc, sz, v[1010][1010], a[1010][1010], mp[1000010],
    d[1000010];
vector<long long> zh[1000010];
queue<long long> q;
void dfs(long long x, long long y) {
  sz++;
  v[x][y] = scc;
  zh[scc].push_back((x - 1) * m + y);
  if (x > 1 && !v[x - 1][y] && a[x - 1][y] == a[x][y]) dfs(x - 1, y);
  if (x < n && !v[x + 1][y] && a[x + 1][y] == a[x][y]) dfs(x + 1, y);
  if (y > 1 && !v[x][y - 1] && a[x][y - 1] == a[x][y]) dfs(x, y - 1);
  if (y < m && !v[x][y + 1] && a[x][y + 1] == a[x][y]) dfs(x, y + 1);
}
char s[1010];
int main() {
  long long i, j, u, x, y, k, w;
  scanf("%lld%lld%lld", &n, &m, &t1);
  for (i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (j = 1; j <= m; j++) a[i][j] = s[j] - '0';
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (!v[i][j]) {
        sz = 0;
        scc++;
        dfs(i, j);
        if (sz > 1) q.push(scc), mp[scc] = 1, d[scc] = 0;
      }
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (j = 0; j < zh[u].size(); j++) {
      x = (zh[u][j] - 1) / m + 1;
      y = zh[u][j] - (x - 1) * m;
      if (x > 1 && !mp[v[x - 1][y]])
        q.push(v[x - 1][y]), mp[v[x - 1][y]] = 1, d[v[x - 1][y]] = d[u] + 1;
      if (x < n && !mp[v[x + 1][y]])
        q.push(v[x + 1][y]), mp[v[x + 1][y]] = 1, d[v[x + 1][y]] = d[u] + 1;
      if (y > 1 && !mp[v[x][y - 1]])
        q.push(v[x][y - 1]), mp[v[x][y - 1]] = 1, d[v[x][y - 1]] = d[u] + 1;
      if (y < m && !mp[v[x][y + 1]])
        q.push(v[x][y + 1]), mp[v[x][y + 1]] = 1, d[v[x][y + 1]] = d[u] + 1;
    }
  }
  while (t1--) {
    scanf("%lld%lld%lld", &x, &y, &k);
    if (!mp[v[x][y]]) {
      printf("%lld\n", a[x][y]);
      continue;
    }
    if (k < d[v[x][y]])
      printf("%lld\n", a[x][y]);
    else {
      w = (k - d[v[x][y]]) % 2;
      printf("%lld\n", a[x][y] ^ w);
    }
  }
}
