#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int dist[N][N], n, m, t;
queue<int> q;
char c[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool good[N][N];
inline bool ok(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); }
void BFS() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dist[i][j] = 1e9;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (ok(x, y) && c[x][y] == c[i][j]) {
          dist[i][j] = 0;
          q.push(i);
          q.push(j);
        }
      }
    }
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x2 = x + dx[i];
      int y2 = y + dy[i];
      if (ok(x2, y2) && dist[x2][y2] == 1e9) {
        dist[x2][y2] = dist[x][y] + 1;
        q.push(x2);
        q.push(y2);
      }
    }
  }
  return;
}
int main() {
  cin >> n >> m >> t;
  for (int i = 0; i <= n - 1; i++) cin >> c[i];
  BFS();
  while (t--) {
    int x, y;
    long long p;
    cin >> x >> y >> p;
    x--, y--;
    bool f = (c[x][y] - '0');
    p -= (long long)dist[x][y];
    if (p < 0 || dist[x][y] == 1e9)
      cout << f << "\n";
    else {
      p %= 2ll;
      if (p) f = !f;
      cout << f << "\n";
    }
  }
  return 0;
}
