#include <bits/stdc++.h>
using namespace std;
int n, m, q;
const int N = 1010;
int a[N][N];
int dp[N][N];
bool ok(int i, int j) { return 0 <= i and i < n and 0 <= j and j < m; }
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m >> q;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
      dp[i][j] = -1;
    }
  }
  queue<pair<int, int>> qq;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      bool ok2 = false;
      for (int it = 0; it < 4; ++it) {
        int ni = i + dx[it], nj = j + dy[it];
        if (ok(ni, nj) and a[i][j] == a[ni][nj]) {
          ok2 = true;
        }
      }
      if (ok2) {
        dp[i][j] = 0;
        qq.emplace(i, j);
      }
    }
  }
  while (!qq.empty()) {
    auto [i, j] = qq.front();
    qq.pop();
    for (int it = 0; it < 4; ++it) {
      int ni = i + dx[it], nj = j + dy[it];
      if (ok(ni, nj) and dp[ni][nj] == -1) {
        dp[ni][nj] = dp[i][j] + 1;
        qq.emplace(ni, nj);
      }
    }
  }
  while (q--) {
    int i, j;
    long long t;
    cin >> i >> j >> t;
    --i, --j;
    if (dp[i][j] <= t and dp[i][j] != -1) {
      cout << (a[i][j] ^ ((t - dp[i][j]) % 2));
    } else {
      cout << a[i][j];
    }
    cout << '\n';
  }
  return 0;
}
