#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 15;
char a[1050][1050];
int siz[maxn];
int pre[1050][1050];
int tot;
int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int n, m;
void dfs(int x, int y, int flag) {
  for (int i = 0; 4 > i; i++) {
    int xx = x + dir[i][0], yy = y + dir[i][1];
    if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[xx][yy] - '0' == flag &&
        !pre[xx][yy]) {
      siz[tot]++;
      pre[xx][yy] = tot;
      dfs(xx, yy, flag);
    }
  }
}
int ans[1050][1050];
int main() {
  int t;
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 1; n >= i; i++) {
    scanf("%s", a[i] + 1);
  }
  for (int i = 1; n >= i; i++) {
    for (int j = 1; m >= j; j++) {
      if (!pre[i][j]) {
        tot++;
        siz[tot]++;
        pre[i][j] = tot;
        dfs(i, j, a[i][j] - '0');
      }
    }
  }
  queue<pair<int, int> > q;
  for (int i = 1; n >= i; i++) {
    for (int j = 1; m >= j; j++) {
      if (siz[pre[i][j]] > 1) {
        q.push(pair<int, int>(i, j));
        ans[i][j] = 1;
      }
    }
  }
  while (q.size()) {
    pair<int, int> x = q.front();
    q.pop();
    for (int i = 0; 4 > i; i++) {
      int xx = x.first + dir[i][0], yy = x.second + dir[i][1];
      if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && !ans[xx][yy]) {
        pre[xx][yy] = 1;
        ans[xx][yy] = ans[x.first][x.second] + 1;
        q.push(pair<int, int>(xx, yy));
      }
    }
  }
  for (int i = 1; t >= i; i++) {
    long long x, y, k;
    scanf("%lld%lld%lld", &x, &y, &k);
    if (ans[x][y] > k || (k - ans[x][y]) % 2 == 1 || ans[x][y] == 0)
      printf("%d\n", a[x][y] - '0');
    else
      printf("%d\n", (a[x][y] - '0') ^ 1);
  }
  return 0;
}
