#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, t;
  cin >> n >> m >> t;
  vector<string> f(n);
  for (int i = 0; i < n; i++) cin >> f[i];
  vector<vector<int>> g(n, vector<int>(m, -1));
  auto isRange = [&](int i, int j) {
    return 0 <= i && i < n && 0 <= j && j < m;
  };
  auto rev = [&](char x) {
    if (x == '1')
      return '0';
    else
      return '1';
  };
  auto color = [&](int i, int j, ll t) {
    if (g[i][j] == -1 || t < g[i][j])
      return f[i][j];
    else if ((t - g[i][j]) % 2)
      return rev(f[i][j]);
    else
      return f[i][j];
  };
  auto flip = [&](int i, int j, int t) {
    assert(g[i][j] == -1);
    for (int k = 0; k < 4; k++) {
      int toi = i + dx[k], toj = j + dy[k];
      if (isRange(toi, toj) && color(toi, toj, t) == f[i][j]) {
        return true;
      }
    }
    return false;
  };
  queue<pair<int, int>> que;
  auto flip_check = [&](int i, int j, int t) {
    if (flip(i, j, t)) {
      g[i][j] = t, que.push({i, j});
    }
  };
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      flip_check(i, j, 0);
    }
  }
  for (int t = 1; !que.empty(); t++) {
    int sz = que.size();
    for (int _ = 0; _ < sz; _++) {
      auto tmp = que.front();
      que.pop();
      int i = tmp.first, j = tmp.second;
      for (int k = 0; k < 4; k++) {
        int toi = i + dx[k], toj = j + dy[k];
        if (!isRange(toi, toj) || g[toi][toj] != -1) continue;
        flip_check(toi, toj, t);
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int i, j;
    ll p;
    cin >> i >> j >> p;
    i--, j--;
    cout << color(i, j, p) << endl;
  }
  return 0;
}
