#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e3;
int n, m, q, a[N + 7][N + 7], b[N + 7][N + 7];
int ok(char c) { return c == '1' || c == '0'; }
int in(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
int tx[] = {0, 0, -1, 1}, ty[] = {-1, 1, 0, 0};
int main() {
  scanf("%d%d%d", &n, &m, &q);
  char c;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      while (!ok(c = getchar()))
        ;
      a[i][j] = (c == '1');
    }
  memset(b, 0x3f, sizeof b);
  vector<pair<int, int> > qe;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int ok = 0;
      for (int k = 0; k < 4; k++) {
        int xt = i + tx[k], yt = j + ty[k];
        if (in(xt, yt) && a[xt][yt] == a[i][j]) {
          ok = 1;
          continue;
        }
      }
      if (ok) b[i][j] = 0, qe.push_back(make_pair(i, j));
    }
  for (int i = 0; i < int((qe).size()); i++) {
    int x = qe[i].first, y = qe[i].second;
    for (int k = 0; k < 4; k++) {
      int xt = x + tx[k], yt = y + ty[k];
      if (in(xt, yt) && b[xt][yt] > b[x][y] + 1) {
        b[xt][yt] = b[x][y] + 1;
        qe.push_back(make_pair(xt, yt));
      }
    }
  }
  for (int i = 1; i <= q; i++) {
    int x, y;
    long long t;
    scanf("%d%d%lld", &x, &y, &t);
    if (b[x][y] > 2000)
      printf("%d\n", a[x][y]);
    else
      printf("%lld\n", t <= b[x][y] ? a[x][y] : a[x][y] ^ (1 & (t - b[x][y])));
  }
  return 0;
}
