#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = N * N;
int n, m, Q, T;
int fd[M], sz[M], d[M], vi[M];
int dx[4] = {-1, +0, +1, +0};
int dy[4] = {+0, -1, +0, +1};
char s[N][N];
queue<int> q;
int fina(int x) { return x == fd[x] ? x : fd[x] = fina(fd[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &Q);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  T = n * m;
  for (int i = 1; i <= T; i++) fd[i] = i, sz[i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (!x || !y || x > n || y > m) continue;
        if (s[i][j] != s[x][y]) continue;
        int v = ((x - 1) * m + y), u = ((i - 1) * m + j);
        u = fina(u);
        v = fina(v);
        if (u != v) {
          sz[u] += sz[v];
          fd[v] = u;
        }
      }
    }
  for (int i = 1; i <= T; i++)
    if (sz[fina(i)] > 1) q.push(i), vi[i] = 1;
  while (q.size()) {
    int u = q.front();
    q.pop();
    int i = (u - 1) / m + 1, j = (u - 1) % m + 1;
    for (int k = 0; k <= 3; k++) {
      int x = i + dx[k], y = j + dy[k];
      if (x < 1 || y < 1 || x > n || y > m) continue;
      int v = ((x - 1) * m + y);
      if (!vi[v]) vi[v] = 1, d[v] = d[u] + 1, q.push(v);
    }
  }
  while (Q--) {
    int x, y;
    long long p;
    scanf("%d%d%lld", &x, &y, &p);
    int u = ((x - 1) * m + y);
    long long t = vi[u] ? min(p, 1ll * d[u]) : p;
    cout << ((s[x][y] ^ p ^ t) & 1) << '\n';
  }
}
