#include <bits/stdc++.h>
using namespace std;
long long n, m, w;
char ch[1001][1001];
long long mark[1001][1001];
long long a[4] = {1, -1, 0, 0};
long long b[4] = {0, 0, 1, -1};
long long vis[1001][1001];
long long dist[1001][1001];
vector<pair<long long, long long> > v;
queue<pair<long long, long long> > q;
long long f(long long i, long long j) {
  for (long long x = 0; x < 4; x++) {
    long long l = i + a[x];
    long long r = j + b[x];
    if (l < 1 || r < 1 || l > n || r > m) continue;
    if (ch[l][r] == ch[i][j]) return 1;
  }
  return 0;
}
void dfs(long long i, long long j) {
  vis[i][j] = 1;
  q.push({i, j});
  dist[i][j] = 0;
  mark[i][j] = 1;
  for (long long x = 0; x < 4; x++) {
    long long l = i + a[x];
    long long r = j + b[x];
    if (vis[l][r]) continue;
    if (l < 1 || r < 1 || l > n || r > m) continue;
    if (ch[l][r] == ch[i][j]) dfs(l, r);
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m >> w;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) cin >> ch[i][j];
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) dist[i][j] = 1e18;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (vis[i][j] == 0 && f(i, j)) dfs(i, j);
    }
  }
  while (!q.empty()) {
    pair<long long, long long> p = q.front();
    q.pop();
    long long x = p.first;
    long long y = p.second;
    for (long long i = 0; i < 4; i++) {
      long long l = x + a[i];
      long long r = y + b[i];
      if (mark[l][r]) continue;
      if (l < 1 || r < 1 || l > n || r > m) continue;
      q.push({l, r});
      mark[l][r] = 1;
      dist[l][r] = min(1 + dist[x][y], dist[l][r]);
    }
  }
  while (w--) {
    long long i, j, p;
    cin >> i >> j >> p;
    if (dist[i][j] == 1e18 || dist[i][j] >= p) {
      cout << ch[i][j] << '\n';
      continue;
    }
    long long diff = p - dist[i][j];
    if (diff % 2)
      cout << ((ch[i][j] - '0') ^ 1) << '\n';
    else
      cout << ch[i][j] << '\n';
  }
}
