#include <bits/stdc++.h>
using namespace std;
namespace IO {
char buf[1 << 21], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline int read() {
  int x = 0, sign = 0;
  char s =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
           ? EOF
           : *p1++);
  while (!isdigit(s))
    sign |= s == '-',
        s = (p1 == p2 &&
                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
                 ? EOF
                 : *p1++);
  while (isdigit(s))
    x = (x << 1) + (x << 3) + (s - '0'),
    s = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
             ? EOF
             : *p1++);
  return sign ? -x : x;
}
void print(int x) {
  if (x > 9) print(x / 10);
  (*O++ = x % 10 + '0');
}
}  // namespace IO
namespace math {
const int mod = 99824353;
const int maxn = 1e6 + 5;
long long ksm(long long a, long long b) {
  long long s = 1, m = a;
  while (b) {
    if (b & 1) s = s * m % mod;
    m = m * m % mod, b >>= 1;
  }
  return s;
}
long long inv(long long x) { return ksm(x, mod - 2); }
long long fc[maxn], ifc[maxn];
void init_m(int n) {
  fc[0] = 1;
  for (int i = 1; i <= n; i++) fc[i] = fc[i - 1] * i % mod;
  ifc[n] = inv(fc[n]);
  for (int i = n - 1; i >= 0; i--) ifc[i] = ifc[i + 1] * (i + 1) % mod;
}
long long C(long long n, long long m) {
  return fc[n] * ifc[m] % mod * ifc[n - m] % mod;
}
}  // namespace math
using namespace IO;
const int N = 1e3 + 5;
const int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
int n, m, t, cnum, col[N][N], cov[N * N], ori[N * N];
char c[N][N];
vector<int> e[N * N];
long long dis[N * N];
int dfs(int x, int y, char z) {
  col[x][y] = cnum;
  int sz = 1;
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (xx && yy && xx <= n && yy <= m && !col[xx][yy] && c[xx][yy] == z)
      sz += dfs(xx, yy, z);
  }
  return sz;
}
void solve() {
  cin >> n >> m >> t;
  queue<int> q;
  memset(dis, 0x7f, sizeof(dis));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> c[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!col[i][j]) {
        ori[++cnum] = c[i][j] - '0';
        int tmp = dfs(i, j, c[i][j]);
        if (tmp > 1) q.push(cnum), cov[cnum] = cnum, dis[cnum] = 0;
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 0; k < 4; k++) {
        int ii = i + dx[k], jj = j + dy[k];
        if (ii && jj && ii <= n && jj <= m && col[i][j] != col[ii][jj]) {
          e[col[i][j]].emplace_back(col[ii][jj]);
          e[col[ii][jj]].emplace_back(col[i][j]);
        }
      }
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int it : e[t])
      if (!cov[it]) cov[it] = cov[t], dis[it] = dis[t] + 1, q.push(it);
  }
  while (t--) {
    long long x, y, m;
    cin >> x >> y >> m;
    int c = col[x][y];
    if (dis[c] >= m)
      cout << ori[c] << endl;
    else
      cout << ((m & 1) ^ ori[cov[c]]) << endl;
  }
}
int main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
