#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 3;
const int maxm = 1e7 + 3;
const int mod = 1e9 + 7;
const int oo = 1e9 + 5;
const long double pi = 3.141592653589793238462643383279502884;
void input() {
  if (fopen("abc"
            ".inp",
            "r")) {
    freopen(
        "abc"
        ".inp",
        "r", stdin);
    freopen(
        "abc"
        ".out",
        "w", stdout);
  }
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int n, m, qry, need_change;
int a[maxn][maxn], ok[maxn][maxn], vis[maxn][maxn];
long long d[maxn][maxn];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
bool inside(int &x, int &y) { return x > 0 && y > 0 && x <= m && y <= n; }
int main() {
  input();
  cin >> m >> n >> qry;
  char c;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  queue<pair<int, int> > q;
  memset(d, 0x3f, sizeof(d));
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      int cnt = 0;
      for (int k = 0; k < 4; ++k) {
        int ni = dx[k] + i, nj = dy[k] + j;
        if (inside(ni, nj)) cnt += a[i][j] == a[ni][nj];
      }
      if (cnt) {
        d[i][j] = 0;
        vis[i][j] = ok[i][j] = 1;
        q.push({i, j});
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (inside(nx, ny) && !vis[nx][ny]) {
        vis[nx][ny] = 1;
        d[nx][ny] = d[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  int i, j;
  long long p;
  while (qry--) {
    cin >> i >> j >> p;
    if (d[i][j] > p)
      cout << a[i][j] << "\n";
    else
      cout << (a[i][j] ^ ((p - d[i][j]) % 2)) << "\n";
  }
  return 0;
}
