#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-12;
const double PI = acos(-1.0);
const double angcst = PI / 180.0;
const long long mod = 998244353;
long long max_3(long long a, long long b, long long c) {
  if (a > b && a > c) return a;
  if (b > c) return b;
  return c;
}
long long min_3(long long a, long long b, long long c) {
  if (a < b && a < c) return a;
  if (b < c) return b;
  return c;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long qmul(long long a, long long b) {
  long long r = 0;
  while (b) {
    if (b & 1) r = (r + a) % mod;
    b >>= 1;
    a = (a + a) % mod;
  }
  return r;
}
long long qpow(long long a, long long n) {
  long long r = 1;
  while (n) {
    if (n & 1) r = (r * a) % mod;
    n >>= 1;
    a = (a * a) % mod;
  }
  return r;
}
long long qpow(long long a, long long n, long long p) {
  long long r = 1;
  while (n) {
    if (n & 1) r = (r * a) % p;
    n >>= 1;
    a = (a * a) % p;
  }
  return r;
}
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int n, m, t;
char mp[1050][1050];
int dis[1050][1050];
inline bool prim(int x, int y) { return x > 0 && y > 0 && x <= n && y <= m; }
void solve() {
  memset(dis, INF, sizeof(dis));
  cin >> n >> m >> t;
  for (int i = (1); i <= (n); i++) cin >> mp[i] + 1;
  queue<pair<int, int> > q;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      if (prim(i - 1, j) && mp[i][j] == mp[i - 1][j] ||
          prim(i + 1, j) && mp[i][j] == mp[i + 1][j] ||
          prim(i, j - 1) && mp[i][j] == mp[i][j - 1] ||
          prim(i, j + 1) && mp[i][j] == mp[i][j + 1]) {
        dis[i][j] = 0;
        q.push(pair<int, int>(i, j));
      }
    }
  bool flag = false;
  if (q.empty()) flag = true;
  while (!q.empty()) {
    pair<int, int> pd = q.front();
    q.pop();
    for (int i = (0); i <= (3); i++) {
      int px = pd.first + dx[i], py = pd.second + dy[i];
      if (prim(px, py) && dis[px][py] > dis[pd.first][pd.second] + 1) {
        dis[px][py] = dis[pd.first][pd.second] + 1;
        q.push(pair<int, int>(px, py));
      }
    }
  }
  while (t--) {
    int x, y;
    long long d;
    cin >> x >> y >> d;
    if (flag) {
      cout << mp[x][y] << '\n';
      continue;
    }
    if (d <= dis[x][y] || (d - dis[x][y]) % 2 == 0)
      cout << mp[x][y] << '\n';
    else
      cout << 1 - (mp[x][y] - '0') << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
