#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  vector<long long> nx = {-1, 0, 0, 1};
  vector<long long> ny = {0, -1, 1, 0};
  long long n, m, t;
  cin >> n >> m >> t;
  vector<vector<bool>> c(n, vector<bool>(m)), v(n, vector<bool>(m, false));
  vector<vector<long long>> d(n, vector<long long>(m, -1));
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      char z;
      cin >> z;
      c[i][j] = (z - '0');
    }
  }
  queue<pair<long long, long long>> q;
  for (long long x = 0; x < n; x++) {
    for (long long y = 0; y < m; y++) {
      for (long long i = 0; i < 4; i++) {
        long long nxtx = x + nx[i], nxty = y + ny[i];
        if (nxtx < 0 or nxtx > n - 1 or nxty < 0 or nxty > m - 1) continue;
        if (c[x][y] == c[nxtx][nxty]) {
          q.emplace(x, y);
          v[x][y] = true;
          d[x][y] = 0;
        }
      }
    }
  }
  while (!q.empty()) {
    long long x, y;
    tie(x, y) = q.front();
    q.pop();
    for (long long i = 0; i < 4; i++) {
      long long nxtx = x + nx[i], nxty = y + ny[i];
      if (nxtx < 0 or nxtx > n - 1 or nxty < 0 or nxty > m - 1 or v[nxtx][nxty])
        continue;
      d[nxtx][nxty] = d[x][y] + 1;
      v[nxtx][nxty] = true;
      q.emplace(nxtx, nxty);
    }
  }
  while (t--) {
    long long x, y, p;
    cin >> x >> y >> p;
    x--, y--;
    if (v[x][y]) {
      cout << (c[x][y] ^ (max(0ll, p - d[x][y]) & 1ll)) << '\n';
    } else {
      cout << c[x][y] << '\n';
    }
  }
}
