#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long double EPS = 1e-9;
long long binpow(long long b, long long p, long long mod) {
  long long ans = 1;
  b %= mod;
  for (; p; p >>= 1) {
    if (p & 1) ans = ans * b % mod;
    b = b * b % mod;
  }
  return ans;
}
void pre() {}
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
long long n, m, tt;
int vis[1010][1010];
long long dist[1010][1010];
long long arr[1010][1010];
bool inside(long long x, long long y) {
  return x >= 0 && x < n && y >= 0 && y < m;
}
using state = pair<long long, pair<long long, long long> >;
deque<state> pq;
void DIJ() {
  while (!pq.empty()) {
    state it = pq.front();
    pq.pop_front();
    long long dis = it.first;
    long long x = it.second.first, y = it.second.second;
    if (vis[x][y]) continue;
    vis[x][y] = 1;
    for (long long k = 0; k < (4); ++k) {
      long long xx = x + dx[k];
      long long yy = y + dy[k];
      if (!inside(xx, yy)) continue;
      if (arr[x][y] == arr[xx][yy] && dist[xx][yy] > dis) {
        dist[xx][yy] = dis;
        pq.push_front(make_pair(dis, make_pair(xx, yy)));
      } else if (arr[x][y] == (arr[xx][yy] ^ 1) && dist[xx][yy] > dis + 1) {
        dist[xx][yy] = dis + 1;
        pq.push_back(make_pair((dis + 1), make_pair(xx, yy)));
      }
    }
  }
}
void solve() {
  cin >> n >> m >> tt;
  string s;
  for (long long i = 0; i < (n); ++i) {
    cin >> s;
    for (long long j = 0; j < (m); ++j) {
      arr[i][j] = s[j] - '0';
    }
  }
  for (long long i = 0; i < (n); ++i)
    for (long long j = 0; j < (m); ++j) {
      dist[i][j] = MOD * MOD;
      for (long long k = 0; k < (4); ++k) {
        if (inside(i + dx[k], j + dy[k])) {
          if (arr[i][j] == arr[i + dx[k]][j + dy[k]]) {
            dist[i][j] = 0;
            pq.push_front(make_pair(0, make_pair(i, j)));
          }
        }
      }
    }
  DIJ();
  for (long long _ = 0; _ < (tt); ++_) {
    long long i, j, p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (p > dist[i][j]) {
      long long mm = (p - dist[i][j]) & 1;
      cout << (arr[i][j] ^ mm) << '\n';
    } else {
      cout << arr[i][j] << '\n';
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  pre();
  long long _t = 1;
  for (long long i = 1; i <= _t; i++) {
    solve();
  }
}
