#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, t;
  cin >> n >> m >> t;
  vector<string> board(n);
  for (int i = 0; i < n; i++) {
    cin >> board[i];
  }
  vector<int> shift = {1, 0, -1, 0, 1};
  vector<vector<int>> dist(n, vector<int>(m, -1));
  deque<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++) {
        int ii = i + shift[k];
        int jj = j + shift[k + 1];
        if (ii < 0 || ii >= n || jj < 0 || jj >= m) {
          continue;
        }
        if (board[ii][jj] == board[i][j]) {
          q.push_back({i, j});
          dist[i][j] = 0;
          break;
        }
      }
    }
  }
  for (; !q.empty();) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    for (int k = 0; k < 4; k++) {
      int ii = i + shift[k];
      int jj = j + shift[k + 1];
      if (ii < 0 || ii >= n || jj < 0 || jj >= m || dist[ii][jj] != -1) {
        continue;
      }
      dist[ii][jj] = dist[i][j] + 1;
      q.push_back({ii, jj});
    }
  }
  for (int i = 0; i < t; i++) {
    int x, y;
    long long p;
    cin >> x >> y >> p;
    x--;
    y--;
    int v = board[x][y] - '0';
    if (dist[x][y] == -1 || dist[x][y] >= p) {
      cout << v << "\n";
    } else {
      int r = (p - dist[x][y]) % 2;
      cout << (v + r) % 2 << "\n";
    }
  }
  return 0;
}
