#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T>
void chmax(T &a, T b) {
  a = max(a, b);
}
template <typename T>
void chmin(T &a, T b) {
  a = min(a, b);
}
template <typename T>
void chadd(T &a, T b) {
  a = a + b;
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
int main() {
  int H, W, T;
  scanf("%d%d%d", &H, &W, &T);
  vector<vector<bool> > vs(H, vector<bool>(W));
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (size_t j = 0; j < W; j++) vs[i][j] = (s[j] == '1' ? true : false);
  }
  queue<pair<int, int> > que;
  vector<vector<ll> > first_time(H, vector<ll>(W, LONGINF));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      bool same_color = false;
      for (int k = 0; k < 4; k++) {
        int ni = i + dx[k], nj = j + dy[k];
        if (ni < 0 or ni >= H or nj < 0 or nj >= W) continue;
        same_color |= (vs[i][j] == vs[ni][nj]);
      }
      if (same_color) {
        first_time[i][j] = 0;
        que.emplace(i, j);
      }
    }
  }
  while (que.size()) {
    int i, j;
    tie(i, j) = que.front();
    que.pop();
    ll t = first_time[i][j];
    for (int k = 0; k < 4; k++) {
      int ni = i + dx[k], nj = j + dy[k];
      if (ni < 0 or ni >= H or nj < 0 or nj >= W) continue;
      if (first_time[ni][nj] < LONGINF) continue;
      first_time[ni][nj] = t + 1;
      que.emplace(ni, nj);
    }
  }
  while (T--) {
    ll x, y, p;
    scanf("%lld%lld%lld", &x, &y, &p);
    x--;
    y--;
    if (p <= first_time[x][y])
      printf("%d\n", vs[x][y] ? 1 : 0);
    else {
      int d = (p - first_time[x][y]) % 2;
      printf("%d\n", (vs[x][y] ? 1 : 0) ^ d);
    }
  }
  return 0;
}
