#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n, long long m) {
  return n % m == 0 ? m : gcd(m, n % m);
}
const int Maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
char Map[Maxn][Maxn];
long long dist[Maxn][Maxn];
bool visit[Maxn][Maxn];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
queue<pair<int, int> > q;
void Ini() {
  memset(dist, inf, sizeof(dist));
  for (long long i = 0; i <= Maxn - 1; i++)
    for (long long j = 0; j <= Maxn - 1; j++) Map[i][j] = -1;
}
void dfs(int x, int y) {
  visit[x][y] = true;
  dist[x][y] = 0;
  q.push(pair<int, int>(x, y));
  for (long long i = 0; i <= 3; i++) {
    int xx = dx[i];
    int yy = dy[i];
    if (Map[xx][yy] == Map[x][y] && !visit[xx][yy]) dfs(xx, yy);
  }
}
void bfs() {
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (long long i = 0; i <= 3; i++) {
      int xx = p.first + dx[i];
      int yy = p.second + dy[i];
      if (Map[xx][yy] != -1 && dist[p.first][p.second] + 1 < dist[xx][yy]) {
        dist[xx][yy] = dist[p.first][p.second] + 1;
        q.push(pair<int, int>(xx, yy));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  Ini();
  int n, m, t;
  cin >> n >> m >> t;
  for (long long i = 1; i <= n; i++) {
    cin >> (Map[i] + 1);
    Map[i][m + 1] = -1;
  }
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) Map[i][j] -= '0';
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) {
      for (long long k = 0; k <= 3; k++) {
        int xx = i + dx[k];
        int yy = j + dy[k];
        if (Map[xx][yy] == Map[i][j]) {
          dfs(i, j);
          break;
        }
      }
    }
  bfs();
  while (t--) {
    long long x, y, z;
    cin >> x >> y >> z;
    if (dist[x][y] == inf)
      cout << int(Map[x][y]) << endl;
    else {
      if (z <= dist[x][y])
        cout << int(Map[x][y]) << endl;
      else if ((z - dist[x][y]) & 1)
        cout << 1 - Map[x][y] << endl;
      else
        cout << int(Map[x][y]) << endl;
    }
  }
  return 0;
}
