#include <bits/stdc++.h>
using namespace std;
const long long inff = 0x3f3f3f3f3f3f3f3f;
const int N = 1e3 + 8;
int n, m, t, x, y, vis[N][N], dis[N][N], dx[4] = {0, 0, -1, 1},
                                         dy[4] = {1, -1, 0, 0};
long long z;
string a[N];
struct node {
  int x, y;
};
bool as(int x, int y, char z) {
  if (x < 1 || x > n || y < 0 || y >= m || a[x][y] != z) return 0;
  return 1;
}
bool as(int x, int y) {
  if (x < 1 || x > n || y < 0 || y >= m) return 0;
  return 1;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> t;
  for (int i(1); i <= (n); ++i) cin >> a[i];
  queue<node> q;
  for (int i(1); i <= (n); ++i) {
    for (int j(0); j <= (m - 1); ++j) {
      if (as(i - 1, j, a[i][j]) || as(i + 1, j, a[i][j]) ||
          as(i, j - 1, a[i][j]) || as(i, j + 1, a[i][j]))
        vis[i][j] = 1, q.push(node{i, j});
    }
  }
  while (!q.empty()) {
    node qw = q.front();
    q.pop();
    for (int i(0); i <= (3); ++i) {
      int x = qw.x + dx[i], y = qw.y + dy[i];
      if (!as(x, y) || vis[x][y]) continue;
      dis[x][y] = dis[qw.x][qw.y] + 1;
      vis[x][y] = 1, q.push(node{x, y});
    }
  }
  while (t--) {
    scanf("%d", &x), scanf("%d", &y), scanf("%lld", &z);
    y--;
    int qw = a[x][y] - '0';
    if (z < dis[x][y] || !vis[x][y]) {
      printf("%d\n", qw);
      continue;
    }
    z -= dis[x][y];
    if (z & 1) qw ^= 1;
    printf("%d\n", qw);
  }
  return 0;
}
