#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
int g[N];
int level[N];
int n, m;
string a[1005];
vector<pair<int, int>> moves = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int comp[N];
bool changing[N];
int visited[N];
bool valid(int pi, int pj, int i, int j) {
  return i >= 0 && j >= 0 && i < n && j < m && a[i][j] == a[pi][pj] &&
         comp[i * m + j] == -1;
}
bool otherValid(int i, int j) {
  return i >= 0 && j >= 0 && i < n && j < m && !changing[comp[i * m + j]] &&
         !visited[i * m + j];
}
void dfs(int x, int y, int c) {
  comp[x * m + y] = c;
  for (auto move : moves) {
    int nx = x + move.first;
    int ny = y + move.second;
    if (valid(x, y, nx, ny)) {
      changing[c] = 1;
      dfs(nx, ny, c);
    }
  }
}
void bfs() {
  queue<pair<int, pair<int, int>>> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (changing[comp[i * m + j]]) {
        visited[i * m + j] = 1;
        g[comp[i * m + j]] = i * m + j;
        q.push({i * m + j, {i * m + j, 0}});
      }
    }
  while (!q.empty()) {
    int i = q.front().first / m;
    int j = q.front().first % m;
    int c = q.front().second.first;
    int t = q.front().second.second;
    q.pop();
    for (auto move : moves) {
      int nx = i + move.first;
      int ny = j + move.second;
      if (otherValid(nx, ny)) {
        g[comp[nx * m + ny]] = c;
        level[comp[nx * m + ny]] = t + 1;
        visited[nx * m + ny] = t + 1;
        q.push({nx * m + ny, {c, t + 1}});
      }
    }
  }
}
int main() {
  int t;
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) cin >> a[i];
  memset(comp, -1, sizeof comp);
  int c = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (comp[i * m + j] == -1) dfs(i, j, c), c++;
  bfs();
  while (t--) {
    long long i, j, p;
    cin >> i >> j >> p;
    i--, j--;
    int cc = comp[i * m + j];
    if (changing[cc]) {
      if (p & 1) {
        if (a[i][j] == '1')
          cout << '0' << endl;
        else
          cout << '1' << endl;
      } else
        cout << a[i][j] << endl;
    } else {
      if (level[cc] == 0 || p <= level[cc])
        cout << a[i][j] << endl;
      else {
        p -= level[cc];
        if ((p & 1) != 0) {
          if (a[i][j] == '1')
            cout << '0' << endl;
          else
            cout << '1' << endl;
        } else
          cout << a[i][j] << endl;
      }
    }
  }
}
