#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = numeric_limits<long long>::max() / 4;
constexpr long long n_max = 2e5 + 10;
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char c) { return to_string((string)&c); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0) cout << " ";
  print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) cout << " ";
  }
  cout << endl;
}
template <class T>
void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  long long h, w, t;
  cin >> h >> w >> t;
  vector<string> s(h);
  for (long long i = 0; i < (long long)(h); i++) cin >> s[i];
  long long dx[] = {0, 0, 1, -1};
  long long dy[] = {1, -1, 0, 0};
  auto update = [&]() {
    vector<string> s2 = s;
    for (long long i = 0; i < (long long)(h); i++)
      for (long long j = 0; j < (long long)(w); j++) {
        long long t = s[i][j] - '0';
        bool ch = false;
        for (long long k = 0; k < (long long)(4); k++) {
          long long nx = dx[k] + i;
          long long ny = dy[k] + j;
          if (0 > nx || 0 > ny || nx >= h || ny >= w) continue;
          ch |= s[i][j] == s[nx][ny];
        }
        if (ch) s2[i][j] = char('0' + (t ^ 1));
      }
    return s2;
  };
  vector<vector<long long>> table(h, vector<long long>(w));
  queue<pair<long long, long long>> que;
  for (long long i = 0; i < (long long)(h); i++)
    for (long long j = 0; j < (long long)(w); j++) {
      long long t = s[i][j] - '0';
      bool ch = false;
      for (long long k = 0; k < (long long)(4); k++) {
        long long nx = dx[k] + i;
        long long ny = dy[k] + j;
        if (0 > nx || 0 > ny || nx >= h || ny >= w) continue;
        ch |= s[i][j] == s[nx][ny];
      }
      if (ch) {
        table[i][j] = 1;
        que.emplace(i, j);
      }
    }
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    auto [x, y] = p;
    for (long long k = 0; k < (long long)(4); k++) {
      long long nx = dx[k] + x;
      long long ny = dy[k] + y;
      if (0 > nx || 0 > ny || nx >= h || ny >= w) continue;
      if (table[nx][ny] == 0) {
        table[nx][ny] = table[x][y] + 1;
        que.emplace(nx, ny);
      }
    }
  }
  for (long long _ = 0; _ < (long long)(t); _++) {
    long long i, j, p;
    cin >> i >> j >> p;
    i--;
    j--;
    if (table[i][j] > p) {
      cout << s[i][j] << "\n";
      continue;
    }
    if (table[i][j] == 0) {
      cout << s[i][j] << "\n";
      continue;
    }
    long long temp = s[i][j] - '0';
    if ((p - table[i][j] + 1) & 1) {
      cout << (temp ^ 1) << "\n";
    } else {
      cout << temp << "\n";
    }
  }
}
