#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int cmod = 998244353;
const long long int N = 5e5 + 5;
const long long int inf = 1e16 + 2;
long long int arr[1001][1001];
long long int dis[1001][1001], vis[1001][1001];
long long int dx[] = {0, 0, -1, 1};
long long int dy[] = {1, -1, 0, 0};
bool valid(long long int x, long long int y, long long int n, long long int m) {
  return (x >= 0 and x < n and y >= 0 and y < m);
}
void solve(long long int ze) {
  long long int n, m, t;
  cin >> n >> m >> t;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      dis[i][j] = inf;
      vis[i][j] = 0;
      char a;
      cin >> a;
      arr[i][j] = a - '0';
    }
  }
  queue<pair<long long int, long long int>> q;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      for (long long int k = 0; k < 4; k++) {
        long long int x = i + dx[k], y = j + dy[k];
        if (valid(x, y, n, m) and arr[x][y] == arr[i][j])
          q.push({i, j}), dis[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    pair<long long int, long long int> cor = q.front();
    vis[cor.first][cor.second] = 1;
    q.pop();
    for (long long int i = 0; i < 4; i++) {
      long long int x = dx[i] + cor.first, y = dy[i] + cor.second;
      if (!valid(x, y, n, m) or vis[x][y]) continue;
      if (valid(x, y, n, m) and arr[x][y] != arr[cor.first][cor.second]) {
        dis[x][y] = min(dis[x][y], dis[cor.first][cor.second] + 1);
        vis[x][y] = 1;
        q.push({x, y});
      }
    }
  }
  while (t--) {
    long long int i, j, p;
    cin >> i >> j >> p;
    i--, j--;
    if (dis[i][j] == inf or dis[i][j] > p) {
      cout << arr[i][j] << '\n';
    } else {
      p -= dis[i][j];
      cout << ((p & 1) ? (arr[i][j]) : (arr[i][j] ^ 1)) << '\n';
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int testcase = 1;
  long long int x = 1;
  while (testcase--) {
    solve(x++);
  }
  return 0;
}
