#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1e9 + 7;
int main() {
  long long n, m, t;
  long long dx[4] = {-1, 0, 1, 0};
  long long dy[4] = {0, -1, 0, 1};
  cin >> n >> m >> t;
  vector<vector<char>> s(n, vector<char>(m));
  for (long long i = 0; i < (n); i++) {
    for (long long j = 0; j < (m); j++) {
      cin >> s[i][j];
    }
  }
  vector<vector<bool>> used(n, vector<bool>(m));
  vector<vector<bool>> used2(n, vector<bool>(m));
  vector<vector<long long>> cnt(n, vector<long long>(m));
  long long allcnt = 0;
  queue<pair<long long, long long>> que;
  bool ok1 = false;
  for (long long i = 0; i < (n); i++) {
    for (long long j = 0; j < (m); j++) {
      bool ok = false;
      for (long long k = 0; k < (4); k++) {
        long long nx = i + dx[k];
        long long ny = j + dy[k];
        if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= m - 1) {
          if (s[nx][ny] == s[i][j]) {
            ok = true;
          }
        }
      }
      if (ok) {
        que.push(make_pair(i, j));
        used2[i][j] = true;
        ok1 = true;
      }
    }
  }
  if (!ok1) {
    while (t--) {
      long long i, j, p;
      cin >> i >> j >> p;
      i--;
      j--;
      cout << s[i][j] << endl;
    }
    return 0;
  }
  long long times = 0;
  while (true) {
    long long val = que.size();
    allcnt += val;
    for (long long i = 0; i < (val); i++) {
      pair<long long, long long> x = que.front();
      que.pop();
      if (used[x.first][x.second]) continue;
      cnt[x.first][x.second] = times;
      used[x.first][x.second] = true;
      for (long long j = 0; j < (4); j++) {
        long long nx = x.first + dx[j];
        long long ny = x.second + dy[j];
        if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= m - 1 &&
            used[nx][ny] == false && used2[nx][ny] == false) {
          que.push(make_pair(nx, ny));
          used2[nx][ny] = true;
        }
      }
    }
    times++;
    if (que.empty()) break;
  }
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (p < cnt[i][j]) {
      cout << s[i][j] << endl;
    } else {
      if (s[i][j] == '0') {
        cout << (p - cnt[i][j]) % 2 << endl;
      } else {
        cout << ((p - cnt[i][j]) % 2 + 1) % 2 << endl;
      }
    }
  }
  return 0;
}
