#include <bits/stdc++.h>
using namespace std;
struct dl {
  int row, col;
};
int n, m, t;
dl q[2000000];
char c[2000][2000];
int f[2000][2000], vis[2000][2000];
void solve() {
  long long x, y, time;
  cin >> x >> y >> time;
  if (m * n == 1) {
    cout << c[1][1] - 48 << endl;
    return;
  }
  if (time < f[x][y] || vis[x][y] == 0)
    cout << c[x][y] - 48 << endl;
  else
    cout << (c[x][y] - 48 + (time - f[x][y]) % 2) % 2 << endl;
}
int go(int x, int y) {
  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++)
      if (i + x > 0 && i + x <= n && j + y > 0 && j + y <= m)
        if (i || j)
          if (i * j == 0)
            if (c[i + x][j + y] == c[x][y]) {
              return 1;
            }
  return 0;
}
void dfs(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m) return;
  vis[x][y] = 1;
  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++)
      if (i || j)
        if (i * j == 0)
          if (c[i + x][j + y] == c[x][y])
            if (vis[i + x][j + y] == 0) dfs(i + x, j + y);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m >> t;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> c[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (go(i, j)) dfs(i, j);
  int r = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j]) {
        r++;
        q[r].row = i;
        q[r].col = j;
      }
  int x, y;
  int l = 1;
  while (l <= r) {
    x = q[l].row;
    y = q[l].col;
    for (int i = -1; i <= 1; i++)
      for (int j = -1; j <= 1; j++)
        if (i + x > 0 && i + x <= n && j + y > 0 && j + y <= m)
          if (i || j)
            if (i * j == 0)
              if (vis[i + x][j + y] == 0) {
                vis[i + x][j + y] = 1;
                r++;
                q[r].row = i + x;
                q[r].col = j + y;
                f[i + x][j + y] = f[x][y] + 1;
              }
    l++;
  }
  while (t--) solve();
}
