#include <bits/stdc++.h>
using namespace std;
long long ch[1005][1005], t[1005][1005];
long long n, m, Q;
void topusort() {
  queue<pair<long long, long long> > Q;
  memset(t, -1, sizeof t);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (ch[i][j] == ch[i - 1][j] || ch[i][j] == ch[i][j - 1] ||
          ch[i][j] == ch[i + 1][j] || ch[i][j] == ch[i][j + 1]) {
        Q.push(make_pair(i, j));
        t[i][j] = 0;
      }
    }
  }
  while (Q.size()) {
    long long i = Q.front().first, j = Q.front().second;
    Q.pop();
    if (t[i - 1][j] == -1 && i - 1 >= 1)
      t[i - 1][j] = t[i][j] + 1, Q.push(make_pair(i - 1, j));
    if (t[i][j - 1] == -1 && j - 1 >= 1)
      t[i][j - 1] = t[i][j] + 1, Q.push(make_pair(i, j - 1));
    if (t[i + 1][j] == -1 && i + 1 <= n)
      t[i + 1][j] = t[i][j] + 1, Q.push(make_pair(i + 1, j));
    if (t[i][j + 1] == -1 && j + 1 <= m)
      t[i][j + 1] = t[i][j] + 1, Q.push(make_pair(i, j + 1));
  }
}
signed main() {
  memset(ch, -1, sizeof ch);
  scanf("%lld %lld %lld", &n, &m, &Q);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      scanf("%1lld", &ch[i][j]);
    }
  }
  topusort();
  while (Q--) {
    long long x, y, z;
    scanf("%lld %lld %lld", &x, &y, &z);
    if (t[x][y] >= z || t[x][y] == -1)
      printf("%lld\n", ch[x][y]);
    else
      printf("%lld\n", ch[x][y] ^ ((z - t[x][y]) % 2));
  }
  return 0;
}
