#include <bits/stdc++.h>
using namespace std;
int dw[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long i, j, k;
  long long n, m;
  cin >> n >> m;
  long long t;
  cin >> t;
  vector<string> s(n);
  for (i = (0); i < (n); i++) cin >> s[i];
  bool good[n][m];
  for (i = (0); i < (n); i++)
    for (j = (0); j < (m); j++) {
      bool isBad = false;
      for (k = (0); k < (4); k++) {
        long long x = i + dw[k][0];
        long long y = j + dw[k][1];
        if (x >= 0 && x < n && y >= 0 && y < m) {
          if (s[x][y] == s[i][j]) {
            isBad = true;
            break;
          }
        }
      }
      good[i][j] = !isBad;
    }
  long long status[n][m];
  queue<pair<long long, long long> > q;
  for (i = (0); i < (n); i++)
    for (j = (0); j < (m); j++) {
      if (!good[i][j]) {
        status[i][j] = 0;
        q.push({i, j});
      } else {
        status[i][j] = 1000000000000000011LL;
      }
    }
  while (!q.empty()) {
    pair<long long, long long> top = q.front();
    q.pop();
    for (k = (0); k < (4); k++) {
      long long x = top.first + dw[k][0];
      long long y = top.second + dw[k][1];
      if (x >= 0 && x < n && y >= 0 && y < m) {
        if (status[x][y] == 1000000000000000011LL) {
          status[x][y] = status[top.first][top.second] + 1;
          q.push({x, y});
        }
      }
    }
  }
  while (t--) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (status[a][b] > c) {
      cout << s[a][b] << '\n';
    } else {
      long long rem = (c - status[a][b]);
      if (rem % 2 == 1) {
        if (s[a][b] == '0')
          cout << "1\n";
        else
          cout << "0\n";
      } else {
        cout << s[a][b] << '\n';
      }
    }
  }
  return 0;
}
