#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
const int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
char s[N][N];
int d[N][N];
bool v[N][N];
int n, m, t;
inline bool valid(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m)
    return 1;
  else
    return 0;
}
int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
  memset(d, 0x3f, sizeof(d));
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      for (int k = 0; k < 4; ++k) {
        int tx = i + dir[k][0], ty = j + dir[k][1];
        if (valid(tx, ty) && s[tx][ty] == s[i][j]) {
          d[i][j] = 0;
          v[i][j] = 1;
          q.push(make_pair(i, j));
        }
      }
    }
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    v[x][y] = 0;
    for (int k = 0; k < 4; ++k) {
      int tx = x + dir[k][0], ty = y + dir[k][1];
      if (valid(tx, ty) && d[tx][ty] > d[x][y] + 1) {
        d[tx][ty] = d[x][y] + 1;
        if (!v[tx][ty]) q.push(make_pair(tx, ty)), v[tx][ty] = 1;
      }
    }
  }
  while (t--) {
    int x, y;
    long long p;
    scanf("%d%d%lld", &x, &y, &p);
    int val;
    if (d[x][y] == 0x3f3f3f3f || p <= d[x][y])
      val = s[x][y] - 48;
    else if (p > d[x][y])
      if ((p - d[x][y]) % 2)
        val = 1 - (s[x][y] - 48);
      else
        val = (s[x][y] - 48);
    printf("%d\n", val);
  }
  return 0;
}
