#include <bits/stdc++.h>
using namespace std;
long long a[100010];
char s[1010][1010];
long long dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
long long n, m, t;
bool chck(long long x, long long y) {
  return x >= 0 && y >= 0 && x < n && y < m;
}
long long vis[1010][1010];
int main() {
  while (cin >> n >> m >> t) {
    long long ans = 0;
    for (long long i = 0; i < n; ++i) {
      scanf("%s", s[i]);
    }
    queue<pair<pair<long long, long long>, long long> > q;
    for (long long i = 0; i < n; ++i) {
      for (long long j = 0; j < m; ++j) {
        for (long long k = 0; k < 4; ++k) {
          long long xx = i + dx[k];
          long long yy = j + dy[k];
          if (chck(xx, yy)) {
            if (s[xx][yy] == s[i][j]) {
              q.push(make_pair(make_pair(i, j), 1));
              break;
            }
          }
        }
      }
    }
    memset(vis, -1, sizeof vis);
    long long x, y;
    pair<pair<long long, long long>, long long> tmp;
    while (!q.empty()) {
      tmp = q.front();
      q.pop();
      x = tmp.first.first;
      y = tmp.first.second;
      if (vis[x][y] > -1) {
        continue;
      }
      vis[x][y] = tmp.second;
      for (long long k = 0; k < 4; ++k) {
        long long xx = x + dx[k];
        long long yy = y + dy[k];
        if (chck(xx, yy)) {
          if (vis[xx][yy] == -1) {
            q.push(make_pair(make_pair(xx, yy), tmp.second + 1));
          }
        }
      }
    }
    long long p, tmp1;
    for (long long i = 0; i < t; ++i) {
      scanf("%lld%lld%lld", &x, &y, &p);
      --x;
      --y;
      if (vis[x][y] == -1) {
        printf("%c\n", s[x][y]);
        continue;
      }
      tmp1 = vis[x][y];
      if (p < tmp1) {
        printf("%c\n", s[x][y]);
      } else {
        p -= tmp1;
        if (p % 2) {
          printf("%c\n", s[x][y]);
        } else {
          if (s[x][y] == '1')
            printf("0\n");
          else
            printf("1\n");
        }
      }
    }
  }
  return 0;
}
