#include <bits/stdc++.h>
using namespace std;
long long n, m, t;
long long x, y, x2, y2, p;
long long bio[1010][1010];
char mat[1010][1010];
long long smjer[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
queue<pair<long long, long long>> q;
bool tr;
bool isti = false;
int main() {
  cin >> n >> m >> t;
  for (long long i = 0; i <= n + 1; i++) {
    for (long long j = 0; j <= m + 1; j++) {
      mat[i][j] = '2';
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> mat[i][j];
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      tr = false;
      for (long long k = 0; k < 4; k++) {
        x = i + smjer[k][0];
        y = j + smjer[k][1];
        if (mat[x][y] == mat[i][j]) {
          tr = true;
        }
      }
      if (tr) {
        bio[i][j] = 1;
        q.push({i, j});
      }
    }
  }
  if (q.empty()) {
    isti = true;
  }
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (x <= 0 || y <= 0 || x > n || y > m) continue;
    for (long long i = 0; i < 4; i++) {
      x2 = x + smjer[i][0];
      y2 = y + smjer[i][1];
      if (bio[x2][y2] == 0) {
        bio[x2][y2] = bio[x][y] + 1;
        q.push({x2, y2});
      }
    }
  }
  for (long long i = 0; i < t; i++) {
    cin >> x >> y >> p;
    if (isti) {
      cout << mat[x][y] << "\n";
    } else {
      if (p < bio[x][y]) {
        cout << mat[x][y] << "\n";
      } else {
        cout << (mat[x][y] + p - bio[x][y] + 1) % 2 << "\n";
      }
    }
  }
}
