#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int R, C;
string grid[maxn];
long long tempo[maxn][maxn];
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
bool dentro(int r, int c) { return (r >= 0 && r < R && c >= 0 && c < C); }
void solve() {
  int t;
  cin >> R >> C >> t;
  for (int r = 0; r < R; r++) cin >> grid[r];
  memset(tempo, 63, sizeof tempo);
  queue<pair<int, int> > fila;
  int nr, nc;
  for (int r = 0; r < R; r++) {
    for (int c = 0; c < C; c++) {
      for (int off = 0; off < 4; off++) {
        nr = r + dr[off], nc = c + dc[off];
        if (dentro(nr, nc) && grid[nr][nc] == grid[r][c]) {
          fila.push({r, c});
          tempo[r][c] = 0;
          break;
        }
      }
    }
  }
  while (!fila.empty()) {
    int r = fila.front().first;
    int c = fila.front().second;
    fila.pop();
    for (int off = 0; off < 4; off++) {
      nr = r + dr[off], nc = c + dc[off];
      if (dentro(nr, nc) && tempo[nr][nc] == inf) {
        fila.push({nr, nc});
        tempo[nr][nc] = tempo[r][c] + 1;
      }
    }
  }
  int r, c;
  long long p;
  int val;
  while (t--) {
    cin >> r >> c >> p;
    r--, c--;
    val = grid[r][c] - '0';
    if (p <= tempo[r][c])
      cout << val << endl;
    else if ((p - tempo[r][c]) & 1)
      cout << 1 - val << endl;
    else
      cout << val << endl;
  }
}
int32_t main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int tc = 1;
  while (tc--) solve();
}
