#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, t;
  cin >> n >> m >> t;
  vector<string> g(n);
  for (int i = 0; i < n; i++) cin >> g[i];
  vector<int> dx = {1, -1, 0, 0};
  vector<int> dy = {0, 0, 1, -1};
  auto good = [&](int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; };
  queue<array<int, 2>> q;
  constexpr long long inf = 1e18 + 14;
  vector<vector<long long>> cost(n, vector<long long>(m, inf));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int ok = 0;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (!good(x, y)) continue;
        if (g[x][y] == g[i][j]) {
          ok |= 1;
          break;
        }
      }
      if (ok) q.push({i, j}), cost[i][j] = 0;
    }
  }
  while (q.size()) {
    auto [i, j] = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int x = i + dx[k], y = j + dy[k];
      if (!good(x, y)) continue;
      if (cost[x][y] > cost[i][j] + 1)
        cost[x][y] = cost[i][j] + 1, q.push({x, y});
    }
  }
  while (t--) {
    int x, y;
    long long p;
    cin >> x >> y >> p;
    --x, --y;
    if (cost[x][y] > p) {
      cout << g[x][y] << '\n';
    } else {
      int init = g[x][y] - '0';
      int diff = (p - cost[x][y]) & 1;
      cout << (init ^ diff) << '\n';
    }
  }
}
