#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int ans = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    ans = (ans << 1) + (ans << 3) + (ch ^ 48), ch = getchar();
  return ans;
}
inline long long L_read() {
  long long ans = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    ans = (ans << 1) + (ans << 3) + (ch ^ 48), ch = getchar();
  return ans;
}
int dx[5] = {0, -1, 1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};
struct cz {
  long long Time;
  bool Initial;
};
cz state[1050][1050];
bool mp[1050][1050];
long long p;
string s;
struct pos {
  int x;
  int y;
};
pos ww;
inline bool check(int x, int y, int n, int m) {
  for (int i = 1; i <= 4; i++) {
    int nowx = x + dx[i], nowy = y + dy[i];
    if (nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m)
      if (mp[nowx][nowy] == mp[x][y]) return true;
  }
  return false;
}
int n, m, k, x, y;
bool vis[1050][1050];
queue<pos> qwq;
inline void bfs() {
  while (!qwq.empty()) {
    int nowx = qwq.front().x;
    int nowy = qwq.front().y;
    qwq.pop();
    for (int i = 1; i <= 4; i++) {
      if (nowx + dx[i] >= 1 && nowx + dx[i] <= n && nowy + dy[i] >= 1 &&
          nowy + dy[i] <= m) {
        if (!vis[nowx + dx[i]][nowy + dy[i]]) {
          vis[nowx + dx[i]][nowy + dy[i]] = 1;
          state[nowx + dx[i]][nowy + dy[i]].Time = state[nowx][nowy].Time + 1;
          ww.x = nowx + dx[i];
          ww.y = nowy + dy[i];
          qwq.push(ww);
        }
      }
    }
  }
}
int Odd(long long i) { return (i & 1) != 0; }
int main() {
  bool ff = 0;
  n = read(), m = read(), k = read();
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '1') state[i][j + 1].Initial = 1, mp[i][j + 1] = 1;
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (check(i, j, n, m)) {
        ww.x = i, ww.y = j;
        vis[i][j] = 1;
        qwq.push(ww);
        ff = 1;
      }
  if (!ff) {
    for (int i = 1; i <= k; i++) {
      x = read(), y = read(), p = L_read();
      cout << mp[x][y] << '\n';
    }
    return 0;
  }
  bfs();
  for (int i = 1; i <= k; i++) {
    x = read(), y = read();
    p = L_read();
    if (p <= state[x][y].Time) {
      cout << state[x][y].Initial << '\n';
      continue;
    }
    if (!Odd(p - state[x][y].Time))
      cout << state[x][y].Initial << '\n';
    else {
      if (state[x][y].Initial == 0)
        cout << 1 << '\n';
      else
        cout << "0\n";
    }
  }
  return 0;
}
