#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e18;
int n, m, k;
char s[1005][1005];
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
long long dis[1005][1005];
queue<pair<int, int>> q;
bool valid(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= m); }
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf(" %s", s[i] + 1);
  for (int x = 1; x <= n; x++) {
    for (int y = 1; y <= m; y++) {
      dis[x][y] = inf;
      for (int i = 0; i < 4; i++) {
        int xx = x + dx[i], yy = y + dy[i];
        if (valid(xx, yy) && s[x][y] == s[xx][yy]) {
          dis[x][y] = 1;
          q.push({x, y});
        }
      }
    }
  }
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (valid(xx, yy) && dis[xx][yy] == inf) {
        dis[xx][yy] = dis[x][y] + 1;
        q.push({xx, yy});
      }
    }
  }
  while (k--) {
    int x, y;
    long long t;
    scanf("%d%d%lld", &x, &y, &t);
    if (t < dis[x][y])
      printf("%c\n", s[x][y]);
    else {
      int res = s[x][y] - '0';
      t = t - dis[x][y] + 1;
      if (t % 2 != 0) res = !res;
      printf("%d\n", res);
    }
  }
}
