#include <bits/stdc++.h>
using namespace std;
const int maxn = 1050;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
long long t, n, m;
long long arr[maxn][maxn];
char s[maxn][maxn];
struct node {
  int x;
  int y;
};
queue<node> que;
int main() {
  scanf("%lld%lld%lld", &n, &m, &t);
  long long tot = n * m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
  }
  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      arr[i][j] = tot;
      for (int k = 3; k >= 0; k--) {
        int xx = i + dx[k];
        int yy = j + dy[k];
        if (s[xx][yy] == s[i][j]) {
          arr[i][j] = 0;
          que.push(node{i, j});
          break;
        }
      }
    }
  }
  while (!que.empty()) {
    node zz = que.front();
    que.pop();
    int x, y;
    x = zz.x;
    y = zz.y;
    for (int i = 3; i >= 0; i--) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      int s1 = s[xx][yy] - '0';
      int s2 = s[x][y] - '0';
      if (abs(s1 - s2) == 1 && arr[xx][yy] - 1 > arr[x][y]) {
        arr[xx][yy] = arr[x][y];
        arr[xx][yy]++;
        que.push(node{xx, yy});
      }
    }
  }
  for (int cas = 1; cas <= t; cas++) {
    long long a, b, fin;
    scanf("%lld%lld%lld", &a, &b, &fin);
    fin = max(fin - (arr[a][b] < tot ? 1ll * arr[a][b] : fin), 0ll);
    fin = fin & 1ll;
    fin = (s[a][b] - '0') ^ fin;
    printf("%lld\n", fin);
  }
  return 0;
}
