#include <bits/stdc++.h>
using namespace std;
char mp[1001][1001];
int visit[1001][1001];
int dist[1001][1001];
int N, M;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool chk(int x, int y) { return 0 <= x && x < N && 0 <= y && y < M; }
void dfs(int x, int y, bool isfirst) {
  visit[x][y] = 1;
  if (!isfirst) dist[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (chk(nx, ny) && mp[x][y] == mp[nx][ny] && visit[nx][ny] == 0) {
      dist[x][y] = 1;
      dfs(nx, ny, false);
    }
  }
}
int main(int argc, char *argv[]) {
  int Q;
  scanf("%d %d %d", &N, &M, &Q);
  for (int i = 0; i < N; i++) scanf("%s", mp[i]);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (visit[i][j] == 0) dfs(i, j, true);
    }
  }
  queue<pair<int, int> > q;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (dist[i][j] == 1) q.push({i, j});
    }
  }
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (chk(nx, ny) && dist[nx][ny] == 0) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  while (Q--) {
    long long x, y, t;
    scanf("%lld %lld %lld", &x, &y, &t);
    printf("%lld\n", dist[x - 1][y - 1]
                         ? ((mp[x - 1][y - 1] - '0') +
                            max(t - (dist[x - 1][y - 1] - 1), 0LL)) %
                               2
                         : mp[x - 1][y - 1] - '0');
  }
}
