#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
using Pa = pair<T, U>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M, T;
  cin >> N >> M >> T;
  vvec<int> A(N, vec<int>(M));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      char c;
      cin >> c;
      int n = c - '0';
      A[i][j] = n;
    }
  auto in = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < M; };
  vec<int> dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
  ll inf = 2e18;
  vvec<ll> change(N, vec<ll>(M, inf));
  queue<Pa<int, int>> Q;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      bool ok = false;
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (!in(nx, ny)) continue;
        ok |= (A[i][j] == A[nx][ny]);
      }
      if (ok) {
        change[i][j] = 1;
        Q.push({i, j});
      }
    }
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (!in(nx, ny)) continue;
      if (change[nx][ny] != inf) continue;
      change[nx][ny] = change[x][y] + 1;
      Q.push({nx, ny});
    }
  }
  for (int t = 0; t < T; t++) {
    int x, y;
    ll p;
    cin >> x >> y >> p;
    x--;
    y--;
    if (change[x][y] > p)
      cout << A[x][y] << "\n";
    else {
      ll d = p - change[x][y];
      if (d % 2 == 0)
        cout << (1 - A[x][y]) << "\n";
      else
        cout << A[x][y] << "\n";
    }
  }
}
