#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int n, m, t;
char s[1010][1010];
int dep[1010][1010];
pair<int, int> stk[1000010];
int stki = 0;
int deri[] = {0, 1, 0, -1};
int derj[] = {1, 0, -1, 0};
int main() {
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dep[i][j] = -1;
      for (int k = 0; k < 4; k++) {
        int newi = i + deri[k];
        int newj = j + derj[k];
        if (newi < 0 || newi >= n || newj < 0 || newj >= m) continue;
        if (s[i][j] == s[newi][newj]) {
          dep[i][j] = 0;
        }
      }
      if (dep[i][j] == 0) {
        stk[stki++] = {i, j};
      }
    }
  }
  int st = 0;
  while (st < stki) {
    auto [pi, pj] = stk[st];
    for (int k = 0; k < 4; k++) {
      int newi = pi + deri[k];
      int newj = pj + derj[k];
      if (newi < 0 || newi >= n || newj < 0 || newj >= m) continue;
      if (dep[newi][newj] != -1) continue;
      dep[newi][newj] = dep[pi][pj] + 1;
      stk[stki++] = {newi, newj};
    }
    st++;
  }
  while (t--) {
    long long i, j, p;
    scanf("%lld %lld %lld", &i, &j, &p);
    i--;
    j--;
    if (dep[i][j] == -1) {
      printf("%c\n", s[i][j]);
      continue;
    }
    if (dep[i][j] > p - 1) {
      printf("%c\n", s[i][j]);
      continue;
    }
    printf("%lld\n", (s[i][j] - '0') ^ ((p - dep[i][j]) % 2));
  }
  return 0;
}
