#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline long long add(long long a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
  return a;
}
inline long long sub(long long a, long long b) {
  a -= b;
  if (a < 0) a += mod;
  return a;
}
inline long long mul(long long a, long long b) {
  return (long long)((long long)a * b % mod);
}
long long n, m, t;
vector<vector<long long> > adj, cost;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m >> t;
  adj.assign(n + 2, vector<long long>(m + 2, 3));
  cost.assign(n + 2, vector<long long>(m + 2, 1e18 + 100));
  for (long long i = (1); i <= (n); i++) {
    string s;
    cin >> s;
    for (long long j = (1); j <= (m); j++) {
      s[j - 1] == '1' ? adj[i][j] = 1 : adj[i][j] = 0;
    }
  }
  queue<pair<long long, long long> > q;
  for (long long i = (1); i <= (n); i++) {
    for (long long j = (1); j <= (m); j++) {
      if (adj[i][j] == adj[i + 1][j] || adj[i][j] == adj[i][j + 1] ||
          adj[i][j] == adj[i - 1][j] || adj[i][j] == adj[i][j - 1]) {
        q.push({i, j});
        cost[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    pair<long long, long long> tag = q.front();
    q.pop();
    if (cost[tag.first + 1][tag.second] > cost[tag.first][tag.second] + 1) {
      if (tag.first + 1 <= n) {
        cost[tag.first + 1][tag.second] = cost[tag.first][tag.second] + 1;
        q.push({tag.first + 1, tag.second});
      }
    }
    if (cost[tag.first][tag.second + 1] > cost[tag.first][tag.second] + 1) {
      if (tag.second + 1 <= m) {
        cost[tag.first][tag.second + 1] = cost[tag.first][tag.second] + 1;
        q.push({tag.first, tag.second + 1});
      }
    }
    if (cost[tag.first - 1][tag.second] > cost[tag.first][tag.second] + 1) {
      if (tag.first - 1 >= 1) {
        cost[tag.first - 1][tag.second] = cost[tag.first][tag.second] + 1;
        q.push({tag.first - 1, tag.second});
      }
    }
    if (cost[tag.first][tag.second - 1] > cost[tag.first][tag.second] + 1) {
      if (tag.second - 1 >= 1) {
        cost[tag.first][tag.second - 1] = cost[tag.first][tag.second] + 1;
        q.push({tag.first, tag.second - 1});
      }
    }
  }
  while (t--) {
    long long i, j, k;
    cin >> i >> j >> k;
    long long m = max((long long)0, k - cost[i][j] + 1);
    (m & 1) == 0 ? cout << adj[i][j] : cout << !adj[i][j];
    cout << "\n";
  }
  return 0;
}
