#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long double E = 1e-8;
mt19937_64 rnd(time(0));
long long n, m, k, h, T;
long long a[1001][1001];
bool tt[1001][1001];
string s[1001];
long long binpow(long long h, long long r) {
  long long l = 1;
  while (r) {
    if (r & 1) l *= h;
    h *= h;
    r /= 2;
  }
  return l;
}
long long gcd(long long x, long long y) {
  if (x < y) swap(x, y);
  while (x && y) {
    x %= y;
    swap(x, y);
  }
  return x + y;
}
void solve() {
  long long t;
  cin >> n >> m >> t;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = ' ' + s[i];
  }
  queue<pair<pair<int, int>, int> > q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (i > 1 && s[i - 1][j] == s[i][j])
        q.push(make_pair(make_pair(i, j), 0)), tt[i][j] = 1, a[i][j] = 0;
      else if (i < n && s[i + 1][j] == s[i][j])
        q.push(make_pair(make_pair(i, j), 0)), tt[i][j] = 1, a[i][j] = 0;
      else if (j > 1 && s[i][j - 1] == s[i][j])
        q.push(make_pair(make_pair(i, j), 0)), tt[i][j] = 1, a[i][j] = 0;
      else if (j < m && s[i][j + 1] == s[i][j])
        q.push(make_pair(make_pair(i, j), 0)), tt[i][j] = 1, a[i][j] = 0;
    }
  vector<pair<int, int> > v;
  v.push_back(make_pair(-1, 0));
  v.push_back(make_pair(1, 0));
  v.push_back(make_pair(0, -1));
  v.push_back(make_pair(0, 1));
  if (q.size() == 0) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = 1e18 + 1;
  }
  while (!q.empty()) {
    long long x = q.front().first.first, y = q.front().first.second,
              k = q.front().second;
    q.pop();
    for (int i = 0; i < v.size(); i++) {
      long long xx = x + v[i].first, yy = y + v[i].second;
      if (xx < 1 || yy < 1 || xx > n || yy > m) continue;
      if (tt[xx][yy]) continue;
      a[xx][yy] = k + 1;
      tt[xx][yy] = 1;
      q.push(make_pair(make_pair(xx, yy), k + 1));
    }
  }
  while (t--) {
    long long x, y, p;
    cin >> x >> y >> p;
    if (p <= a[x][y])
      cout << s[x][y] << "\n";
    else {
      long long o = s[x][y] - 48;
      cout << (o + (p - a[x][y]) % 2) % 2 << "\n";
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
