#include <bits/stdc++.h>
using namespace std;
struct kor {
  int x, y;
};
const int N = 1005;
int n, m;
vector<vector<bool>> pole(N, vector<bool>(N, 0)), used = pole;
vector<vector<int>> dist(N, vector<int>(N, N* N));
bool ok(int i, int j) {
  bool f = 0;
  if ((i - 1 >= 0) && (pole[i - 1][j] == pole[i][j])) f = 1;
  if ((i + 1 < n) && (pole[i + 1][j] == pole[i][j])) f = 1;
  if ((j - 1 >= 0) && (pole[i][j - 1] == pole[i][j])) f = 1;
  if ((j + 1 < m) && (pole[i][j + 1] == pole[i][j])) f = 1;
  return f;
}
queue<kor> Q;
void dfs(int i, int j) {
  used[i][j] = 1;
  dist[i][j] = 0;
  Q.push({i, j});
  if ((i - 1 >= 0) && (pole[i - 1][j] == pole[i][j]) && !used[i - 1][j])
    dfs(i - 1, j);
  if ((i + 1 < n) && (pole[i + 1][j] == pole[i][j]) && !used[i + 1][j])
    dfs(i + 1, j);
  if ((j - 1 >= 0) && (pole[i][j - 1] == pole[i][j]) && !used[i][j - 1])
    dfs(i, j - 1);
  if ((j + 1 < m) && (pole[i][j + 1] == pole[i][j]) && !used[i][j + 1])
    dfs(i, j + 1);
}
void bfs() {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (ok(i, j) && !used[i][j]) {
        dfs(i, j);
      }
    }
  }
  while (!Q.empty()) {
    int X = (Q.front()).x, Y = (Q.front()).y;
    Q.pop();
    for (int i = -1; i <= 1; ++i) {
      for (int j = -1; j <= 1; ++j) {
        if (abs(i) + abs(j) != 1) continue;
        int x = X + i, y = Y + j;
        if (0 <= x && x < n && 0 <= y && y < m && !used[x][y]) {
          Q.push({x, y});
          used[x][y] = 1;
          dist[x][y] = dist[X][Y] + 1;
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int q;
  cin >> n >> m >> q;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < m; ++j) {
      pole[i][j] = (s[j] == '1');
    }
  }
  bfs();
  while (q--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    --i, --j;
    if (p <= dist[i][j] || dist[i][j] > n * m) {
      cout << pole[i][j] << "\n";
    } else {
      cout << (p - dist[i][j] + pole[i][j]) % 2 << "\n";
    }
  }
  return 0;
}
