#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
string mat[1010];
bool trav[1010][1010];
ll timers[1010][1010];
bool same(ll r, ll c) {
  if (mat[r - 1][c] == mat[r][c]) return 1;
  if (mat[r + 1][c] == mat[r][c]) return 1;
  if (mat[r][c - 1] == mat[r][c]) return 1;
  return mat[r][c + 1] == mat[r][c];
}
void bfs(ll n, ll m) {
  deque<pair<ll, ll> > dq;
  for (ll i = 1; i <= n; i++)
    for (ll j = 1; j <= m; j++)
      if (same(i, j)) {
        dq.push_back(make_pair(i, j));
        trav[i][j] = 1;
        timers[i][j] = 0;
      }
  while (!dq.empty()) {
    ll r = dq.front().first, c = dq.front().second;
    dq.pop_front();
    if (r - 1 > 0 and !trav[r - 1][c]) {
      timers[r - 1][c] = timers[r][c] + 1;
      trav[r - 1][c] = 1;
      dq.push_back(make_pair(r - 1, c));
    }
    if (r + 1 <= n and !trav[r + 1][c]) {
      timers[r + 1][c] = timers[r][c] + 1;
      trav[r + 1][c] = 1;
      dq.push_back(make_pair(r + 1, c));
    }
    if (c - 1 > 0 and !trav[r][c - 1]) {
      timers[r][c - 1] = timers[r][c] + 1;
      trav[r][c - 1] = 1;
      dq.push_back(make_pair(r, c - 1));
    }
    if (c + 1 <= m and !trav[r][c + 1]) {
      timers[r][c + 1] = timers[r][c] + 1;
      trav[r][c + 1] = 1;
      dq.push_back(make_pair(r, c + 1));
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, t, r, c, ti;
  string second = "";
  cin >> n >> m >> t;
  for (ll i = 1; i <= m + 2; i++) second += "-";
  mat[0] = mat[n + 1] = second;
  for (ll i = 1; i <= n; i++) {
    cin >> second;
    mat[i] = "-" + second + "-";
  }
  bfs(n, m);
  for (ll i = 1; i <= t; i++) {
    cin >> r >> c >> ti;
    if (!trav[r][c] or ti < timers[r][c])
      cout << mat[r][c] << "\n";
    else {
      if ((ti - timers[r][c]) % 2)
        cout << (mat[r][c] == '1' ? '0' : '1') << "\n";
      else
        cout << mat[r][c] << "\n";
    }
  }
  return 0;
}
