#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mx = 1e3 + 4;
vector<vector<long long>> dist;
vector<vector<bool>> col;
queue<pair<long long, long long>> vis;
long long n, m, t;
long long expo_pow(long long x, long long y) {
  if (y == 0) return 1;
  y = y % (mod - 1);
  x %= mod;
  if (y == 0) y = mod - 1;
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return res;
}
void solve() {
  cin >> n >> m >> t;
  col.resize(n + 1, vector<bool>(m + 1));
  dist.resize(n + 1, vector<long long>(m + 1));
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      dist[i][j] = -1;
      char tmp;
      cin >> tmp;
      col[i][j] = (tmp == '1');
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (i != 1 and col[i - 1][j] == col[i][j]) {
        dist[i][j] = 0;
        vis.push({i, j});
      } else if (i != n and col[i + 1][j] == col[i][j]) {
        dist[i][j] = 0;
        vis.push({i, j});
      } else if (j != 1 and col[i][j - 1] == col[i][j]) {
        dist[i][j] = 0;
        vis.push({i, j});
      } else if (j != m and col[i][j + 1] == col[i][j]) {
        dist[i][j] = 0;
        vis.push({i, j});
      }
    }
  }
  while (!vis.empty()) {
    auto cur = vis.front();
    vis.pop();
    if (cur.first != 1 and dist[cur.first - 1][cur.second] == -1) {
      dist[cur.first - 1][cur.second] = dist[cur.first][cur.second] + 1;
      vis.push({cur.first - 1, cur.second});
    }
    if (cur.first != n and dist[cur.first + 1][cur.second] == -1) {
      dist[cur.first + 1][cur.second] = dist[cur.first][cur.second] + 1;
      vis.push({cur.first + 1, cur.second});
    }
    if (cur.second != 1 and dist[cur.first][cur.second - 1] == -1) {
      dist[cur.first][cur.second - 1] = dist[cur.first][cur.second] + 1;
      vis.push({cur.first, cur.second - 1});
    }
    if (cur.second != m and dist[cur.first][cur.second + 1] == -1) {
      dist[cur.first][cur.second + 1] = dist[cur.first][cur.second] + 1;
      vis.push({cur.first, cur.second + 1});
    }
  }
  while (t--) {
    long long x, y, p;
    cin >> x >> y >> p;
    if (dist[x][y] == -1) {
      cout << col[x][y] << "\n";
      continue;
    }
    if (p <= dist[x][y]) {
      cout << col[x][y] << "\n";
      continue;
    }
    long long tmp = p - dist[x][y];
    if (tmp % 2) {
      cout << !col[x][y] << "\n";
    } else {
      cout << col[x][y] << "\n";
    }
  }
}
signed main() {
  long long test = 1;
  long long i = 1;
  while (test--) {
    solve();
  }
}
