#include <bits/stdc++.h>
using namespace std;
char fi[1010][1010];
long long d[1010][1010];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m, qq;
  cin >> n >> m >> qq;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> fi[i][j];
      d[i][j] = 2e18;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (j != m - 1 && fi[i][j] == fi[i][j + 1]) {
        d[i][j] = d[i][j + 1] = 0;
      }
      if (i != n - 1 && fi[i][j] == fi[i + 1][j]) {
        d[i][j] = d[i + 1][j] = 0;
      }
    }
  }
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!d[i][j]) {
        q.push(make_pair(i, j));
      }
    }
  }
  while (q.size()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        if (d[nx][ny] > d[x][y] + 1) {
          d[nx][ny] = d[x][y] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  while (qq--) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    long long p;
    cin >> p;
    int z = fi[i][j] - '0';
    if (d[i][j] < p) {
      if ((p - d[i][j]) % 2) z ^= 1;
    }
    printf("%d\n", z);
  }
}
