#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
  int n, m, i, t, j;
  cin >> n >> m >> t;
  vector<vector<char> > v(n + 2, vector<char>(m + 2));
  for (i = 0; i < n + 2; i++) {
    for (j = 0; j < m + 2; j++) {
      if (i == 0 || j == 0 || i == n + 1 || j == m + 1)
        v[i][j] = '*';
      else
        cin >> v[i][j];
    }
  }
  queue<pair<int, int> > q;
  int visited[n + 1][m + 1];
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      visited[i][j] = -1;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (v[i - 1][j] == v[i][j] || v[i][j] == v[i + 1][j] ||
          v[i][j] == v[i][j + 1] || v[i][j] == v[i][j - 1]) {
        q.push({i, j});
        visited[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (x + 1 <= n && visited[x + 1][y] == -1) {
      visited[x + 1][y] = visited[x][y] + 1;
      q.push({x + 1, y});
    }
    if (y + 1 <= m && visited[x][y + 1] == -1) {
      visited[x][y + 1] = visited[x][y] + 1;
      q.push({x, y + 1});
    }
    if (x - 1 > 0 && visited[x - 1][y] == -1) {
      visited[x - 1][y] = visited[x][y] + 1;
      q.push({x - 1, y});
    }
    if (y - 1 > 0 && visited[x][y - 1] == -1) {
      visited[x][y - 1] = visited[x][y] + 1;
      q.push({x, y - 1});
    }
  }
  long long p, val;
  while (t--) {
    cin >> i >> j >> p;
    val = visited[i][j];
    if (val > p || val == -1)
      cout << v[i][j] << '\n';
    else {
      long long diff = p - val, k = 2;
      if (diff % k == 0) {
        if (v[i][j] == '0')
          cout << 1 << '\n';
        else
          cout << 0 << '\n';
      } else
        cout << v[i][j] << '\n';
    }
  }
}
