#include <bits/stdc++.h>
using namespace std;
const int maxn = 1003;
const long long mod = 1e9 + 7;
char a[maxn][maxn];
int dp[maxn][maxn][2], mf[4] = {1, -1, 0, 0}, mc[4] = {0, 0, 1, -1};
struct tres {
  int f, c, id;
};
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 1; i <= n; i++) cin >> (a[i] + 1);
  queue<tres> q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dp[i][j][a[i][j] - '0'] = 0;
      dp[i][j][1 - (a[i][j] - '0')] = 1 << 30;
      q.push({i, j, a[i][j] - '0'});
    }
  int f, c, id;
  while (!q.empty()) {
    f = q.front().f;
    c = q.front().c;
    id = q.front().id;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nf = f + mf[i], nc = c + mc[i];
      if (nf < 1 || nf > n || nc < 1 || nc > m) continue;
      if (a[nf][nc] - '0' != id) continue;
      if (dp[nf][nc][1 - id] > dp[f][c][id] + 1) {
        dp[nf][nc][1 - id] = dp[f][c][id] + 1;
        q.push({nf, nc, 1 - id});
      }
    }
  }
  long long p;
  while (t--) {
    cin >> f >> c >> p;
    id = 1 - (a[f][c] - '0');
    if (dp[f][c][id] == 1 << 30) {
      cout << 1 - id << "\n";
      continue;
    }
    if (p < dp[f][c][id]) {
      cout << 1 - id << "\n";
      continue;
    }
    p -= dp[f][c][id];
    if (p % 2 == 0)
      cout << id << "\n";
    else
      cout << 1 - id << "\n";
  }
  return 0;
}
