#include <bits/stdc++.h>
const int mod = (int)1e9 + 7;
using namespace std;
int n, m, k;
int x[] = {1, 0, -1, 0};
int y[] = {0, 1, 0, -1};
bool check(int i, int j) {
  if (i < 0 or j < 0 or i >= n or j >= m) return false;
  return true;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  vector<string> vc(n);
  for (auto &i : vc) cin >> i;
  int best[1001][1001];
  memset(best, -1, sizeof(best));
  queue<pair<int, int>> qu;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int p = 0; p < 4; p++) {
        if (check(i + x[p], j + y[p]) and vc[i][j] == vc[i + x[p]][j + y[p]]) {
          best[i][j] = 1;
          qu.push({i, j});
          break;
        }
      }
    }
  }
  while (!qu.empty()) {
    int xx, yy;
    tie(xx, yy) = qu.front();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      if (check(xx + x[i], yy + y[i]) and best[xx + x[i]][yy + y[i]] == -1) {
        best[xx + x[i]][yy + y[i]] = best[xx][yy] + 1;
        qu.push({xx + x[i], yy + y[i]});
      }
    }
  }
  while (k--) {
    int a, b;
    long long p;
    cin >> a >> b >> p;
    a--, b--;
    if (best[a][b] == -1) {
      cout << vc[a][b] - '0' << "\n";
    } else {
      if (best[a][b] > p) {
        cout << vc[a][b] - '0' << "\n";
      } else {
        if ((p - best[a][b]) % 2) {
          cout << vc[a][b] - '0' << "\n";
        } else {
          vc[a][b] == '1' ? cout << 0 << "\n" : cout << 1 << "\n";
        }
      }
    }
  }
  return 0;
}
