#include <bits/stdc++.h>
using namespace std;
int t, n, m, i, j, k, A[1010][1010], chk[1010][1010], dx[4] = {-1, 0, 1, 0},
                                                      dy[4] = {0, 1, 0, -1};
char S[1010];
queue<pair<int, int> > q;
int main() {
  for (scanf("%d %d %d", &n, &m, &t); i++ < n;) {
    scanf("%s", &S);
    for (j = 0; j++ < m;) A[i][j] = S[j - 1] - '0';
  }
  for (i = 0; i++ < n;) {
    for (j = 0; j++ < m;) {
      bool flag = false;
      for (k = 0; k < 4; k++) {
        int a = dx[k] + i;
        int b = dy[k] + j;
        if (a > 0 && a <= n && b > 0 && b <= m && A[a][b] == A[i][j]) {
          flag = true;
          break;
        }
      }
      if (flag) {
        q.push({i, j});
      } else {
        chk[i][j] = -1;
      }
    }
  }
  while (q.size()) {
    int a = q.front().first;
    int b = q.front().second;
    q.pop();
    for (k = 0; k < 4; k++) {
      int x = a + dx[k];
      int y = b + dy[k];
      if (chk[x][y] == -1) {
        chk[x][y] = chk[a][b] + 1;
        q.push({x, y});
      }
    }
  }
  while (t--) {
    long long int k;
    scanf("%d %d %lld", &i, &j, &k);
    if (chk[i][j] == -1 || chk[i][j] >= k)
      printf("%d\n", A[i][j]);
    else {
      printf("%lld\n", (k - chk[i][j] + A[i][j]) % 2);
    }
  }
}
