#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
using namespace std;
template <class T>
ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}
template <class T>
istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}
template <class T>
ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}
template <class T>
istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}
void solve();
signed main() {
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}
const long long MOD = 998244353, MAXN = 2e5 + 10, INF = 1e18 + 10, BASE = 37;
void solve() {
  long long n, m, t;
  cin >> n >> m >> t;
  vector<vector<long long>> a(n + 2, vector<long long>(m + 2, 2));
  for (long long i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (long long j = 1; j <= m; ++j) {
      a[i][j] = s[j - 1] - '0';
    }
  }
  vector<vector<pair<long long, long long>>> b(
      n + 2, vector<pair<long long, long long>>(m + 2, {-1, -1}));
  vector<vector<long long>> c(n + 2, vector<long long>(m + 2));
  deque<pair<long long, long long>> q;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      if (a[i + 1][j] == a[i][j] || a[i - 1][j] == a[i][j] ||
          a[i][j + 1] == a[i][j] || a[i][j - 1] == a[i][j]) {
        c[i][j] = 1;
        b[i][j] = {i, j};
        q.push_back({i, j});
      }
    }
  }
  vector<long long> dx = {-1, 0, 1, 0};
  vector<long long> dy = {0, 1, 0, -1};
  while (!q.empty()) {
    auto it = q.front();
    q.pop_front();
    for (long long i = 0; i < 4; ++i) {
      long long nx = it.first + dx[i], ny = it.second + dy[i];
      if (a[nx][ny] == 2) continue;
      pair<long long, long long> tmp = {-1, -1};
      if (b[nx][ny] == tmp) {
        b[nx][ny] = b[it.first][it.second];
        q.push_back({nx, ny});
      }
    }
  }
  for (long long _ = 0; _ < t; ++_) {
    long long i, j, p;
    cin >> i >> j >> p;
    pair<long long, long long> tmp = {-1, -1};
    if (tmp == b[i][j]) {
      cout << a[i][j] << '\n';
    } else {
      long long t = abs(i - b[i][j].first) + abs(j - b[i][j].second);
      if (c[i][j]) {
        cout << (a[i][j] + p) % 2 << '\n';
      } else {
        if (t <= p) {
          long long nx = b[i][j].first, ny = b[i][j].second;
          cout << (a[nx][ny] + p) % 2 << '\n';
        } else {
          cout << a[i][j] << '\n';
        }
      }
    }
  }
}
