#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5, K = 4;
int dx[K] = {1, -1, 0, 0};
int dy[K] = {0, 0, 1, -1};
int n, m, q, dist[N][N];
queue<pair<int, int>> Q;
char c[N][N];
bool is_valid(int x, int y) { return ~x && ~y && x < n && y < m; }
void read_input() {
  cin >> n >> m >> q;
  for (int i = 0; i < n; i++) cin >> c[i];
}
void solve() {
  memset(dist, 63, sizeof dist);
  for (int x = 0; x < n; x++)
    for (int y = 0; y < m; y++)
      for (int i = 0; i < K; i++) {
        int u = x + dx[i], v = y + dy[i];
        if (is_valid(u, v) && c[x][y] == c[u][v])
          dist[x][y] = 0, Q.push({x, y});
      }
  for (; !Q.empty(); Q.pop()) {
    int x = Q.front().first, y = Q.front().second;
    for (int i = 0; i < K; i++) {
      int u = x + dx[i], v = y + dy[i];
      if (is_valid(u, v) && dist[u][v] > dist[x][y] + 1)
        dist[u][v] = dist[x][y] + 1, Q.push({u, v});
    }
  }
}
void write_output() {
  while (q--) {
    int x, y;
    long long t;
    cin >> x >> y >> t;
    t -= dist[--x][--y];
    if (dist[x][y] < 1e9 && t > 0 && t & 1)
      cout << char('0' ^ '1' ^ c[x][y]) << endl;
    else
      cout << c[x][y] << endl;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  read_input(), solve(), write_output();
  return 0;
}
