#include <bits/stdc++.h>
using namespace std;
const short int N = 1000;
short int n, m, ax[] = {0, 0, 1, -1}, ay[] = {1, -1, 0, 0}, man_dis[N][N];
bool vis[N][N], v[N][N], g[N][N];
void good() {
  for (short int i = 0; i < n; i++) {
    for (short int j = 0; j < m; j++) {
      bool f = 0;
      if (i and v[i][j] == v[i - 1][j]) f = 1;
      if (j and v[i][j] == v[i][j - 1]) f = 1;
      if (i < n - 1 and v[i][j] == v[i + 1][j]) f = 1;
      if (j < m - 1 and v[i][j] == v[i][j + 1]) f = 1;
      g[i][j] = f;
    }
  }
}
void man_dist() {
  queue<pair<short int, short int>> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (g[i][j]) man_dis[i][j] = 0, q.push({i, j}), vis[i][j] = true;
  while (q.size()) {
    short int y = q.front().first, x = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + ax[i], ny = y + ay[i];
      if (nx < 0 || ny < 0 || nx >= m || ny >= n || vis[ny][nx]) continue;
      man_dis[ny][nx] = man_dis[y][x] + 1;
      vis[ny][nx] = true;
      q.push({ny, nx});
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt, ii = 0;
  short int k = 0;
  cin >> n >> m >> tt;
  for (short int i = 0; i < n; i++)
    for (short int j = 0; j < m; j++) {
      char x;
      cin >> x;
      v[i][j] = (x == '1');
    }
  good();
  man_dist();
  while (ii++ < tt) {
    short int i, j;
    long long p;
    cin >> i >> j >> p;
    i--, j--;
    if (n + m == 2) {
      cout << v[i][j] << endl;
      continue;
    }
    short int f = man_dis[i][j];
    if (p <= f || !vis[i][j])
      cout << v[i][j] << endl;
    else
      cout << (p - f + v[i][j]) % 2 << endl;
  }
  cerr << "[Execution : " << (1.0 * clock()) / CLOCKS_PER_SEC << "s]\n";
  return 0;
}
