#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
int MX = 1000;
vector<vector<int>> grid(MX, vector<int>(MX));
vector<vector<int>> dist(MX, vector<int>(MX, -1));
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, -1, 0, 1};
queue<pair<int, int>> q;
void start(int n, int m) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      bool flag = false;
      for (int k = 0; k < 4; k++) {
        int ni = i + dx[k];
        int nj = j + dy[k];
        if (ni < 0 || ni >= n) continue;
        if (nj < 0 || nj >= m) continue;
        if (grid[i][j] == grid[ni][nj]) flag = true;
      }
      if (flag) {
        dist[i][j] = 0;
        q.emplace(i, j);
      }
    }
  }
  return;
}
void bfs(int n, int m) {
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int i = p.first, j = p.second;
    for (int k = 0; k < 4; k++) {
      int ni = i + dx[k];
      int nj = j + dy[k];
      if (ni < 0 || ni >= n) continue;
      if (nj < 0 || nj >= m) continue;
      if (dist[ni][nj] != -1) continue;
      dist[ni][nj] = dist[i][j] + 1;
      q.emplace(ni, nj);
    }
  }
  return;
}
int query(int i, int j, ll p) {
  if (dist[i][j] != -1)
    p -= dist[i][j];
  else
    p = -1;
  if (p < 0 || p % 2 == 0) return grid[i][j];
  if (p % 2) return 1 ^ 0 ^ grid[i][j];
}
int main() {
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      grid[i][j] = s[j] - '0';
    }
  }
  start(n, m);
  bfs(n, m);
  for (int _ = 0; _ < t; _++) {
    int i, j;
    ll p;
    cin >> i >> j >> p;
    i--;
    j--;
    int ans = query(i, j, p);
    cout << ans << '\n';
  }
}
