#include <bits/stdc++.h>
const int INF = std::numeric_limits<int>::max() / 2;
const long long INFLL = std::numeric_limits<long long>::max() / 2;
const int MAX_N = 1000;
const int DX[4] = {0, 1, 0, -1};
const int DY[4] = {1, 0, -1, 0};
int n, m, t, p[MAX_N][MAX_N];
char c[MAX_N][MAX_N];
bool in_grid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }
bool get_col(int i, int j, long long k) {
  if (p[i][j] == -1 || p[i][j] >= k)
    return c[i][j];
  else
    return c[i][j] ^ ((k - p[i][j]) & 1);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  std::cin >> n >> m >> t;
  for (int i = 0; i < n; ++i) {
    std::string s;
    std::cin >> s;
    for (int j = 0; j < m; ++j) c[i][j] = s[j] - '0';
  }
  std::queue<std::pair<int, int> > queue;
  std::fill(p[0], p[0] + MAX_N * MAX_N, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      for (int k = 0; k < 4; ++k) {
        int x = i + DX[k];
        int y = j + DY[k];
        if (in_grid(x, y) && c[i][j] == c[x][y]) {
          p[i][j] = 0;
          queue.emplace(i, j);
          break;
        }
      }
  while (!queue.empty()) {
    auto [i, j] = queue.front();
    queue.pop();
    for (int k = 0; k < 4; ++k) {
      int x = i + DX[k];
      int y = j + DY[k];
      if (in_grid(x, y) && p[x][y] == -1) {
        p[x][y] = p[i][j] + 1;
        queue.emplace(x, y);
      }
    }
  }
  while (t--) {
    int i, j;
    long long k;
    std::cin >> i >> j >> k;
    std::cout << (int)get_col(i - 1, j - 1, k) << '\n';
  }
  return 0;
}
