#include <bits/stdc++.h>
const int N = 1e3 + 5;
using namespace std;
int n, m, t;
int nx[6] = {0, 1, 0, -1, 0};
int a[N][N], c[N][N];
queue<pair<int, int> > q;
void create() {
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      for (int p = 0; p <= 3; p++) {
        int u = i + nx[p];
        int v = j + nx[p + 1];
        if (c[u][v] == c[i][j]) a[i][j] = 1, p = 4;
      }
      a[i][j]--;
      if (a[i][j] == 0)
        q.push(make_pair(i, j));
      else
        a[i][j] = 1e9;
    }
  }
}
void BFS() {
  while (q.size()) {
    pair<int, int> T = q.front();
    q.pop();
    for (int p = 0; p <= 3; p++) {
      int u = T.first + nx[p];
      int v = T.second + nx[p + 1];
      if (a[u][v] > a[T.first][T.second] + 1) {
        a[u][v] = a[T.first][T.second] + 1;
        q.push(make_pair(u, v));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> t;
  memset(c, -1, sizeof(c));
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      char cc;
      cin >> cc;
      c[i][j] = cc - '0';
    }
  create();
  BFS();
  int d = 0;
  while (t--) {
    d++;
    int u, v;
    long long p;
    cin >> u >> v >> p;
    if (a[u][v] == 1e9)
      cout << c[u][v] << '\n';
    else {
      if (p <= a[u][v])
        cout << c[u][v] << '\n';
      else {
        if ((p - (long long)a[u][v]) & 1)
          cout << 1 - c[u][v] << '\n';
        else
          cout << c[u][v] << '\n';
      }
    }
  }
}
