#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> dir[] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool check(int i, int j) {
  return (i <= n - 1 && i >= 0 && j <= m - 1 && j >= 0);
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> n >> m >> t;
  string s[n];
  for (int i = 0; i < n; i++) cin >> s[i];
  int a[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      a[i][j] = (s[i][j] - '0');
    }
  queue<pair<int, int> > q;
  long long di[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) di[i][j] = numeric_limits<long long>::max();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int ch = 0;
      for (auto d : dir) {
        if (check(i + d.first, j + d.second) &&
            a[i + d.first][j + d.second] == a[i][j]) {
          q.push({i, j});
          di[i][j] = 0;
          ch = 1;
          break;
        }
      }
      if (ch == 0) di[i][j] = numeric_limits<long long>::max();
    }
  while (!q.empty()) {
    auto c = q.front();
    q.pop();
    for (auto d : dir) {
      if (check(c.first + d.first, c.second + d.second) &&
          di[c.first + d.first][c.second + d.second] ==
              numeric_limits<long long>::max()) {
        q.push({c.first + d.first, c.second + d.second});
        di[c.first + d.first][c.second + d.second] = di[c.first][c.second] + 1;
      }
    }
  }
  for (int tc = 0; tc < t; tc++) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    if (p < di[i - 1][j - 1]) {
      cout << a[i - 1][j - 1] << endl;
      continue;
    }
    cout << (((p - di[i - 1][j - 1]) & 1) ^ a[i - 1][j - 1]) << endl;
  }
}
