#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e3 + 5;
const int MAX_L = 20;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
int n, m, t, grid[MAX_N][MAX_N], visited[MAX_N][MAX_N];
long long f[MAX_N][MAX_N];
const int dr[4] = {1, 0, -1, 0};
const int dc[4] = {0, -1, 0, 1};
bool is_good(int r, int c) {
  for (int d = 0; d < 4; d++) {
    int nr = r + dr[d], nc = c + dc[d];
    if (nr < 0 || nc < 0 || nr >= n || nc >= m) continue;
    if (grid[nr][nc] == grid[r][c]) return true;
  }
  return false;
}
void bfs() {
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (is_good(i, j)) {
        f[i][j] = 0;
        visited[i][j] = true;
        q.push({i, j});
      }
    }
  }
  while (!q.empty()) {
    int r = q.front().first, c = q.front().second;
    q.pop();
    for (int d = 0; d < 4; d++) {
      int nr = r + dr[d], nc = c + dc[d];
      if (nr < 0 || nc < 0 || nr >= n || nc >= m || visited[nr][nc]) continue;
      f[nr][nc] = f[r][c] + 1;
      visited[nr][nc] = true;
      q.push({nr, nc});
    }
  }
}
void solve() {
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      grid[i][j] = (int)(c - '0');
    }
  }
  bfs();
  while (t--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (visited[i][j])
      printf("%d\n", grid[i][j] ^ (max(0LL, p - f[i][j]) & 1));
    else
      printf("%d\n", grid[i][j]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  tc = 1;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}
