#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const long long mod = 1e9 + 7;
template <typename T>
inline T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <typename T>
inline T q_pow(T a, T x) {
  T ans = 1, tmp = a;
  while (x) {
    if (x & 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T>
inline void re(T &N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1, st, en;
int a[N][N];
int v[N][N], vis[N][N];
char s[N];
queue<pair<int, int> > q;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main() {
  re(m), re(n), re(t);
  for (int i = 1; i <= m; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++) a[i][j] = s[j] - '0';
  }
  memset(v, -1, sizeof(v));
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 0; k <= 3; k++) {
        int ex = i + dx[k], ey = j + dy[k];
        if (ex < 1 || ex > m || ey < 1 || ey > n) continue;
        if (!(a[i][j] ^ a[ex][ey])) v[i][j] = 0;
      }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      if (v[i][j] == -1) {
        int ok = 0;
        for (int k = 0; k <= 3; k++) {
          int ex = i + dx[k], ey = j + dy[k];
          if (ex < 1 || ex > m || ey < 1 || ey > n) continue;
          if (!v[ex][ey]) {
            ok = 1;
            break;
          }
        }
        if (ok) q.push(make_pair(i, j)), vis[i][j] = 1;
      }
  for (int tu = 1;; tu++) {
    if (q.empty()) break;
    int siz = q.size();
    for (int i = 1; i <= siz; i++) {
      pair<int, int> tmp = q.front();
      q.pop();
      v[tmp.first][tmp.second] = tu;
      for (int j = 0; j <= 3; j++) {
        int ex = tmp.first + dx[j], ey = tmp.second + dy[j];
        if (ex < 1 || ex > m || ey < 1 || ey > n || vis[ex][ey] ||
            v[ex][ey] != -1)
          continue;
        q.push(make_pair(ex, ey));
        vis[ex][ey] = 1;
      }
    }
  }
  for (int i = 1; i <= t; i++) {
    int x, y;
    long long ti;
    re(x);
    re(y);
    re(ti);
    if (ti <= v[x][y] || v[x][y] == -1)
      printf("%d\n", a[x][y]);
    else
      printf("%lld\n", ((ti - v[x][y]) & 1) ^ a[x][y]);
  }
  return 0;
}
