#include <bits/stdc++.h>
using namespace std;
char a[1005][1005], b[1005][1005];
bool ok[1005][1005];
bool vis[1005][1005];
long long dis[1005][1005];
vector<long long> d_x = {1, -1, 0, 0};
vector<long long> d_y = {0, 0, 1, -1};
int32_t main() {
  long long n, m, t;
  cin >> n >> m >> t;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  queue<pair<long long, long long> > q;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (a[i][j] == a[i - 1][j] || a[i][j] == a[i + 1][j] ||
          a[i][j] == a[i][j - 1] || a[i][j] == a[i][j + 1]) {
        ok[i][j] = 1;
        q.push({i, j});
        vis[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    long long x = q.front().first, y = q.front().second;
    q.pop();
    for (long long i = 0; i < 4; i++) {
      long long c_x = d_x[i] + x, c_y = d_y[i] + y;
      if (c_x <= 0 || c_y <= 0 || c_x > n || c_y > m || vis[c_x][c_y]) {
        continue;
      }
      dis[c_x][c_y] = dis[x][y] + 1;
      vis[c_x][c_y] = 1;
      q.push({c_x, c_y});
    }
  }
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    if (ok[i][j])
      if (p % 2 == 0) {
        cout << a[i][j] << "\n";
      } else {
        cout << (a[i][j] == '1' ? '0' : '1') << "\n";
      }
    else {
      if (!vis[i][j]) {
        cout << a[i][j] << "\n";
        continue;
      }
      if (max(0ll, (p - dis[i][j])) % 2 == 0) {
        cout << a[i][j] << "\n";
      } else {
        cout << (a[i][j] == '1' ? '0' : '1') << "\n";
      }
    }
  }
  return 0;
}
