#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const int M = 1000;
int n, m, t;
int deg[M + 2][M + 2];
char a[M + 2][M + 2];
queue<pair<int, int> > q;
bool valid(int x, int u) { return (x >= 1 && u >= 1 && x <= n && u <= m); }
char op(char x) {
  if (x == '1') return '0';
  return '1';
}
int main() {
  cin >> n >> m >> t;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &a[i][j]);
      deg[i][j] = -1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int di = -1; di <= 1; ++di) {
        for (int dj = -1; dj <= 1; ++dj) {
          if (di * dj != 0 || di + dj == 0) continue;
          int ni, nj;
          ni = i + di;
          nj = j + dj;
          if (valid(ni, nj)) {
            if (a[i][j] == a[ni][nj]) {
              deg[i][j] = 0;
              q.push(make_pair(i, j));
            }
          }
        }
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> cord = q.front();
    q.pop();
    int x = cord.first;
    int u = cord.second;
    for (int di = -1; di <= 1; ++di) {
      for (int dj = -1; dj <= 1; ++dj) {
        if (di * dj != 0 || di + dj == 0) continue;
        int ni, nj;
        ni = x + di;
        nj = u + dj;
        if (valid(ni, nj)) {
          if (deg[ni][nj] == -1) {
            deg[ni][nj] = deg[x][u] + 1;
            q.push(make_pair(ni, nj));
          }
        }
      }
    }
  }
  while (t) {
    --t;
    long long i, j, p;
    scanf("%lld%lld%lld", &i, &j, &p);
    if (p <= deg[i][j] || deg[i][j] == -1) {
      printf("%c\n", a[i][j]);
    } else {
      char answ = ((p - deg[i][j]) % 2 == 0) ? a[i][j] : op(a[i][j]);
      printf("%c\n", answ);
    }
  }
  return 0;
}
