#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool umax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
long long POW(long long a, long long p, long long M) {
  if (!p) return 1LL;
  long long T = POW(a, p / 2, M);
  T = T * T % M;
  if (p & 1) T = T * (a % M) % M;
  return T;
}
long long SQRT(long long a) {
  long long b = (long long)sqrtl(((double)a) + 0.5);
  while (b * b < a) ++b;
  while (b * b > a) --b;
  return (b * b == a) ? b : -1;
}
const long long MOD = 1e9 + 7;
const long long N = 1002;
long long mat[N][N];
long long dist[N][N];
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, 1, -1};
void fill(long long n, long long m) {
  deque<pair<long long, long long>> qq;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      long long cnt = 0;
      for (long long k = 0; k < 4; k++) {
        long long xx = dx[k] + i;
        long long yy = dy[k] + j;
        if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
          if (mat[xx][yy] == mat[i][j]) cnt++;
        }
      }
      if (cnt > 0) {
        dist[i][j] = 0;
        qq.push_back({i, j});
      }
    }
  while (qq.size()) {
    auto it = qq.front();
    qq.pop_front();
    long long x = it.first;
    long long y = it.second;
    for (long long i = 0; i < 4; i++) {
      long long xx = dx[i] + x;
      long long yy = dy[i] + y;
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && dist[xx][yy] == -1) {
        dist[xx][yy] = dist[x][y] + 1;
        qq.push_back({xx, yy});
      }
    }
  }
  return;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  long long n, m, t;
  cin >> n >> m >> t;
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (long long j = 0; j < m; j++) {
      mat[i][j] = s[j] - '0';
    }
  }
  memset(dist, -1, sizeof(dist));
  fill(n, m);
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    i--;
    j--;
    long long dd = dist[i][j];
    if (dd == -1) {
      cout << mat[i][j] << endl;
      continue;
    }
    if (p <= dd)
      cout << mat[i][j] << endl;
    else {
      dd = p - dd;
      if (dd % 2)
        cout << (!mat[i][j]) << endl;
      else
        cout << (mat[i][j]) << endl;
    }
  }
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
  return 0;
}
