#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 5;
const long long INF = 1e18 + 1;
vector<vector<int> > mat(N, vector<int>(N));
vector<vector<long long> > lvl(N, vector<long long>(N, INF));
vector<vector<bool> > vis(N, vector<bool>(N, false));
void solve() {
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < m; j++) {
      mat[i][j] = str[j] - '0';
    }
  }
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i - 1 >= 0 && mat[i - 1][j] == mat[i][j] ||
          i + 1 < n && mat[i + 1][j] == mat[i][j] ||
          j - 1 >= 0 && mat[i][j - 1] == mat[i][j] ||
          j + 1 < m && mat[i][j + 1] == mat[i][j]) {
        lvl[i][j] = 0;
        q.push({i, j});
        vis[i][j] = true;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> x = q.front();
    q.pop();
    int i = x.first, j = x.second;
    if (i - 1 >= 0 && !vis[i - 1][j]) {
      lvl[i - 1][j] = lvl[i][j] + 1;
      vis[i - 1][j] = true;
      q.push({i - 1, j});
    }
    if (i + 1 < n && !vis[i + 1][j]) {
      lvl[i + 1][j] = lvl[i][j] + 1;
      vis[i + 1][j] = true;
      q.push({i + 1, j});
    }
    if (j - 1 >= 0 && !vis[i][j - 1]) {
      lvl[i][j - 1] = lvl[i][j] + 1;
      vis[i][j - 1] = true;
      q.push({i, j - 1});
    }
    if (j + 1 < m && !vis[i][j + 1]) {
      lvl[i][j + 1] = lvl[i][j] + 1;
      vis[i][j + 1] = true;
      q.push({i, j + 1});
    }
  }
  while (t--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    --i;
    --j;
    if (p < lvl[i][j])
      cout << mat[i][j] << '\n';
    else {
      if ((p - lvl[i][j]) % 2 == 0)
        cout << mat[i][j] << '\n';
      else
        cout << (mat[i][j] ^ 1) << '\n';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
