#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\n";
  err(++it, args...);
}
const double PI = acos(-1);
const double eps = (1e-9);
const long long int INF = 2e18;
const int M = (1e9) + 7;
const int N = 1003;
bitset<N> v[N];
int dp[N][N];
int n, m;
int I[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int check(int x, int y) {
  int k, a, b;
  for (k = 0; k < 4; ++k) {
    a = x + I[k][0], b = y + I[k][1];
    if ((a >= 0 && a <= n - 1) && (b >= 0 && b <= m - 1) && v[a][b] == v[x][y])
      return 1;
  }
  return 0;
}
long long int query(int x, int y) {
  int i, ans = M;
  for (i = 0; i < n; ++i) ans = min(ans, abs(x - i) + dp[i][y]);
  if (ans == M) return INF;
  return ans;
}
void solve() {
  int i, j, q;
  scanf("%d%d%d", &n, &m, &q);
  string str;
  for (i = 0; i < n; ++i) {
    cin >> str;
    for (j = 0; j < m; ++j) v[i][j] = str[j] - '0';
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) dp[i][j] = M;
  };
  int last;
  for (i = 0; i < n; ++i) {
    last = -M - M;
    for (j = 0; j < m; ++j) {
      if (check(i, j)) last = j;
      dp[i][j] = min(dp[i][j], j - last);
    }
    last = M + M;
    for (j = m - 1; j >= 0; --j) {
      if (check(i, j)) last = j;
      dp[i][j] = min(dp[i][j], last - j);
    }
  }
  long long int p, d;
  while (q--) {
    scanf("%d%d", &i, &j);
    i--, j--;
    scanf("%lld", &p);
    d = query(i, j);
    if (p <= d)
      p = 0;
    else
      p -= d, p = (p & 1);
    int x = v[i][j] ^ p;
    printf("%d\n", x);
  }
}
int main() {
  int t = 1;
  while (t--) solve();
}
