#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
const int N = 1e3 + 2;
const long long oo = 1e18 + 7;
char mx[N][N];
long long dist[N][N];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = int(0); i < int(n); ++i)
    for (int j = int(0); j < int(m); ++j) {
      cin >> mx[i][j];
      dist[i][j] = oo;
    }
  auto ok = [&](int r, int s) {
    if (r < 0 or s < 0 or r >= n or s >= m) return false;
    return true;
  };
  int src = n * m;
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      pq;
  for (int i = int(0); i < int(n); ++i) {
    for (int j = int(0); j < int(m); ++j) {
      int flag = false;
      for (int k = int(0); k < int(4); ++k) {
        int r = i + dx[k], s = j + dy[k];
        if (!ok(r, s)) continue;
        if (mx[i][j] == mx[r][s]) flag = true;
      }
      if (flag) {
        dist[i][j] = 0;
        pq.push(pair<long long, long long>(0, i * m + j));
      }
    }
  }
  while (!pq.empty()) {
    pair<long long, long long> fr = pq.top();
    pq.pop();
    int d = fr.first, node = fr.second;
    int x = node / m, y = node % m;
    if (d > dist[x][y]) continue;
    for (int i = int(0); i < int(4); ++i) {
      int r = x + dx[i], s = y + dy[i];
      if (!ok(r, s)) continue;
      if (dist[r][s] > dist[x][y] + 1) {
        dist[r][s] = dist[x][y] + 1;
        pq.push(pair<long long, long long>(dist[r][s], r * m + s));
      }
    }
  }
  while (q--) {
    long long x, y, t;
    cin >> x >> y >> t;
    x--;
    y--;
    if (t < dist[x][y])
      cout << mx[x][y] << endl;
    else {
      long long chg = (t - (long long)(dist[x][y])) % 2;
      int xd = chg;
      int ini = mx[x][y] - '0';
      cout << (ini ^ xd) << endl;
    }
  }
  return 0;
}
