#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
template <class T>
void print(T& x) {
  cout << x;
}
template <class T, class U>
void print(pair<T, U>& p) {
  cout << "(";
  print(p.first);
  cout << ", ";
  print(p.second);
  cout << ")";
}
template <class T>
void print(vector<T>& v) {
  cout << "{";
  if (((int)(v).size())) print(v[0]);
  for (int i = 1; i < ((int)(v).size()); i++) cout << ", ", print(v[i]);
  cout << "}\n";
}
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m;
char A[maxn][maxn], vis[maxn][maxn];
long long t[maxn][maxn];
queue<pair<int, int> > q;
void bfs() {
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 1 || ny < 1 || nx > n || ny > m) continue;
      t[nx][ny] = min(t[nx][ny], t[x][y] + 1);
      if (!vis[nx][ny]) {
        q.push({nx, ny});
        vis[nx][ny] = 1;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int p;
  cin >> n >> m >> p;
  memset(A, -1, sizeof(A));
  memset(t, 127, sizeof(t));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> A[i][j];
      A[i][j] -= '0';
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (A[i - 1][j] == A[i][j] || A[i + 1][j] == A[i][j] ||
          A[i][j + 1] == A[i][j] || A[i][j - 1] == A[i][j])
        q.push({i, j}), t[i][j] = 1, vis[i][j] = 1;
    }
  }
  bfs();
  for (int i = 1; i <= p; ++i) {
    int x, y;
    long long z;
    cin >> x >> y >> z;
    if (z < t[x][y])
      cout << (int)A[x][y] << endl;
    else {
      cout << (A[x][y] ^ ((z - t[x][y] + 1) % 2)) << endl;
    }
  }
  return 0;
}
