#include <bits/stdc++.h>
static int solve();
using ll = long long;
int main(int argc, char* argv[]) {
  int tc = 1;
  while (tc--) solve();
  return 0;
}
const int N = 1024;
static int solve() {
  int n, m, Q;
  scanf("%d %d %d", &n, &m, &Q);
  std::vector<std::array<int, 1024>> v(1024);
  char s[1024];
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    for (int j = 1; j <= m; ++j) v[i][j] = (s[j - 1] - '0');
  }
  for (int i = 0; i < 1024; ++i) {
    v[i][0] = v[i][m + 1] = 2;
  }
  for (int j = 0; j < 1024; ++j) {
    v[0][j] = v[n + 1][j] = 2;
  }
  std::vector<std::array<bool, 1024>> visit(1024);
  std::vector<std::array<int, 1024>> dist(1024);
  std::vector<int> que;
  que.reserve(1024 * 1024);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      bool has = v[i][j] == v[i - 1][j] || v[i][j] == v[i + 1][j] ||
                 v[i][j] == v[i][j - 1] || v[i][j] == v[i][j + 1];
      if (has) {
        visit[i][j] = true;
        que.push_back((i << 16) | j);
        dist[i][j] = 0;
      }
    }
  }
  int const dx[4] = {1, 0, -1, 0};
  int const dy[4] = {0, 1, 0, -1};
  for (int i = 0; i < (int)que.size(); ++i) {
    int x = que[i] >> 16;
    int y = que[i] & 65535;
    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
      if (visit[nx][ny]) continue;
      visit[nx][ny] = true;
      dist[nx][ny] = 1 + dist[x][y];
      que.push_back((nx << 16) | ny);
    }
  }
  while (Q--) {
    int x, y;
    long long p;
    scanf("%d %d %lld", &x, &y, &p);
    int ans =
        !visit[x][y] ? v[x][y] : v[x][y] ^ (std::max(0LL, p - dist[x][y]) & 1);
    printf("%d\n", ans);
  }
  return 0;
}
