#include <bits/stdc++.h>
using namespace std;
const int MX = 1010;
const int MN = 1e7;
char s[MX][MX];
int n, m, t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int dp[MX][MX];
int ok(int x, int y) { return (0 <= x && x < n && 0 <= y && y < m); }
int main() {
  cin.sync_with_stdio(0);
  cin >> n >> m >> t;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> s[i][j];
      dp[i][j] = MN;
    }
  }
  queue<pair<int, int>> q;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k < 4; ++k) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (ok(x, y) && s[x][y] == s[i][j]) {
          dp[i][j] = 0;
          q.push(make_pair(i, j));
        }
      }
    }
  }
  while (q.size()) {
    auto fr = q.front();
    q.pop();
    int x = fr.first;
    int y = fr.second;
    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (!ok(nx, ny)) continue;
      if (dp[nx][ny] > dp[x][y] + 1) {
        dp[nx][ny] = dp[x][y] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  while (t--) {
    int i, j;
    long long int k;
    cin >> i >> j >> k;
    --i, --j;
    long long int v = dp[i][j];
    if (v == MN) {
      cout << s[i][j] << "\n";
      continue;
    }
    if (v >= k) {
      cout << s[i][j] << "\n";
    } else {
      k -= v;
      k %= 2;
      int ans = s[i][j] - '0';
      cout << (ans ^ k) << "\n";
    }
  }
  return 0;
}
