#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5, inf = 1e9;
int n, m, t, ok[N][N];
long long d[N][N];
string s[N];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
bool is_val(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
    if (s[x][y] == s[nx][ny]) return true;
  }
  return false;
}
void bfs() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) d[i][j] = inf;
  deque<pair<pair<int, int>, int>> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (ok[i][j]) d[i][j] = 0, q.push_front({{i, j}, s[i][j] - '0'});
  while (!q.empty()) {
    pair<pair<int, int>, int> p = q.front();
    q.pop_front();
    int x = p.first.first, y = p.first.second, col = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
      int c = s[nx][ny] - '0';
      if (col == c && d[nx][ny] > d[x][y]) {
        d[nx][ny] = d[x][y];
        q.push_front({{nx, ny}, col});
      } else if (col != c && d[nx][ny] > d[x][y] + 1) {
        d[nx][ny] = d[x][y] + 1;
        q.push_back({{nx, ny}, c});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) ok[i][j] = is_val(i, j);
  bfs();
  for (int i = 0; i < t; i++) {
    int x, y;
    long long p;
    cin >> x >> y >> p;
    x--, y--;
    int bit = s[x][y] - '0';
    if (d[x][y] == inf || d[x][y] > p)
      cout << s[x][y] << "\n";
    else {
      long long k = 1LL * (p - d[x][y]);
      if (k % 2 == 0)
        cout << bit << "\n";
      else
        cout << (bit ^ 1) << "\n";
    }
  }
}
