#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, m, t;
int a[N][N];
long long mn[N][N];
bool good(int i, int j) {
  if (a[i - 1][j] != a[i][j] && a[i + 1][j] != a[i][j] &&
      a[i][j - 1] != a[i][j] && a[i][j + 1] != a[i][j])
    return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(a, -1, sizeof a);
  cin >> n >> m >> t;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
      mn[i][j] = 1e18;
    }
  }
  bool vis[n + 5][m + 5];
  memset(vis, 0, sizeof vis);
  queue<pair<pair<int, int>, int>> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (good(i, j)) q.push({{i, j}, 0});
    }
  }
  while (!q.empty()) {
    long long i = q.front().first.first, j = q.front().first.second,
              h = q.front().second;
    q.pop();
    if (!vis[i][j]) {
      vis[i][j] = 1;
      mn[i][j] = min(mn[i][j], h);
      if (i > 1 && !vis[i - 1][j]) q.push({{i - 1, j}, h + 1});
      if (i < n && !vis[i + 1][j]) q.push({{i + 1, j}, h + 1});
      if (j > 1 && !vis[i][j - 1]) q.push({{i, j - 1}, h + 1});
      if (j < m && !vis[i][j + 1]) q.push({{i, j + 1}, h + 1});
    }
  }
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    if (p < mn[i][j])
      cout << a[i][j] << '\n';
    else
      cout << (((p - mn[i][j]) & 1) ^ a[i][j]) << '\n';
  }
  return 0;
}
