#include <bits/stdc++.h>
using namespace std;
int mat[1003][1003];
long long dis[1003][1003];
char str[1003][1003];
int dirx[] = {0, 0, 1, -1};
int diry[] = {1, -1, 0, 0};
queue<pair<int, int> > q;
int main() {
  int n, m, t;
  scanf("%d %d %d", &n, &m, &t);
  for (int i = 0; i < n; i++) {
    scanf("%s", str[i]);
    for (int j = 0; str[i][j]; j++) mat[i + 1][j + 1] = (str[i][j] - '0');
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dis[i][j] = 1000000000000000008ll;
      for (int k = 0; k < 4; k++) {
        int x = i + dirx[k];
        int y = j + diry[k];
        if (x >= 1 && x <= n && y >= 1 && y <= m && mat[i][j] == mat[x][y]) {
          q.push({i, j});
          dis[i][j] = 0;
          break;
        }
      }
    }
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = p.first + dirx[i];
      int y = p.second + diry[i];
      if (dis[x][y] > dis[p.first][p.second] + 1)
        dis[x][y] = dis[p.first][p.second] + 1, q.push({x, y});
    }
  }
  while (t--) {
    int x, y;
    long long p;
    scanf("%d %d %lld", &x, &y, &p);
    if (dis[x][y] < p) {
      if ((p - dis[x][y]) & 1ll)
        printf("%d\n", (str[x - 1][y - 1] - '0') ^ 1);
      else
        printf("%c\n", str[x - 1][y - 1]);
    } else
      printf("%c\n", str[x - 1][y - 1]);
  }
  return 0;
}
