#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long mod = (119 << 23) + 1;
const long long M = 1e9 + 7;
const long long N = 1e5 + 5;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(15);
  long long n, m, t;
  cin >> n >> m >> t;
  long long g[n][m];
  for (long long i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (long long j = 0; j < m; ++j) g[i][j] = s[j] == '1';
  }
  long long d[n][m];
  memset(d, INF, sizeof(d));
  queue<pair<long long, long long> > q;
  for (long long i = 0; i < n; ++i)
    for (long long j = 0; j < m; ++j) {
      bool b = false;
      if (i != 0) b |= g[i][j] == g[i - 1][j];
      if (i != n - 1) b |= g[i][j] == g[i + 1][j];
      if (j != 0) b |= g[i][j] == g[i][j - 1];
      if (j != m - 1) b |= g[i][j] == g[i][j + 1];
      if (b) d[i][j] = 0, q.push({i, j});
    }
  while (!q.empty()) {
    pair<long long, long long> p = q.front();
    long long x = p.first, y = p.second;
    q.pop();
    long long alt = d[x][y] + 1;
    if (x != 0 && alt < d[x - 1][y]) d[x - 1][y] = alt, q.push({x - 1, y});
    if (x != n - 1 && alt < d[x + 1][y]) d[x + 1][y] = alt, q.push({x + 1, y});
    if (y != 0 && alt < d[x][y - 1]) d[x][y - 1] = alt, q.push({x, y - 1});
    if (y != m - 1 && alt < d[x][y + 1]) d[x][y + 1] = alt, q.push({x, y + 1});
  }
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    --i, --j;
    p -= d[i][j];
    if (p >= 0)
      cout << (p & 1 ? !g[i][j] : g[i][j]) << '\n';
    else
      cout << g[i][j] << '\n';
  }
}
