#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
struct node {
  int x, y;
  long long val;
};
int step[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char s[maxn][maxn];
int vis[maxn][maxn];
long long dis[maxn][maxn];
int main() {
  queue<node> Q;
  int n, m, t;
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      dis[i][j] = 1e18 + 1;
    }
  }
  for (int i = 0; i < n; ++i) scanf("%s", s[i]);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k < 4; ++k) {
        int x = i + step[k][0];
        int y = j + step[k][1];
        if (x >= 0 && x < n && y >= 0 && y < m && s[x][y] == s[i][j]) {
          Q.push({i, j, 0});
          vis[i][j] = 1;
          dis[i][j] = 0;
          break;
        }
      }
    }
  }
  while (!Q.empty()) {
    node p = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int x = step[i][0] + p.x;
      int y = step[i][1] + p.y;
      if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y]) {
        vis[x][y] = 1;
        dis[x][y] = p.val + 1;
        Q.push({x, y, dis[x][y]});
      }
    }
  }
  while (t--) {
    int x, y;
    long long q;
    scanf("%d%d%lld", &x, &y, &q);
    x--;
    y--;
    int val = s[x][y] - '0';
    if (dis[x][y] <= q) {
      val = ((q - dis[x][y]) % 2 + val) % 2;
      printf("%d\n", val);
    } else
      printf("%d\n", val);
  }
  return 0;
}
