#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, k;
char g[N][N];
long long f[N][N];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
queue<pair<int, int>> q;
bool check(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
    if (g[xx][yy] == g[x][y]) return true;
  }
  return false;
}
void bfs() {
  while (q.size()) {
    auto t = q.front();
    q.pop();
    int x = t.first, y = t.second;
    int dist = f[x][y];
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
      if (f[xx][yy] > dist + 1) {
        f[xx][yy] = dist + 1;
        q.push({xx, yy});
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> g[i][j];
      f[i][j] = 1e18;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (check(i, j)) {
        f[i][j] = 0;
        q.push({i, j});
      }
  bfs();
  for (int i = 0; i < k; i++) {
    int x, y;
    long long p;
    cin >> x >> y >> p;
    if (p <= f[x][y])
      cout << g[x][y] << endl;
    else if ((p - f[x][y]) % 2) {
      if (g[x][y] == '1')
        cout << '0' << endl;
      else
        cout << '1' << endl;
    } else
      cout << g[x][y] << endl;
  }
}
