#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, t;
  cin >> n >> m >> t;
  vector<string> s(n);
  for (int i = 0; i < n; i++) cin >> s[i];
  queue<pair<int, int> > q;
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  vector<vector<long long int> > d(n, vector<long long int>(m, LLONG_MAX));
  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], y = j + dy[k];
        if (x >= 0 && x < n && y >= 0 && y < m && s[i][j] == s[x][y]) {
          d[i][j] = 0;
          q.push({i, j});
          break;
        }
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int x = u.first + dx[k], y = u.second + dy[k];
      if (x >= 0 && x < n && y >= 0 && y < m &&
          d[x][y] > d[u.first][u.second] + 1) {
        d[x][y] = d[u.first][u.second] + 1;
        q.push({x, y});
      }
    }
  }
  while (t--) {
    int i, j;
    long long int p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (p <= d[i][j]) {
      cout << s[i][j] << '\n';
    } else {
      long long int u = p - d[i][j];
      if (u % 2 == 0)
        cout << s[i][j] << '\n';
      else
        cout << 1 - (s[i][j] - '0') << '\n';
    }
  }
}
