#include <bits/stdc++.h>
using namespace std;
int vis[1005][1005];
int ar[1005][1005];
bool st[1005][1005];
int n, m;
bool check(int i, int j) {
  if (i == 0 || i > n || j == 0 || j > m) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long a, b, c, d, e, f, i, j, k, o, x, y;
  cin >> n >> m >> k;
  memset(st, false, sizeof(st));
  string s;
  for (i = 1; i <= n; i++) {
    cin >> s;
    for (j = 0; j < m; j++) {
      ar[i][j + 1] = (s[j] - '0');
    }
  }
  vector<pair<int, int> > st;
  memset(vis, -1, sizeof(vis));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (check(i + 1, j) && ar[i][j] == ar[i + 1][j] && vis[i][j] == -1)
        st.push_back({i, j}), vis[i][j] = 0;
      if (check(i, j + 1) && ar[i][j] == ar[i][j + 1] && vis[i][j] == -1)
        st.push_back({i, j}), vis[i][j] = 0;
      if (check(i - 1, j) && ar[i][j] == ar[i - 1][j] && vis[i][j] == -1)
        st.push_back({i, j}), vis[i][j] = 0;
      if (check(i, j - 1) && ar[i][j] == ar[i][j - 1] && vis[i][j] == -1)
        st.push_back({i, j}), vis[i][j] = 0;
    }
  }
  for (int kk = 0; kk < st.size(); kk++) {
    pair<int, int> p = st[kk];
    i = p.first;
    j = p.second;
    if (check(i + 1, j) && ar[i][j] != ar[i + 1][j] && vis[i + 1][j] == -1)
      st.push_back({i + 1, j}), vis[i + 1][j] = vis[i][j] + 1;
    if (check(i, j + 1) && ar[i][j] != ar[i][j + 1] && vis[i][j + 1] == -1)
      st.push_back({i, j + 1}), vis[i][j + 1] = vis[i][j] + 1;
    if (check(i - 1, j) && ar[i][j] != ar[i - 1][j] && vis[i - 1][j] == -1)
      st.push_back({i - 1, j}), vis[i - 1][j] = vis[i][j] + 1;
    if (check(i, j - 1) && ar[i][j] != ar[i][j - 1] && vis[i][j - 1] == -1)
      st.push_back({i, j - 1}), vis[i][j - 1] = vis[i][j] + 1;
  }
  while (k--) {
    cin >> i >> j >> a;
    if (vis[i][j] == -1)
      cout << ar[i][j] << "\n";
    else if (a < vis[i][j])
      cout << ar[i][j] << "\n";
    else {
      long long ans = a - vis[i][j];
      if (ans % 2 == 1)
        cout << (!ar[i][j]) << "\n";
      else
        cout << ar[i][j] << "\n";
    }
  }
}
