#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n, m, q, x, y;
long long t;
char mp[N][N];
long long step[N][N];
int vis[N][N];
struct node {
  int x, y;
};
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
bool check(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
void bfs() {
  memset(step, 0x3f, sizeof step);
  queue<node> qu;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int cnt = 0;
      for (int k = 0; k < 4; k++) {
        int nex = i + dir[k][0], ney = j + dir[k][1];
        if (!check(nex, ney)) continue;
        if (mp[nex][ney] == mp[i][j]) cnt++;
      }
      if (cnt) step[i][j] = 0, vis[i][j] = 1, qu.push({i, j});
    }
  while (!qu.empty()) {
    node cur = qu.front();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      int nex = cur.x + dir[i][0];
      int ney = cur.y + dir[i][1];
      if (!check(nex, ney) || vis[nex][ney]) continue;
      step[nex][ney] = step[cur.x][cur.y] + 1;
      vis[nex][ney] = 1;
      qu.push({nex, ney});
    }
  }
  return;
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  bfs();
  while (q--) {
    scanf("%d%d%lld", &x, &y, &t);
    char ans;
    if (t <= step[x][y])
      ans = mp[x][y];
    else
      ans = ((mp[x][y] - '0') ^ ((t - step[x][y]) & 1)) + '0';
    printf("%c\n", ans);
  }
  return 0;
}
