#include <bits/stdc++.h>
using namespace std;
char table[1001][1001];
int n, m;
inline bool check(int i, int j, int r, int c) {
  if (i < 0 || j < 0 || i >= n || j >= m) return false;
  return table[i][j] == table[r][c];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t, fstChange[1001][1001], dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
  for (int i = (0); i < (1001); i++)
    for (int j = (0); j < (1001); j++) fstChange[i][j] = 1000100;
  queue<pair<int, int> > q;
  cin >> n >> m >> t;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) cin >> table[i][j];
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      for (int k = (0); k < (4); k++)
        if (check(i + dx[k], j + dy[k], i, j)) {
          q.push(make_pair(i, j));
          fstChange[i][j] = 1;
          break;
        }
    }
  while (!q.empty()) {
    int r = q.front().first, c = q.front().second;
    q.pop();
    for (int k = (0); k < (4); k++) {
      int rr = r + dx[k], cc = c + dy[k];
      if (!(rr < 0 || cc < 0 || rr >= n || cc >= m) &&
          fstChange[r][c] + 1 < fstChange[rr][cc]) {
        fstChange[rr][cc] = fstChange[r][c] + 1;
        q.push(make_pair(rr, cc));
      }
    }
  }
  for (int i = (0); i < (t); i++) {
    int r, c;
    long long ite;
    cin >> r >> c >> ite;
    r--;
    c--;
    if (fstChange[r][c] == 1000100 || ite < fstChange[r][c])
      cout << table[r][c] << '\n';
    else {
      ite -= fstChange[r][c] - 1;
      if (ite % 2 == 0)
        cout << table[r][c] << '\n';
      else
        cout << (char)(1 - (table[r][c] - '0') + '0') << '\n';
    }
  }
  return 0;
}
