#include <bits/stdc++.h>
template <typename Arg1>
void ZZ(const char* name, Arg1&& arg1) {
  std::cout << name << " = " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cout.write(names, comma - names) << " = " << arg1;
  ZZ(comma, args...);
}
using namespace std;
int t, n, m;
vector<vector<bool>> ar;
set<int> st;
queue<pair<int, int>> q;
vector<vector<int>> dist;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> t;
  ar.resize(n, vector<bool>(m));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      ar[i][j] = s[j] - '0';
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i < n - 1 && ar[i][j] == ar[i + 1][j]) {
        st.insert(i * m + j);
        st.insert((i + 1) * m + j);
      }
      if (j < m - 1 && ar[i][j] == ar[i][j + 1]) {
        st.insert(i * m + j);
        st.insert(i * m + j + 1);
      }
    }
  }
  dist.resize(n, vector<int>(m, -1));
  for (auto it = st.begin(); it != st.end(); it++) {
    q.push({*it, 0});
    int i = (*it) / m;
    int j = (*it) % m;
    dist[i][j] = 0;
  }
  while (!q.empty()) {
    int node = q.front().first;
    int i = node / m;
    int j = node % m;
    q.pop();
    if (i < n - 1 && ar[i][j] != ar[i + 1][j] && dist[i + 1][j] == -1) {
      q.push({(i + 1) * m + j, dist[i][j] + 1});
      dist[i + 1][j] = dist[i][j] + 1;
    }
    if (j < m - 1 && ar[i][j] != ar[i][j + 1] && dist[i][j + 1] == -1) {
      q.push({i * m + j + 1, dist[i][j] + 1});
      dist[i][j + 1] = dist[i][j] + 1;
    }
    if (i > 0 && ar[i][j] != ar[i - 1][j] && dist[i - 1][j] == -1) {
      q.push({(i - 1) * m + j, dist[i][j] + 1});
      dist[i - 1][j] = dist[i][j] + 1;
    }
    if (j > 0 && ar[i][j] != ar[i][j - 1] && dist[i][j - 1] == -1) {
      q.push({i * m + j - 1, dist[i][j] + 1});
      dist[i][j - 1] = dist[i][j] + 1;
    }
  }
  while (t--) {
    int i, j;
    long long p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (dist[i][j] == -1 || dist[i][j] >= p) {
      cout << ar[i][j] << endl;
    } else {
      bool flip = (p - dist[i][j]) % 2;
      if (flip)
        cout << !ar[i][j] << endl;
      else
        cout << ar[i][j] << endl;
    }
  }
  return 0;
}
