#include<bits/stdc++.h>
using namespace std;

short int n,m;
const short int N=1000;
vector<vector<bool>> v(N,vector<bool>(N)),g(N,vector<bool>(N));

void good(){
    for(short int i=0;i<n;i++){
        for(short int j=0;j<m;j++){
            bool f=0;
            if(i     and v[i][j]==v[i-1][j])    f=1;
            if(j     and v[i][j]==v[i][j-1])    f=1;
            if(i<n-1 and v[i][j]==v[i+1][j])    f=1;
            if(j<m-1 and v[i][j]==v[i][j+1])    f=1;
            g[i][j]=f;
        }
    }
}
short int man_dist(int i,int j){
    queue<pair<pair<short int,short int>,short int>> q;
    map<pair<short int,short int>,bool> ma;
    q.push({{i,j},0});
    while(q.size()){
        short int r=q.front().first.first,c=q.front().first.second;
        short int d=q.front().second;
        q.pop();
        if(g[r][c]) return d;
        ma[{r,c}]=true;
        if(r     and !ma[{r-1,c}])    q.push({{r-1,c},d+1});
        if(c     and !ma[{r,c-1}])    q.push({{r,c-1},d+1});
        if(r<n-1 and !ma[{r+1,c}])    q.push({{r+1,c},d+1});
        if(c<m-1 and !ma[{r,c+1}])    q.push({{r,c+1},d+1});
    }
    return 0;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt,ii=0;
    short int k=0;
    cin>>n>>m>>tt;
    for(short int i=0;i<n;i++)
        for(short int j=0;j<m;j++){
            char x; cin>>x;
            v[i][j]=(x=='1');
        }
    good();

    while(ii++<tt)
    {
        short int i,j;
        long long p;  cin>>i>>j>>p;
        i--,j--;

        if(n==m and n==1){
            cout<<v[i][j]<<endl;
            continue;
        }

        short int f=man_dist(i,j);
        if(p<=f)
            cout<<v[i][j]<<endl;
        else
            cout<<(p-f+v[i][j])%2<<endl;
    }
    cerr<<"[Execution : "<<(1.0*clock())/CLOCKS_PER_SEC<<"s]\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const short int N=1000;
short int n,m,ax[]={0,0,1,-1},ay[]={1,-1,0,0},man_dis[N][N];
bool vis[N][N],v[N][N],g[N][N];

void good(){
    for(short int i=0;i<n;i++){
        for(short int j=0;j<m;j++){
            bool f=0;
            if(i     and v[i][j]==v[i-1][j])    f=1;
            if(j     and v[i][j]==v[i][j-1])    f=1;
            if(i<n-1 and v[i][j]==v[i+1][j])    f=1;
            if(j<m-1 and v[i][j]==v[i][j+1])    f=1;
            g[i][j]=f;
        }
    }
}

void man_dist(){
    queue<pair<short int,short int>> q;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(g[i][j])
                man_dis[i][j]=0,q.push({i,j}),vis[i][j]=true;
    while(q.size()){
        short int x=q.front().first,y=q.front().second;
        q.pop();
        for(int i=0;i<4;i++){
            int nx=x+ax[i],ny=y+ay[i];
            if(nx<0||ny<0||nx>=m||ny>=n||vis[ny][nx])
                continue;
            man_dis[ny][nx]=man_dis[y][x]+1;
            vis[ny][nx]=true;
            q.push({ny,nx});
        }
    }
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt,ii=0;
    short int k=0;
    cin>>n>>m>>tt;
    for(short int i=0;i<n;i++)
        for(short int j=0;j<m;j++){
            char x; cin>>x;
            v[i][j]=(x=='1');
        }
    good();
    man_dist();

    while(ii++<tt)
    {
        short int i,j;
        long long p;  cin>>i>>j>>p;
        i--,j--;

        if(n+m==2){
            cout<<v[i][j]<<endl;
            continue;
        }
        short int f=man_dis[i][j];
        if(p<=f)
            cout<<v[i][j]<<endl;
        else
            cout<<(p-f+v[i][j])%2<<endl;
    }
    cerr<<"[Execution : "<<(1.0*clock())/CLOCKS_PER_SEC<<"s]\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
const int N=1000;
vector<vector<bool>> v(N,vector<bool>(N)),g(N,vector<bool>(N));

void good(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int f=0;
            if(i     and v[i][j]==v[i-1][j])    f=1;
            if(j     and v[i][j]==v[i][j-1])    f=1;
            if(i<n-1 and v[i][j]==v[i+1][j])    f=1;
            if(j<m-1 and v[i][j]==v[i][j+1])    f=1;
            g[i][j]=f;
        }
    }
}
int man_dist(int i,int j){
    queue<pair<pair<int,int>,int>> q;
    map<pair<int,int>,bool> ma;
    q.push({{i,j},0});
    while(q.size()){
        int r=q.front().first.first,c=q.front().first.second;
        int d=q.front().second;
        q.pop();
        if(g[r][c]) return d;
        ma[{r,c}]=true;
        if(r     and !ma[{r-1,c}])    q.push({{r-1,c},d+1});
        if(c     and !ma[{r,c-1}])    q.push({{r,c-1},d+1});
        if(r<n-1 and !ma[{r+1,c}])    q.push({{r+1,c},d+1});
        if(c<m-1 and !ma[{r,c+1}])    q.push({{r,c+1},d+1});
    }
    return 0;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt,ii=0,k=0;
    cin>>n>>m>>tt;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            char x; cin>>x;
            v[i][j]=(x=='1');
        }
    good();

    while(ii++<tt)
    {
        short int i,j;
        long long p;  cin>>i>>j>>p;
        i--,j--;

        if(n==m and n==1){
            cout<<v[i][j]<<endl;
            continue;
        }

        int f=man_dist(i,j);
        if(p<=f)
            cout<<v[i][j]<<endl;
        else
            cout<<(p-f+v[i][j])%2<<endl;
    }
    cerr<<"[Execution : "<<(1.0*clock())/CLOCKS_PER_SEC<<"s]\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;

vector<vector<bool>> next(vector<vector<bool>> v){
    vector<vector<bool>> v1(n,vector<bool>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int f=0;
            if(i     and v[i][j]==v[i-1][j])    f=1;
            if(j     and v[i][j]==v[i][j-1])    f=1;
            if(i<n-1 and v[i][j]==v[i+1][j])    f=1;
            if(j<m-1 and v[i][j]==v[i][j+1])    f=1;
            if(f)       v1[i][j]=!v[i][j];
            else        v1[i][j]= v[i][j];
        }
    }
    return v1;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt,ii=0,k=0;
    cin>>n>>m>>tt;
    vector<vector<bool>> v(n,vector<bool>(m)),v1(n,vector<bool>(m));
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            char x; cin>>x;
            v[i][j]=(x=='1');
        }
    map<int,vector<vector<bool>>> mp;
    map<vector<vector<bool>>,int> hs;

    v1=v;
    while(!hs[v1]){
        hs[v1]=k+1;
        mp[k++]=v1;
        v1=next(v1);
    }
    int kk=hs[v1]-1;
    /*
    cout<<kk<<' '<<k<<endl;
    for(int l=0;l<k;l++){
        cout<<"k - "<<l<<endl;
        for(auto i:mp[l]){for(auto j:i)cout<<j;cout<<endl;}
    }
    */
    while(ii++<tt)
    {
        short int i,j;
        long long p;  cin>>i>>j>>p;
        if(p<k)
            cout<<mp[p][i-1][j-1]<<endl;
        else
            cout<<mp[kk+(p-k)%(k-kk)][i-1][j-1]<<endl;
    }
    cerr<<"[Execution : "<<(1.0*clock())/CLOCKS_PER_SEC<<"s]\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;*/

#define ll long long int
#define ld long double
#define le length()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mkp make_pair
#define ppl pair<ll,ll>
#define ppi pair<int,int>
#define ppli pair<ll,int>
#define mod 1000000007
#define fi first
#define se second
#define debug1(x) cout<<#x<<" = "<<x<<'\n';
#define debug2(x, y) cout << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define all(v) v.begin(),v.end()
#define mset(x) memset(x,0,sizeof(x))
#define sz size()

ll power(ll a,ll b)
{
    if(b==0)
    return 1;
    if(b==1)
    return a;
    ll r=power(a,b/2)%mod;
    if(b%2==0)
    return (r*r)%mod;
    else
    return (((r*r)%mod)*a)%mod;
}
#define pri 1000009
int prime[pri];
int sumprime[pri];
void sieve(int n)
{
    prime[0]=prime[1]=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(!prime[i])
        {
            for(int j=2*i;j<=n;j=j+i)
            {
                prime[j]++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!prime[i])
        {
            sumprime[i]=sumprime[i-1]+1;
        }
        else
        {
            sumprime[i]=sumprime[i-1];
        }
    }
}

bool comp(const pair<ll,ll> &a,
               const pair<ll,ll> &b)
{
       return (a.second < b.second);
}

const int N=5*1e5+9;
int n,m;
string s[1005];
vector<ppi> v;
bool vis[1005][1005];
int level[1005][1005],c;
bool funcond(int i,int j)
{
    if(i>0&&s[i][j]==s[i-1][j])
    {
        return true;
    }
    if(i<n-1&&s[i][j]==s[i+1][j])
    {
        return true;
    }
    if(j>0&&s[i][j]==s[i][j-1])
    {
        return true;
    }
    if(j<n-1&&s[i][j]==s[i][j+1])
    {
        return true;
    }
    return false;
}
bool valid(int i,int j)
{
    if(i>=0&&i<n&&j>=0&&j<n)
        return true;
    return false;
}

void dfs(int i,int j)
{
    queue<ppi> q;
    // mp[{i,j}]++;
    vis[i][j]=true;
    q.push({i,j});
    v.pb({i,j});
    // mp[{i,j}]++;
    level[i][j]=1;
    c++;
    while(!q.empty())
    {
        ppi p=q.front();
        int i1=p.fi;
        int j1=p.se;
        q.pop();
        int a[4]={0,1,-1,0};
        int b[4]={1,0,0,-1};
        for(int k=0;k<n;k++)
        {
            if(valid(i1+a[k],j1+b[k])&&s[i1][j1]==s[i1+a[k]][j1+b[k]])
            {
                if(!vis[i1+a[k]][j1+b[k]])
                {
                    vis[i1+a[k]][j1+b[k]]=true;
                    q.push({i1+a[k],j1+b[k]});
                    v.pb({i1+a[k],j1+b[k]});
                    level[i1+a[k]][j1+b[k]]=1;
                    // mp[{i1+a[k],j1+b[k]}]++;
                    c++;
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  /*  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int q;
    cin>>n>>m>>q;
    // string s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int flag=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            level[i][j]=INT_MAX;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(funcond(i,j))
            {
                // cout<<i<<" "<<j<<"\n";
                if(!vis[i][j])
                {
                    dfs(i,j);
                    // cout<<i<<" "<<j<<"\n";
                }
                flag++;
            }
        }
    }
    if(!flag)
    {
        while(q--)
        {
            int i,j,r;
            cin>>i>>j>>r;
            i--;
            j--;
            cout<<s[i][j]<<"\n";
        }
        return 0;
    }

    int ww=2;
    while(c<n*m)
    {
        int r=v.sz;
        vector<ppi> final;
        int a[4]={0,1,0,-1};
        int b[4]={1,0,-1,0};
        for(int i=0;i<r;i++)
        {
            int i1=v[i].fi;
            int j1=v[i].se;
            for(int j=0;j<4;j++)
            {
                if(valid(i1+a[j],j1+b[j]))
                {
                    if(!vis[i1+a[j]][j1+b[j]])
                    {

                        vis[i1+a[j]][j1+b[j]]=true;
                        final.pb({i1+a[j],j1+b[j]});
                        level[i1+a[j]][j1+b[j]]=ww;
                        c++;

                    }
                }
            }
        }
        ww++;
        if(!final.sz)
        {
            break;
        }
        v.clear();
        for(auto it:final)
        {
            v.pb(it);
        }
        // ww++;
    }

    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
    //         cout<<level[i][j]<<" ";
    //     }
    //     cout<<"\n";
    // }

    while(q--)
    {
        int i,j,r;
        cin>>i>>j>>r;
        i--;
        j--;
        // cout<<level[i][j]<<" ";
        if(level[i][j]>r)
        {
            cout<<s[i][j]<<"\n";
        }
        else
        {
            int ff=r-level[i][j];
            if(ff%2)
            {
                cout<<s[i][j]<<"\n";
            }
            else
            {
                // cout<<" masala ";
                if(s[i][j]=='1')
                {
                    cout<<"0\n";
                }
                else
                {
                    cout<<"1\n";
                }
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;*/

#define ll long long int
#define ld long double
#define le length()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mkp make_pair
#define ppl pair<ll,ll>
#define ppi pair<int,int>
#define ppli pair<ll,int>
#define mod 1000000007
#define fi first
#define se second
#define debug1(x) cout<<#x<<" = "<<x<<'\n';
#define debug2(x, y) cout << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define all(v) v.begin(),v.end()
#define mset(x) memset(x,0,sizeof(x))
#define sz size()

ll power(ll a,ll b)
{
    if(b==0)
    return 1;
    if(b==1)
    return a;
    ll r=power(a,b/2)%mod;
    if(b%2==0)
    return (r*r)%mod;
    else
    return (((r*r)%mod)*a)%mod;
}
#define pri 1000009
int prime[pri];
int sumprime[pri];
void sieve(int n)
{
    prime[0]=prime[1]=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(!prime[i])
        {
            for(int j=2*i;j<=n;j=j+i)
            {
                prime[j]++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!prime[i])
        {
            sumprime[i]=sumprime[i-1]+1;
        }
        else
        {
            sumprime[i]=sumprime[i-1];
        }
    }
}

bool comp(const pair<ll,ll> &a,
               const pair<ll,ll> &b)
{
       return (a.second < b.second);
}

const int N=5*1e5+9;
int n,m;
string s[1005];
vector<ppi> v;
bool vis[1005][1005];
int level[1005][1005],c;
bool funcond(int i,int j)
{
    if(i>0&&s[i][j]==s[i-1][j])
    {
        return true;
    }
    if(i<n-1&&s[i][j]==s[i+1][j])
    {
        return true;
    }
    if(j>0&&s[i][j]==s[i][j-1])
    {
        return true;
    }
    if(j<m-1&&s[i][j]==s[i][j+1])
    {
        return true;
    }
    return false;
}
bool valid(int i,int j)
{
    if(i>=0&&i<n&&j>=0&&j<m)
        return true;
    return false;
}

void dfs(int i,int j)
{
    queue<ppi> q;
    // mp[{i,j}]++;
    vis[i][j]=true;
    q.push({i,j});
    v.pb({i,j});
    // mp[{i,j}]++;
    level[i][j]=1;
    c++;
    while(!q.empty())
    {
        ppi p=q.front();
        int i1=p.fi;
        int j1=p.se;
        q.pop();
        int a[4]={0,1,-1,0};
        int b[4]={1,0,0,-1};
        for(int k=0;k<n;k++)
        {
            if(valid(i1+a[k],j1+b[k]))
            {
                if(s[i1][j1]==s[i1+a[k]][j1+b[k]])
                {
                if(!vis[i1+a[k]][j1+b[k]])
                {
                    vis[i1+a[k]][j1+b[k]]=true;
                    q.push({i1+a[k],j1+b[k]});
                    v.pb({i1+a[k],j1+b[k]});
                    level[i1+a[k]][j1+b[k]]=1;
                    // mp[{i1+a[k],j1+b[k]}]++;
                    c++;
                }
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  /*  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int q;
    cin>>n>>m>>q;
    // string s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int flag=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            level[i][j]=INT_MAX;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(funcond(i,j))
            {
                // cout<<i<<" "<<j<<"\n";
                if(!vis[i][j])
                {
                    dfs(i,j);
                    // cout<<i<<" "<<j<<"\n";
                }
                flag++;
            }
        }
    }
    if(!flag)
    {
        while(q--)
        {
            int i,j,r;
            cin>>i>>j>>r;
            i--;
            j--;
            cout<<s[i][j]<<"\n";
        }
        return 0;
    }

    int ww=2;
    while(c<n*m)
    {
        int r=v.sz;
        vector<ppi> final;
        int a[4]={0,1,0,-1};
        int b[4]={1,0,-1,0};
        for(int i=0;i<r;i++)
        {
            int i1=v[i].fi;
            int j1=v[i].se;
            for(int j=0;j<4;j++)
            {
                if(valid(i1+a[j],j1+b[j]))
                {
                    if(!vis[i1+a[j]][j1+b[j]])
                    {

                        vis[i1+a[j]][j1+b[j]]=true;
                        final.pb({i1+a[j],j1+b[j]});
                        level[i1+a[j]][j1+b[j]]=ww;
                        c++;

                    }
                }
            }
        }
        ww++;
        if(!final.sz)
        {
            break;
        }
        v.clear();
        for(auto it:final)
        {
            v.pb(it);
        }
        // ww++;
    }

    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
    //         cout<<level[i][j]<<" ";
    //     }
    //     cout<<"\n";
    // }

    while(q--)
    {
        int i,j,r;
        cin>>i>>j>>r;
        i--;
        j--;
        // cout<<level[i][j]<<" ";
        if(level[i][j]>r)
        {
            cout<<s[i][j]<<"\n";
        }
        else
        {
            int ff=r-level[i][j];
            if(ff%2)
            {
                cout<<s[i][j]<<"\n";
            }
            else
            {
                // cout<<" masala ";
                if(s[i][j]=='1')
                {
                    cout<<"0\n";
                }
                else
                {
                    cout<<"1\n";
                }
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;*/

#define ll long long int
#define ld long double
#define le length()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mkp make_pair
#define ppl pair<ll,ll>
#define ppi pair<int,int>
#define ppli pair<ll,int>
#define mod 1000000007
#define fi first
#define se second
#define debug1(x) cout<<#x<<" = "<<x<<'\n';
#define debug2(x, y) cout << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define all(v) v.begin(),v.end()
#define mset(x) memset(x,0,sizeof(x))
#define sz size()

ll power(ll a,ll b)
{
    if(b==0)
    return 1;
    if(b==1)
    return a;
    ll r=power(a,b/2)%mod;
    if(b%2==0)
    return (r*r)%mod;
    else
    return (((r*r)%mod)*a)%mod;
}
#define pri 1000009
int prime[pri];
int sumprime[pri];
void sieve(int n)
{
    prime[0]=prime[1]=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(!prime[i])
        {
            for(int j=2*i;j<=n;j=j+i)
            {
                prime[j]++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!prime[i])
        {
            sumprime[i]=sumprime[i-1]+1;
        }
        else
        {
            sumprime[i]=sumprime[i-1];
        }
    }
}

bool comp(const pair<ll,ll> &a,
               const pair<ll,ll> &b)
{
       return (a.second < b.second);
}

const int N=5*1e5+9;
int n,m;
string s[1005];
vector<ppi> v;
bool vis[1005][1005];
int level[1005][1005],c;
bool funcond(int i,int j)
{
    if(i>0&&s[i][j]==s[i-1][j])
    {
        return true;
    }
    if(i<n-1&&s[i][j]==s[i+1][j])
    {
        return true;
    }
    if(j>0&&s[i][j]==s[i][j-1])
    {
        return true;
    }
    if(j<m-1&&s[i][j]==s[i][j+1])
    {
        return true;
    }
    return false;
}
bool valid(int i,int j)
{
    if(i>=0&&i<n&&j>=0&&j<m)
        return true;
    return false;
}

void dfs(int i,int j)
{
    queue<ppi> q;
    // mp[{i,j}]++;
    vis[i][j]=true;
    q.push({i,j});
    v.pb({i,j});
    // mp[{i,j}]++;
    level[i][j]=1;
    c++;
    while(!q.empty())
    {
        ppi p=q.front();
        int i1=p.fi;
        int j1=p.se;
        q.pop();
        int a[4]={0,1,-1,0};
        int b[4]={1,0,0,-1};
        for(int k=0;k<4;k++)
        {
            if(valid(i1+a[k],j1+b[k]))
            {
                if(s[i1][j1]==s[i1+a[k]][j1+b[k]])
                {
                if(!vis[i1+a[k]][j1+b[k]])
                {
                    vis[i1+a[k]][j1+b[k]]=true;
                    q.push({i1+a[k],j1+b[k]});
                    v.pb({i1+a[k],j1+b[k]});
                    level[i1+a[k]][j1+b[k]]=1;
                    // mp[{i1+a[k],j1+b[k]}]++;
                    c++;
                }
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  /*  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int q;
    cin>>n>>m>>q;
    // string s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int flag=0;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(funcond(i,j))
            {
                // cout<<i<<" "<<j<<"\n";
                if(!vis[i][j])
                {
                    dfs(i,j);
                    // cout<<i<<" "<<j<<"\n";
                }
                flag++;
            }
        }
    }
    if(!flag)
    {
        while(q--)
        {
            int i,j,r;
            cin>>i>>j>>r;
            i--;
            j--;
            cout<<s[i][j]<<"\n";
        }
        return 0;
    }

    int ww=2;
   vector<ppi> final;
    while(c<n*m)
    {
        int r=v.sz;
        // vector<ppi> final;
        int a[4]={0,1,0,-1};
        int b[4]={1,0,-1,0};
        for(int i=0;i<r;i++)
        {
            int i1=v[i].fi;
            int j1=v[i].se;
            for(int j=0;j<4;j++)
            {
                if(valid(i1+a[j],j1+b[j]))
                {
                    if(!vis[i1+a[j]][j1+b[j]])
                    {

                        vis[i1+a[j]][j1+b[j]]=true;
                        final.pb({i1+a[j],j1+b[j]});
                        level[i1+a[j]][j1+b[j]]=ww;
                        c++;

                    }
                }
            }
        }
        ww++;
        if(!final.sz)
        {
            break;
        }
        v.clear();
        for(auto it:final)
        {
            v.pb(it);
        }
        final.clear();
    }

    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
    //         cout<<level[i][j]<<" ";
    //     }
    //     cout<<"\n";
    // }

    while(q--)
    {
        int i,j,r;
        cin>>i>>j>>r;
        i--;
        j--;
        // cout<<level[i][j]<<" ";
        if(level[i][j]>r)
        {
            cout<<s[i][j]<<"\n";
        }
        else
        {
            int ff=r-level[i][j];
            if(ff%2)
            {
                cout<<s[i][j]<<"\n";
            }
            else
            {
                if(s[i][j]=='1')
                {
                    cout<<"0\n";
                }
                else
                {
                    cout<<"1\n";
                }
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;*/

#define ll long long int
#define ld long double
#define le length()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mkp make_pair
#define ppl pair<ll,ll>
#define ppi pair<int,int>
#define ppli pair<ll,int>
#define mod 1000000007
#define fi first
#define se second
#define debug1(x) cout<<#x<<" = "<<x<<'\n';
#define debug2(x, y) cout << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define all(v) v.begin(),v.end()
#define mset(x) memset(x,0,sizeof(x))
#define sz size()

ll power(ll a,ll b)
{
    if(b==0)
    return 1;
    if(b==1)
    return a;
    ll r=power(a,b/2)%mod;
    if(b%2==0)
    return (r*r)%mod;
    else
    return (((r*r)%mod)*a)%mod;
}
#define pri 1000009
int prime[pri];
int sumprime[pri];
void sieve(int n)
{
    prime[0]=prime[1]=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(!prime[i])
        {
            for(int j=2*i;j<=n;j=j+i)
            {
                prime[j]++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!prime[i])
        {
            sumprime[i]=sumprime[i-1]+1;
        }
        else
        {
            sumprime[i]=sumprime[i-1];
        }
    }
}

bool comp(const pair<ll,ll> &a,
               const pair<ll,ll> &b)
{
       return (a.second < b.second);
}

const int N=5*1e5+9;
ll n,m;
string s[1005];
vector<ppi> v;
bool vis[1005][1005];
ll level[1005][1005],c;
bool funcond(ll i,ll j)
{
    if(i>0&&s[i][j]==s[i-1][j])
    {
        return true;
    }
    if(i<n-1&&s[i][j]==s[i+1][j])
    {
        return true;
    }
    if(j>0&&s[i][j]==s[i][j-1])
    {
        return true;
    }
    if(j<m-1&&s[i][j]==s[i][j+1])
    {
        return true;
    }
    return false;
}
bool valid(int i,int j)
{
    if(i>=0&&i<n&&j>=0&&j<m)
        return true;
    return false;
}

void dfs(int i,int j)
{
    queue<ppi> q;
    // mp[{i,j}]++;
    vis[i][j]=true;
    q.push({i,j});
    v.pb({i,j});
    // mp[{i,j}]++;
    level[i][j]=1;
    c++;
    while(!q.empty())
    {
        ppi p=q.front();
        int i1=p.fi;
        int j1=p.se;
        q.pop();
        int a[4]={0,1,-1,0};
        int b[4]={1,0,0,-1};
        for(int k=0;k<4;k++)
        {
            if(valid(i1+a[k],j1+b[k]))
            {
                if(s[i1][j1]==s[i1+a[k]][j1+b[k]])
                {
                if(!vis[i1+a[k]][j1+b[k]])
                {
                    vis[i1+a[k]][j1+b[k]]=true;
                    q.push({i1+a[k],j1+b[k]});
                    v.pb({i1+a[k],j1+b[k]});
                    level[i1+a[k]][j1+b[k]]=1;
                    // mp[{i1+a[k],j1+b[k]}]++;
                    c++;
                }
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  /*  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int q;
    cin>>n>>m>>q;
    // string s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int flag=0;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(funcond(i,j))
            {
                // cout<<i<<" "<<j<<"\n";
                if(!vis[i][j])
                {
                    dfs(i,j);
                    // cout<<i<<" "<<j<<"\n";
                }
                flag++;
            }
        }
    }
    if(!flag)
    {
        while(q--)
        {
            ll i,j,r;
            cin>>i>>j>>r;
            i--;
            j--;
            cout<<s[i][j]<<"\n";
        }
        return 0;
    }

    int ww=2;
   vector<ppi> final;
    while(c<n*m)
    {
        ll r=v.sz;
        // vector<ppi> final;
        ll a[4]={0,1,0,-1};
        ll b[4]={1,0,-1,0};
        for(int i=0;i<r;i++)
        {
            ll i1=v[i].fi;
            ll j1=v[i].se;
            for(int j=0;j<4;j++)
            {
                if(valid(i1+a[j],j1+b[j]))
                {
                    if(!vis[i1+a[j]][j1+b[j]])
                    {

                        vis[i1+a[j]][j1+b[j]]=true;
                        final.pb({i1+a[j],j1+b[j]});
                        level[i1+a[j]][j1+b[j]]=ww;
                        c++;

                    }
                }
            }
        }
        ww++;
        if(!final.sz)
        {
            break;
        }
        v.clear();
        for(auto it:final)
        {
            v.pb(it);
        }
        final.clear();
    }

    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
    //         cout<<level[i][j]<<" ";
    //     }
    //     cout<<"\n";
    // }

    while(q--)
    {
        ll i,j,r;
        cin>>i>>j>>r;
        i--;
        j--;
        // cout<<level[i][j]<<" ";
        if(level[i][j]>r)
        {
            cout<<s[i][j]<<"\n";
        }
        else
        {
            ll ff=r-level[i][j];
            if(ff%2)
            {
                cout<<s[i][j]<<"\n";
            }
            else
            {
                if(s[i][j]=='1')
                {
                    cout<<"0\n";
                }
                else
                {
                    cout<<"1\n";
                }
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;*/

#define ll long long int
#define ld long double
#define le length()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mkp make_pair
#define ppl pair<ll,ll>
#define ppi pair<int,int>
#define ppli pair<ll,int>
#define mod 1000000007
#define fi first
#define se second
#define debug1(x) cout<<#x<<" = "<<x<<'\n';
#define debug2(x, y) cout << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define all(v) v.begin(),v.end()
#define mset(x) memset(x,0,sizeof(x))
#define sz size()

ll power(ll a,ll b)
{
    if(b==0)
    return 1;
    if(b==1)
    return a;
    ll r=power(a,b/2)%mod;
    if(b%2==0)
    return (r*r)%mod;
    else
    return (((r*r)%mod)*a)%mod;
}
#define pri 1000009
int prime[pri];
int sumprime[pri];
void sieve(int n)
{
    prime[0]=prime[1]=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(!prime[i])
        {
            for(int j=2*i;j<=n;j=j+i)
            {
                prime[j]++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!prime[i])
        {
            sumprime[i]=sumprime[i-1]+1;
        }
        else
        {
            sumprime[i]=sumprime[i-1];
        }
    }
}

bool comp(const pair<ll,ll> &a,
               const pair<ll,ll> &b)
{
       return (a.second < b.second);
}

const int N=5*1e5+9;
int n,m;
string s[1005];
vector<ppi> v;
bool vis[1005][1005];
int level[1005][1005],c;
bool funcond(int i,int j)
{
    if(i>0&&s[i][j]==s[i-1][j])
    {
        return true;
    }
    if(i<n-1&&s[i][j]==s[i+1][j])
    {
        return true;
    }
    if(j>0&&s[i][j]==s[i][j-1])
    {
        return true;
    }
    if(j<m-1&&s[i][j]==s[i][j+1])
    {
        return true;
    }
    return false;
}
bool valid(int i,int j)
{
    if(i>=0&&i<n&&j>=0&&j<m)
        return true;
    return false;
}

void dfs(int i,int j)
{
    queue<ppi> q;
    // mp[{i,j}]++;
    vis[i][j]=true;
    q.push({i,j});
    v.pb({i,j});
    // mp[{i,j}]++;
    level[i][j]=1;
    c++;
    while(!q.empty())
    {
        ppi p=q.front();
        int i1=p.fi;
        int j1=p.se;
        q.pop();
        int a[4]={0,1,-1,0};
        int b[4]={1,0,0,-1};
        for(int k=0;k<4;k++)
        {
            if(valid(i1+a[k],j1+b[k]))
            {
                if(s[i1][j1]==s[i1+a[k]][j1+b[k]])
                {
                if(!vis[i1+a[k]][j1+b[k]])
                {
                    vis[i1+a[k]][j1+b[k]]=true;
                    q.push({i1+a[k],j1+b[k]});
                    v.pb({i1+a[k],j1+b[k]});
                    level[i1+a[k]][j1+b[k]]=1;
                    // mp[{i1+a[k],j1+b[k]}]++;
                    c++;
                }
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  /*  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int q;
    cin>>n>>m>>q;
    // string s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int flag=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            level[i][j]=INT_MAX;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(funcond(i,j))
            {
                // cout<<i<<" "<<j<<"\n";
                if(!vis[i][j])
                {
                    dfs(i,j);
                    // cout<<i<<" "<<j<<"\n";
                }
                flag++;
            }
        }
    }
    if(!flag)
    {
        while(q--)
        {
            int i,j,r;
            cin>>i>>j>>r;
            i--;
            j--;
            cout<<s[i][j]<<"\n";
        }
        return 0;
    }

    int ww=2;
    while(c<n*m)
    {
        int r=v.sz;
        vector<ppi> final;
        int a[4]={0,1,0,-1};
        int b[4]={1,0,-1,0};
        for(int i=0;i<r;i++)
        {
            int i1=v[i].fi;
            int j1=v[i].se;
            for(int j=0;j<4;j++)
            {
                if(valid(i1+a[j],j1+b[j]))
                {
                    if(!vis[i1+a[j]][j1+b[j]])
                    {

                        vis[i1+a[j]][j1+b[j]]=true;
                        final.pb({i1+a[j],j1+b[j]});
                        level[i1+a[j]][j1+b[j]]=ww;
                        c++;

                    }
                }
            }
        }
        ww++;
        if(!final.sz)
        {
            break;
        }
        v.clear();
        for(auto it:final)
        {
            v.pb(it);
        }
        // ww++;
    }

    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
    //         cout<<level[i][j]<<" ";
    //     }
    //     cout<<"\n";
    // }

    while(q--)
    {
        int i,j,r;
        cin>>i>>j>>r;
        i--;
        j--;
        // cout<<level[i][j]<<" ";
        if(level[i][j]>r)
        {
            cout<<s[i][j]<<"\n";
        }
        else
        {
            int ff=r-level[i][j];
            if(ff%2)
            {
                cout<<s[i][j]<<"\n";
            }
            else
            {
                // cout<<" masala ";
                if(s[i][j]=='1')
                {
                    cout<<"0\n";
                }
                else
                {
                    cout<<"1\n";
                }
            }
        }
    }

    return 0;
}


/*************************************************************************
  > File Name: e.cpp
  > Author: Samuel
  > Mail: enminghuang21119@gmail.com
  > Created Time: Mon May 18 18:18:34 2020
*************************************************************************/

#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

bool in[1000][1000];
int vis[1000][1000];
int n, m, k;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool same(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || ny < 0 | nx >= n || ny >= m)
            continue;
        if (in[x][y] == in[nx][ny])
            return 1;
    }
    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            char a;
            cin >> a;
            in[i][j] = a == '1';
        }
    queue<pii> que;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (same(i, j)) {
                vis[i][j] = 1;
                que.push({i, j});
            }
        }
    while (!que.empty()) {
        pii cur = que.front();
        que.pop();
        int x = cur.first;
        int y = cur.second;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || ny < 0 | nx >= n || ny >= m || vis[nx][ny])
                continue;
            que.push({nx, ny});
            vis[nx][ny] = vis[x][y] + 1;
        }
    }
    while (k--) {
        int x, y;
        ll q;
        cin >> x >> y >> q;
        x--, y--;
        ll tmp = 0;
        if (vis[x][y])
            tmp = max(0LL, q - (vis[x][y] - 1));
        //cout << vis[x][y] << ' ';
        cout << (in[x][y] ^ (tmp & 1)) << '\n';
    }
    return 0;
}


/*************************************************************************
  > File Name: e.cpp
  > Author: Samuel
  > Mail: enminghuang21119@gmail.com
  > Created Time: Mon May 18 18:18:34 2020
*************************************************************************/

#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

bool in[1000][1000];
int vis[1000][1000];
int n, m, k;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool same(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || ny < 0 | nx >= n || ny >= m)
            continue;
        if (in[x][y] == in[nx][ny])
            return 1;
    }
    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            char a;
            cin >> a;
            in[i][j] = a == '1';
        }
    queue<pii> que;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (same(i, j)) {
                vis[i][j] = 1;
                que.push({i, j});
            }
        }
    while (!que.empty()) {
        pii cur = que.front();
        que.pop();
        int x = cur.first;
        int y = cur.second;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || ny < 0 | nx >= n || ny >= m || vis[nx][ny])
                continue;
            que.push({nx, ny});
            vis[nx][ny] = vis[x][y] + 1;
        }
    }
    while (k--) {
        int x, y;
        ll q;
        cin >> x >> y >> q;
        x--, y--;
        int tmp = 0;
        if (vis[x][y])
            tmp = max(0LL, q - (vis[x][y] - 1));
        assert(tmp >= 0);
        //cout << vis[x][y] << ' ';
        cout << (in[x][y] ^ (tmp & 1)) << '\n';
    }
    return 0;
}


/*************************************************************************
  > File Name: e.cpp
  > Author: Samuel
  > Mail: enminghuang21119@gmail.com
  > Created Time: Mon May 18 18:18:34 2020
*************************************************************************/

#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

bool in[1000][1000];
int vis[1000][1000];
int n, m, k;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool same(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || ny < 0 | nx >= n || ny >= m)
            continue;
        if (in[x][y] == in[nx][ny])
            return 1;
    }
    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            char a;
            cin >> a;
            in[i][j] = a == '1';
        }
    queue<pii> que;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (same(i, j)) {
                vis[i][j] = 1;
                que.push({i, j});
            }
        }
    while (!que.empty()) {
        pii cur = que.front();
        que.pop();
        int x = cur.first;
        int y = cur.second;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || ny < 0 | nx >= n || ny >= m || vis[nx][ny])
                continue;
            que.push({nx, ny});
            vis[nx][ny] = vis[x][y] + 1;
        }
    }
    while (k--) {
        int x, y;
        ll q;
        cin >> x >> y >> q;
        x--, y--;
        int tmp = 0;
        if (vis[x][y])
            tmp = max(0LL, q - (vis[x][y] - 1));
        //cout << vis[x][y] << ' ';
        cout << (in[x][y] ^ (tmp % 2)) << '\n';
    }
    return 0;
}


/*************************************************************************
  > File Name: e.cpp
  > Author: Samuel
  > Mail: enminghuang21119@gmail.com
  > Created Time: Mon May 18 18:18:34 2020
*************************************************************************/

#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

bool in[1000][1000];
int vis[1000][1000];
int n, m, k;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool same(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || ny < 0 | nx >= n || ny >= m)
            continue;
        if (in[x][y] == in[nx][ny])
            return 1;
    }
    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            char a;
            cin >> a;
            in[i][j] = a == '1';
        }
    queue<pii> que;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (same(i, j)) {
                vis[i][j] = 1;
                que.push({i, j});
            }
        }
    while (!que.empty()) {
        pii cur = que.front();
        que.pop();
        int x = cur.first;
        int y = cur.second;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || ny < 0 | nx >= n || ny >= m || vis[nx][ny])
                continue;
            que.push({nx, ny});
            vis[nx][ny] = vis[x][y] + 1;
        }
    }
    while (k--) {
        int x, y;
        ll q;
        cin >> x >> y >> q;
        x--, y--;
        int tmp = 0;
        if (vis[x][y])
            tmp = max(0LL, q - (vis[x][y] - 1));
        if (tmp < 0)
            tmp = 0;
        //cout << vis[x][y] << ' ';
        cout << (in[x][y] ^ (tmp & 1)) << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], idx[N][N], sz[N*N], d[N*N], par[N*N], col[N*N];
string g[N];

void dfs(int x, int y, int t) {
  vis[x][y] = 1;
  idx[x][y] = t;
  sz[t]++;
  col[t] = g[x][y] - '0';
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && !vis[cx][cy] && g[x][y] == g[cx][cy]) {
      dfs(cx, cy, t);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) cin >> g[i];
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j]) {
    dfs(i, j, k++);
  }
  memset(d, 0x3f, sizeof(d));
  memset(par, -1, sizeof(par));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
    if (sz[idx[i][j]] > 1) {
      q.push(make_pair(i, j));
      d[idx[i][j]] = 0;
      par[idx[i][j]] = idx[i][j];
    }
  }
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        int u = idx[p.first][p.second], v = idx[nx][ny];
        if (d[v] == INF) {
          d[v] = d[u] + 1;
          par[v] = par[u];
          q.push(make_pair(nx, ny));
        } else if (d[v] == d[u] + 1) {
          if (par[v] != par[u] && col[par[v]] != col[par[u]]) {
            if (d[v] % 2 == (col[v] ^ col[par[u]])) par[v] = par[u];
          }
        }
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    int id = idx[x-1][y-1];
    if (d[id] == INF) {
      printf("%d\n", col[id]);
    } else if (p <= d[id]) {
      printf("%d\n", col[id]);
    } else {
      if ((col[id] ^ col[par[id]]) == d[id] % 2) {
        printf("%d\n", col[id] ^ ((p - d[id]) % 2));
      } else {
        printf("%d\n", col[id] ^ ((p - d[id] - 1) % 2));
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], idx[N][N], sz[N*N], d[N*N], par[N*N], col[N*N];
string g[N];

void dfs(int x, int y, int t) {
  vis[x][y] = 1;
  idx[x][y] = t;
  sz[t]++;
  col[t] = g[x][y] - '0';
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && !vis[cx][cy] && g[x][y] == g[cx][cy]) {
      dfs(cx, cy, t);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) cin >> g[i];
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j]) {
    dfs(i, j, k++);
  }
  memset(d, 0x3f, sizeof(d));
  memset(par, -1, sizeof(par));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    if (sz[idx[i][j]] > 1) {
      q.push(make_pair(i, j));
      d[idx[i][j]] = 0;
      par[idx[i][j]] = idx[i][j];
    }
  }
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        int u = idx[p.first][p.second], v = idx[nx][ny];
        if (d[v] == INF) {
          d[v] = d[u] + 1;
          par[v] = par[u];
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    int id = idx[x-1][y-1];
    if (d[id] == INF) {
      printf("%d\n", col[id]);
    } else if (p <= d[id]) {
      printf("%d\n", col[id]);
    } else {
      printf("%d\n", col[par[id]] ^ (p % 2));
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], idx[N][N], sz[N*N], d[N*N], par[N*N], col[N*N];
string g[N];

void dfs(int x, int y, int t) {
  vis[x][y] = 1;
  idx[x][y] = t;
  sz[t]++;
  col[t] = g[x][y] - '0';
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && !vis[cx][cy] && g[x][y] == g[cx][cy]) {
      dfs(cx, cy, t);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) cin >> g[i];
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j]) {
    dfs(i, j, k++);
  }
  memset(d, 0x3f, sizeof(d));
  memset(par, -1, sizeof(par));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
    if (sz[idx[i][j]] > 1) {
      q.push(make_pair(i, j));
      d[idx[i][j]] = 0;
      par[idx[i][j]] = idx[i][j];
    }
  }
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        int u = idx[p.first][p.second], v = idx[nx][ny];
        if (d[v] == INF) {
          d[v] = d[u] + 1;
          par[v] = par[u];
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    int id = idx[x-1][y-1];
    if (d[id] == INF) {
      printf("%d\n", col[id]);
    } else if (p <= d[id]) {
      printf("%d\n", col[id]);
    } else {
      printf("%d\n", col[par[id]] ^ (p % 2));
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], d[N][N], col[N][N];

bool check(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && col[x][y] == col[cx][cy]) {
      return true;
    }
  }
  return false;
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    for (int j = 0; j < m; j++) col[i][j] = s[j] - '0';
  }
  memset(d, 0x3f, sizeof(d));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    if (check(i, j)) {
      q.push(make_pair(i, j));
      d[i][j] = 0;
      vis[i][j] = 1;
    }
  }
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m && !vis[nx][ny]) {
        d[nx][ny] = d[p.first][p.second] + 1;
        vis[nx][ny] = 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    x--; y--;
    if (p <= d[x][y]) {
      printf("%d\n", col[x][y]);
    } else {
      printf("%d\n", col[x][y] ^ ((p - d[x][y]) % 2));
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], d[N][N], col[N][N];

bool check(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && col[x][y] == col[cx][cy]) {
      return true;
    }
  }
  return false;
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    for (int j = 0; j < m; j++) col[i][j] = s[j] - '0';
  }
  memset(d, 0x3f, sizeof(d));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    if (check(i, j)) {
      q.push(make_pair(i, j));
      d[i][j] = 0;
      vis[i][j] = 1;
    }
  }
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m && !vis[nx][ny]) {
        d[nx][ny] = d[p.first][p.second] + 1;
        vis[nx][ny] = 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    x--; y--;
    if (!vis[x][y] || p <= d[x][y]) {
      printf("%d\n", col[x][y]);
    } else {
      printf("%d\n", col[x][y] ^ ((p - d[x][y]) % 2));
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], idx[N][N], sz[N*N], d[N*N], par[N*N], col[N*N];
string g[N];
vector<set<int> > es;

void dfs(int x, int y, int t) {
  vis[x][y] = 1;
  idx[x][y] = t;
  sz[t]++;
  col[t] = g[x][y] - '0';
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && !vis[cx][cy] && g[x][y] == g[cx][cy]) {
      dfs(cx, cy, t);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) cin >> g[i];
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j]) {
    dfs(i, j, k++);
  }
  es.resize(k);
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    for (int d = 0; d < 4; d++) {
      int cx = i + dx[d], cy = j + dy[d];
      if (0 <= cx && cx < n && 0 <= cy && cy < m && idx[i][j] != idx[cx][cy]) {
        es[idx[i][j]].insert(idx[cx][cy]);
      }
    }
  }
  memset(d, 0x3f, sizeof(d));
  memset(par, -1, sizeof(par));
  queue<int> q;
  for (int i = 0; i < k; i++) {
    if (sz[i] > 1) {
      q.push(i);
      d[i] = 0;
      par[i] = i;
    }
  }
  while (!q.empty()) {
    int u = q.front(); q.pop();
    for (auto v : es[u]) {
      if (d[v] == INF) {
        d[v] = d[u] + 1;
        par[v] = par[u];
        q.push(v);
      } else if (d[v] == d[u] + 1) {
        if (par[v] != par[u] && col[par[v]] != col[par[u]]) {
          if (d[v] % 2 == (col[v] ^ col[par[u]])) par[v] = par[u];
        }
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    int id = idx[x-1][y-1];
    if (d[id] == INF) {
      printf("%d\n", col[id]);
    } else if (p <= d[id]) {
      printf("%d\n", col[id]);
    } else {
      if ((col[id] ^ col[par[id]]) == d[id] % 2) {
        printf("%d\n", col[id] ^ ((p - d[id]) % 2));
      } else {
        printf("%d\n", col[id] ^ ((p - d[id] - 1) % 2));
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], idx[N][N], sz[N*N], d[N*N], par[N*N], col[N*N];
string g[N];
vector<set<int> > es;

void dfs(int x, int y, int t) {
  vis[x][y] = 1;
  idx[x][y] = t;
  sz[t]++;
  col[t] = g[x][y] - '0';
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && !vis[cx][cy] && g[x][y] == g[cx][cy]) {
      dfs(cx, cy, t);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) cin >> g[i];
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j]) {
    dfs(i, j, k++);
  }
  es.resize(k);
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    for (int d = 0; d < 4; d++) {
      int cx = i + dx[d], cy = j + dy[d];
      if (0 <= cx && cx < n && 0 <= cy && cy < m && idx[i][j] != idx[cx][cy]) {
        es[idx[i][j]].insert(idx[cx][cy]);
      }
    }
  }
  memset(d, 0x3f, sizeof(d));
  memset(par, -1, sizeof(par));
  queue<int> q;
  for (int i = 0; i < k; i++) {
    if (sz[i] > 1) {
      q.push(i);
      d[i] = 0;
      par[i] = i;
    }
  }
  while (!q.empty()) {
    int u = q.front(); q.pop();
    for (auto v : es[u]) {
      if (d[v] == INF) {
        d[v] = d[u] + 1;
        par[v] = par[u];
        q.push(v);
      } else if (d[v] == d[u] + 1) {
        if (par[v] != par[u] && col[par[v]] != col[par[u]]) {
          if (d[v] % 2 == (col[v] ^ col[par[u]])) par[v] = par[u];
        }
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y, p;
    scanf("%d%d%d", &x, &y, &p);
    int id = idx[x-1][y-1];
    if (d[id] == INF) {
      printf("%d\n", col[id]);
    } else if (p <= d[id]) {
      printf("%d\n", col[id]);
    } else {
      if ((col[id] ^ col[par[id]]) == d[id] % 2) {
        printf("%d\n", col[id] ^ ((p - d[id]) % 2));
      } else {
        printf("%d\n", col[id] ^ ((p - d[id] - 1) % 2));
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

const int N = 1e3 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t, k, vis[N][N], d[N][N], col[N][N];

bool check(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int cx = x + dx[i], cy = y + dy[i];
    if (0 <= cx && cx < n && 0 <= cy && cy < m && col[x][y] == col[cx][cy]) {
      return true;
    }
  }
  return false;
}

int main() {
  scanf("%d%d%d", &n, &m, &t);
  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    for (int j = 0; j < m; j++) col[i][j] = s[j] - '0';
  }
  memset(d, 0x3f, sizeof(d));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    if (check(i, j)) {
      printf("%d %d\n", i, j);
      q.push(make_pair(i, j));
      d[i][j] = 0;
      vis[i][j] = 1;
    }
  }
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m && !vis[nx][ny]) {
        d[nx][ny] = d[p.first][p.second] + 1;
        vis[nx][ny] = 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  for (int _ = 0; _ < t; _++) {
    int x, y;
    ll p;
    scanf("%d%d%lld", &x, &y, &p);
    x--; y--;
    if (!vis[x][y] || p <= d[x][y]) {
      printf("%d\n", col[x][y]);
    } else {
      printf("%d\n", col[x][y] ^ ((p - d[x][y]) % 2));
    }
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }

typedef long long ll;
typedef long double ld;

#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"

#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

#define sz(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
#define ms(x,a) memset(x,a,sizeof(x))

#define PI 3.141592653589793238462643383279502884
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define mod 1000000007
#define MAXN 1010

ll T,N,M,K;
char v[MAXN][MAXN];
int tempo[MAXN][MAXN];
bool vis[MAXN][MAXN];

bool posValid(int x , int y){

    if(x >= 0 && x < N && y>=0 && y<M) return true;
    return false;
}

void dfs(){

    priority_queue<pair<int,pii>> pq;

    FOR(i,0,N){
        FOR(j,0,M){
            int cont = 0;

            if(posValid(i-1,j)) if(v[i-1][j] == v[i][j]) cont++;
            if(posValid(i+1,j)) if(v[i+1][j] == v[i][j]) cont++;
            if(posValid(i,j-1)) if(v[i][j-1] == v[i][j]) cont++;
            if(posValid(i,j+1)) if(v[i][j+1] == v[i][j]) cont++;

            if(cont){
                pq.push(make_pair(0,pii(i,j)));
                vis[i][j] = 1;
                tempo[i][j] = 0;
            }
        }
    }

    while(!pq.empty()){
        int t = -pq.top().fi;
        int i = pq.top().se.fi, j = pq.top().se.se;
        pq.pop();

        if(posValid(i-1,j) && !vis[i-1][j]){
            tempo[i-1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i-1,j)));
            vis[i-1][j] = 1;
        }

        if(posValid(i+1,j) && !vis[i+1][j]){
            tempo[i+1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i+1,j)));
            vis[i+1][j] = 1;
        }

        if(posValid(i,j-1) && !vis[i][j-1]){
            tempo[i][j-1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j-1)));
            vis[i][j-1] = 1;
        }

        if(posValid(i,j+1) && !vis[i][j+1]){
            tempo[i][j+1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j+1)));
            vis[i][j+1] = 1;
        }
    }
}

void solve(){

    ms(tempo,64);

    dfs();

    FOR(i,0,K){
        int a,b,c;

        cin >> a >> b >> c; a--; b--;

        if(tempo[a][b] == 1077952576) cout << v[a][b] << endl;
        else if(tempo[a][b] >= c){
            cout << v[a][b] << endl;
        }else{
            int dif = c - tempo[a][b];

            if( !(dif&1)){
                cout << v[a][b] << endl;
            }else{
                int ans = v[a][b] - '0';
                cout << (ans ^ 1) << endl;
            }
        }
    }
}

int main(){

    cin >> N >> M >> K;

    FOR(i,0,N) FOR(j,0,M) cin >> v[i][j];

    solve();

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }

typedef long long ll;
typedef long double ld;

#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"

#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

#define sz(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
#define ms(x,a) memset(x,a,sizeof(x))

#define PI 3.141592653589793238462643383279502884
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define mod 1000000007
#define MAXN 1010
#define MAXL 987654321

ll T,N,M,K;
char v[MAXN][MAXN];
ll tempo[MAXN][MAXN];
bool vis[MAXN][MAXN];

bool posValid(int x , int y){

    if(x >= 0 && x < N && y>=0 && y<M) return true;
    return false;
}

void dfs(){

    priority_queue<pair<ll,pii>> pq;

    FOR(i,0,N){
        FOR(j,0,M){
            int cont = 0;

            if(posValid(i-1,j)) if(v[i-1][j] == v[i][j]) cont++;
            if(posValid(i+1,j)) if(v[i+1][j] == v[i][j]) cont++;
            if(posValid(i,j-1)) if(v[i][j-1] == v[i][j]) cont++;
            if(posValid(i,j+1)) if(v[i][j+1] == v[i][j]) cont++;

            if(cont){
                pq.push(make_pair(0,pii(i,j)));
                vis[i][j] = 1;
                tempo[i][j] = 0;
            }
        }
    }

    while(!pq.empty()){
        ll t = -pq.top().fi;
        int i = pq.top().se.fi, j = pq.top().se.se;
        pq.pop();

        if(posValid(i-1,j) && !vis[i-1][j]){
            tempo[i-1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i-1,j)));
            vis[i-1][j] = 1;
        }

        if(posValid(i+1,j) && !vis[i+1][j]){
            tempo[i+1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i+1,j)));
            vis[i+1][j] = 1;
        }

        if(posValid(i,j-1) && !vis[i][j-1]){
            tempo[i][j-1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j-1)));
            vis[i][j-1] = 1;
        }

        if(posValid(i,j+1) && !vis[i][j+1]){
            tempo[i][j+1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j+1)));
            vis[i][j+1] = 1;
        }
    }
}

void solve(){

    FOR(i,0,N) FOR(j,0,M) tempo[i][j] = MAXL;

    dfs();

    FOR(i,0,K){
        int a,b;
        ll c;

        cin >> a >> b >> c; a--; b--;

        if(tempo[a][b] == MAXL) cout << v[a][b] << endl;
        else if( tempo[a][b] >= c){
            cout << v[a][b] << endl;
        }else{
            ll dif = c - tempo[a][b];

            if( !(dif&1)){
                cout << v[a][b] << endl;
            }else{
                int ans = v[a][b] - '0';
                cout << (ans ^ 1) << endl;
            }
        }
    }
}

int main(){

    cin >> N >> M >> K;

    FOR(i,0,N) FOR(j,0,M) cin >> v[i][j];

    solve();

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }

typedef long long ll;
typedef long double ld;

#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"

#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

#define sz(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
#define ms(x,a) memset(x,a,sizeof(x))

#define PI 3.141592653589793238462643383279502884
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define mod 1000000007
#define MAXN 1010

ll T,N,M,K;
char v[MAXN][MAXN];
int tempo[MAXN][MAXN];
bool vis[MAXN][MAXN];

bool posValid(int x , int y){

    if(x >= 0 && x < N && y>=0 && y<M) return true;
    return false;
}

void dfs(){

    priority_queue<pair<int,pii>> pq;

    FOR(i,0,N){
        FOR(j,0,M){
            int cont = 0;

            if(posValid(i-1,j)) if(v[i-1][j] == v[i][j]) cont++;
            if(posValid(i+1,j)) if(v[i+1][j] == v[i][j]) cont++;
            if(posValid(i,j-1)) if(v[i][j-1] == v[i][j]) cont++;
            if(posValid(i,j+1)) if(v[i][j+1] == v[i][j]) cont++;

            if(cont){
                pq.push(make_pair(0,pii(i,j)));
                vis[i][j] = 1;
            }
        }
    }

    while(!pq.empty()){
        int t = -pq.top().fi;
        int i = pq.top().se.fi, j = pq.top().se.se;
        pq.pop();

        if(posValid(i-1,j) && !vis[i-1][j]){
            tempo[i-1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i-1,j)));
            vis[i-1][j] = 1;
        }

        if(posValid(i+1,j) && !vis[i+1][j]){
            tempo[i+1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i+1,j)));
            vis[i+1][j] = 1;
        }

        if(posValid(i,j-1) && !vis[i][j-1]){
            tempo[i][j-1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j-1)));
            vis[i][j-1] = 1;
        }

        if(posValid(i,j+1) && !vis[i][j+1]){
            tempo[i][j+1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j+1)));
            vis[i][j+1] = 1;
        }
    }
}

void solve(){

    ms(tempo,64);

    dfs();

    FOR(i,0,K){
        int a,b,c;

        cin >> a >> b >> c; a--; b--;

        if(tempo[a][b] == 1077952576) cout << v[a][b] << endl;
        else if(tempo[a][b] >= c){
            cout << v[a][b] << endl;
        }else{
            int dif = c - tempo[a][b];

            if( !(dif&1)){
                cout << v[a][b] << endl;
            }else{
                int ans = v[a][b] - '0';
                cout << (ans ^ 1) << endl;
            }
        }
    }
}

int main(){

    cin >> N >> M >> K;

    FOR(i,0,N) FOR(j,0,M) cin >> v[i][j];

    solve();

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }

typedef long long ll;
typedef long double ld;

#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"

#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

#define sz(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
#define ms(x,a) memset(x,a,sizeof(x))

#define PI 3.141592653589793238462643383279502884
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define mod 1000000007
#define MAXN 1010

ll T,N,M,K;
char v[MAXN][MAXN];
int tempo[MAXN][MAXN];
bool vis[MAXN][MAXN];

bool posValid(int x , int y){

    if(x >= 0 && x < N && y>=0 && y<M) return true;
    return false;
}

void dfs(){

    priority_queue<pair<int,pii>> pq;

    FOR(i,0,N){
        FOR(j,0,M){
            int cont = 0;

            if(posValid(i-1,j)) if(v[i-1][j] == v[i][j]) cont++;
            if(posValid(i+1,j)) if(v[i+1][j] == v[i][j]) cont++;
            if(posValid(i,j-1)) if(v[i][j-1] == v[i][j]) cont++;
            if(posValid(i,j+1)) if(v[i][j+1] == v[i][j]) cont++;

            if(cont){
                pq.push(make_pair(0,pii(i,j)));
                vis[i][j] = 1;
                tempo[i][j] = 0;
            }
        }
    }

    while(!pq.empty()){
        int t = -pq.top().fi;
        int i = pq.top().se.fi, j = pq.top().se.se;
        pq.pop();

        if(posValid(i-1,j) && !vis[i-1][j]){
            tempo[i-1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i-1,j)));
            vis[i-1][j] = 1;
        }

        if(posValid(i+1,j) && !vis[i+1][j]){
            tempo[i+1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i+1,j)));
            vis[i+1][j] = 1;
        }

        if(posValid(i,j-1) && !vis[i][j-1]){
            tempo[i][j-1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j-1)));
            vis[i][j-1] = 1;
        }

        if(posValid(i,j+1) && !vis[i][j+1]){
            tempo[i][j+1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j+1)));
            vis[i][j+1] = 1;
        }
    }
}

void solve(){

    FOR(i,0,N) FOR(j,0,M) tempo[i][j] = MAXN;

    dfs();

    FOR(i,0,K){
        int a,b,c;

        cin >> a >> b >> c; a--; b--;

        if(tempo[a][b] == MAXN) cout << v[a][b] << endl;
        else if(tempo[a][b] >= c){
            cout << v[a][b] << endl;
        }else{
            int dif = c - tempo[a][b];

            if( !(dif&1)){
                cout << v[a][b] << endl;
            }else{
                int ans = v[a][b] - '0';
                cout << (ans ^ 1) << endl;
            }
        }
    }
}

int main(){

    cin >> N >> M >> K;

    FOR(i,0,N) FOR(j,0,M) cin >> v[i][j];

    solve();

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }

typedef long long ll;
typedef long double ld;

#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"

#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

#define sz(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
#define ms(x,a) memset(x,a,sizeof(x))

#define PI 3.141592653589793238462643383279502884
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define mod 1000000007
#define MAXN 1010
#define MAXL 987654321

ll T,N,M,K;
char v[MAXN][MAXN];
ll tempo[MAXN][MAXN];
bool vis[MAXN][MAXN];

bool posValid(int x , int y){

    if(x >= 0 && x < N && y>=0 && y<M) return true;
    return false;
}

void dfs(){

    priority_queue<pair<ll,pii>> pq;

    FOR(i,0,N){
        FOR(j,0,M){
            int cont = 0;

            if(posValid(i-1,j)) if(v[i-1][j] == v[i][j]) cont++;
            if(posValid(i+1,j)) if(v[i+1][j] == v[i][j]) cont++;
            if(posValid(i,j-1)) if(v[i][j-1] == v[i][j]) cont++;
            if(posValid(i,j+1)) if(v[i][j+1] == v[i][j]) cont++;

            if(cont){
                pq.push(make_pair(0,pii(i,j)));
                vis[i][j] = 1;
                tempo[i][j] = 0;
            }
        }
    }

    while(!pq.empty()){
        ll t = -pq.top().fi;
        int i = pq.top().se.fi, j = pq.top().se.se;
        pq.pop();

        if(posValid(i-1,j) && !vis[i-1][j]){
            tempo[i-1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i-1,j)));
            vis[i-1][j] = 1;
        }

        if(posValid(i+1,j) && !vis[i+1][j]){
            tempo[i+1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i+1,j)));
            vis[i+1][j] = 1;
        }

        if(posValid(i,j-1) && !vis[i][j-1]){
            tempo[i][j-1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j-1)));
            vis[i][j-1] = 1;
        }

        if(posValid(i,j+1) && !vis[i][j+1]){
            tempo[i][j+1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j+1)));
            vis[i][j+1] = 1;
        }
    }
}

void solve(){

    FOR(i,0,N) FOR(j,0,M) tempo[i][j] = MAXL;

    dfs();

    FOR(i,0,K){
        int a,b;
        ll c;

        cin >> a >> b >> c; a--; b--;

        if(tempo[a][b] == MAXL) cout << v[a][b] << endl;
        else if( tempo[a][b] >= c){
            cout << v[a][b] << endl;
        }else{
            ll dif = c - tempo[a][b];

            if( !(dif&1LL)){
                cout << v[a][b] << endl;
            }else{
                int ans = v[a][b] - '0';
                cout << (ans ^ 1) << endl;
            }
        }
    }
}

int main(){

    cin >> N >> M >> K;

    FOR(i,0,N) FOR(j,0,M) cin >> v[i][j];

    solve();

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }

typedef long long ll;
typedef long double ld;

#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"

#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

#define sz(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
#define ms(x,a) memset(x,a,sizeof(x))

#define PI 3.141592653589793238462643383279502884
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define mod 1000000007
#define MAXN 1010

ll T,N,M,K;
char v[MAXN][MAXN];
ll tempo[MAXN][MAXN];
bool vis[MAXN][MAXN];

bool posValid(int x , int y){

    if(x >= 0 && x < N && y>=0 && y<M) return true;
    return false;
}

void dfs(){

    priority_queue<pair<ll,pii>> pq;

    FOR(i,0,N){
        FOR(j,0,M){
            int cont = 0;

            if(posValid(i-1,j)) if(v[i-1][j] == v[i][j]) cont++;
            if(posValid(i+1,j)) if(v[i+1][j] == v[i][j]) cont++;
            if(posValid(i,j-1)) if(v[i][j-1] == v[i][j]) cont++;
            if(posValid(i,j+1)) if(v[i][j+1] == v[i][j]) cont++;

            if(cont){
                pq.push(make_pair(0,pii(i,j)));
                vis[i][j] = 1;
                tempo[i][j] = 0;
            }
        }
    }

    while(!pq.empty()){
        ll t = -pq.top().fi;
        int i = pq.top().se.fi, j = pq.top().se.se;
        pq.pop();

        if(posValid(i-1,j) && !vis[i-1][j]){
            tempo[i-1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i-1,j)));
            vis[i-1][j] = 1;
        }

        if(posValid(i+1,j) && !vis[i+1][j]){
            tempo[i+1][j] = t+1;
            pq.push(make_pair(-(t+1),pii(i+1,j)));
            vis[i+1][j] = 1;
        }

        if(posValid(i,j-1) && !vis[i][j-1]){
            tempo[i][j-1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j-1)));
            vis[i][j-1] = 1;
        }

        if(posValid(i,j+1) && !vis[i][j+1]){
            tempo[i][j+1] = t+1;
            pq.push(make_pair(-(t+1),pii(i,j+1)));
            vis[i][j+1] = 1;
        }
    }
}

void solve(){

    FOR(i,0,N) FOR(j,0,M) tempo[i][j] = MAXN;

    dfs();

    FOR(i,0,K){
        int a,b;
        ll c;

        cin >> a >> b >> c; a--; b--;

        if(tempo[a][b] == MAXN) cout << v[a][b] << endl;
        else if( tempo[a][b] >= c){
            cout << v[a][b] << endl;
        }else{
            ll dif = c - tempo[a][b];

            if( !(dif&1)){
                cout << v[a][b] << endl;
            }else{
                int ans = v[a][b] - '0';
                cout << (ans ^ 1) << endl;
            }
        }
    }
}

int main(){

    cin >> N >> M >> K;

    FOR(i,0,N) FOR(j,0,M) cin >> v[i][j];

    solve();

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Max = 10000000;
int n, m, t;
vector<vector<int>> field;
vector<vector<int>> bt;
vector<vector<bool>> visited;

int dfs(int i, int j) {
    if (visited[i][j])
        return bt[i][j];
    visited[i][j] = true;
    for (int sx = -1; sx <= 1; ++sx) {
        for (int sy = -1; sy <= 1; ++sy) {
            if (sx * sy != 0 || (sx == 0 && sy == 0))
                continue;
            int ni = i + sx;
            int nj = j + sy;
            if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                continue;
            if (field[ni][nj] == field[i][j]) {
                bt[i][j] = 0;
                break;
            }
            bt[i][j] = min(bt[i][j], dfs(ni, nj) + 1);
        }
    }
    return bt[i][j];
}

int main() {
    cin >> n >> m >> t;
    for (int i = 0; i < n; ++i) {
        field.emplace_back(m);
        string line;
        cin >> line;
        for (int j = 0; j < m; ++j)
            field[i][j] = line[j] - '0';
    }
    for (int i = 0; i < n; ++i) {
        bt.emplace_back(m, Max);
        visited.emplace_back(m);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
//            cout << dfs(i, j) << " ";
            dfs(i, j);
        }
//        cout << "\n";
    }

    for (int q = 0; q < t; ++q) {
        int i, j;
        long long p;
        cin >> i >> j >> p;
        --i; --j;
        if (bt[i][j] == Max) {
            cout << field[i][j] << "\n";
        } else {
            if (p <= bt[i][j]) {
                cout << field[i][j] << "\n";
            } else {
                cout << (field[i][j] + (p - bt[i][j]) % 2) % 2 << "\n";
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Max = 10000000;
int n, m, t;
vector<vector<int>> field;
vector<vector<long long>> bt;
vector<vector<bool>> visited;

long long dfs(int i, int j) {
    if (visited[i][j])
        return bt[i][j];
    visited[i][j] = true;
    for (int sx = -1; sx <= 1; ++sx) {
        for (int sy = -1; sy <= 1; ++sy) {
            if (abs(sx) + abs(sy) != 1)
                continue;
            int ni = i + sx;
            int nj = j + sy;
            if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                continue;
            if (field[ni][nj] == field[i][j]) {
                bt[i][j] = 0;
                break;
            }
            bt[i][j] = min(bt[i][j], dfs(ni, nj) + 1);
        }
    }
    return bt[i][j];
}

int main() {
    cin >> n >> m >> t;
    for (int i = 0; i < n; ++i) {
        field.emplace_back(m);
        string line;
        cin >> line;
        for (int j = 0; j < m; ++j)
            field[i][j] = line[j] - '0';
    }
    for (int i = 0; i < n; ++i) {
        bt.emplace_back(m, Max);
        visited.emplace_back(m);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
//            cout << dfs(i, j) << " ";
            dfs(i, j);
        }
//        cout << "\n";
    }

    for (int q = 0; q < t; ++q) {
        int i, j;
        long long p;
        cin >> i >> j >> p;
        --i; --j;
        if (bt[i][j] == Max) {
            cout << field[i][j] << "\n";
        } else {
            if (p <= bt[i][j]) {
                cout << field[i][j] << "\n";
            } else {
                cout << (field[i][j] + (p - bt[i][j]) % 2) % 2 << "\n";
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Max = 10000000;
int n, m, t;
vector<vector<int>> field;
vector<vector<long long>> bt;
vector<vector<bool>> visited;

int query(int i, int j, int p) {
    if (bt[i][j] == Max) {
        return field[i][j];
    } else {
        if (p <= bt[i][j]) {
            return field[i][j];
        } else {
            return (field[i][j] + (p - bt[i][j]) % 2) % 2;
        }
    }
}

int main() {
    cin >> n >> m >> t;
    for (int i = 0; i < n; ++i) {
        field.emplace_back(m);
        string line;
        cin >> line;
        for (int j = 0; j < m; ++j)
            field[i][j] = line[j] - '0';
    }
    for (int i = 0; i < n; ++i) {
        bt.emplace_back(m, Max);
        visited.emplace_back(m);
    }
    queue<pair<int, int>> q;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int sx = -1; sx <= 1; ++sx) {
                for (int sy = -1; sy <= 1; ++sy) {
                    if (abs(sx) + abs(sy) != 1)
                        continue;
                    int ni = i + sx;
                    int nj = j + sy;
                    if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                        continue;
                    if (field[i][j] == field[ni][nj]) {
                        q.emplace(i, j);
                        bt[i][j] = 0;
                    }
                }
            }
        }
    }
    while (!q.empty()) {
        int i, j;
        tie(i, j) = q.front(); q.pop();
        if (visited[i][j])
            continue;
        visited[i][j] = true;
        for (int sx = -1; sx <= 1; ++sx) {
            for (int sy = -1; sy <= 1; ++sy) {
                if (abs(sx) + abs(sy) != 1)
                    continue;
                int ni = i + sx;
                int nj = j + sy;
                if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                    continue;
                bt[ni][nj] = min(bt[ni][nj], bt[i][j] + 1);
                q.emplace(ni, nj);
            }
        }
    }
    for (int q = 0; q < t; ++q) {
        int i, j;
        long long p;
        cin >> i >> j >> p;
        --i; --j;
        cout << query(i, j, p) << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    const int di[4] = {1, -1, 0, 0};
    const int dj[4] = {0, 0, 1, -1};
    int n, m, t;
    cin >> n >> m >> t;
    vector<vector<int>> f(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        string line;
        cin >> line;
        for (int j = 0; j < m; ++j)
            f[i][j] = line[j] - '0';
    }
    vector<vector<int>> when(n, vector<int>(m, -1));
    deque<pair<int, int>> q;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int k = 0; k < 4; ++k) {
                int ni = i + di[k];
                int nj = j + dj[k];
                if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                    continue;
                if (f[i][j] == f[ni][nj]) {
                    when[i][j] = 0;
                    q.emplace_back(i, j);
                    break;
                }
            }
        }
    }
    while (!q.empty()) {
        int i, j;
        tie(i, j) = q.front(); q.pop_front();
        for (int k = 0; k < 4; ++k) {
            int ni = i + di[k];
            int nj = j + dj[k];
            if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                continue;
            if (when[ni][nj] < 0) {
                when[ni][nj] = when[i][j] + 1;
                q.emplace_back(ni, nj);
            }
        }
    }
    while (t--) {
        int i, j;
        long long p;
        cin >> i >> j >> p;
        --i; --j;
        if (when[i][j] < 0 || p < when[i][j])
            cout << f[i][j] << "\n";
        else
            cout << ((p - when[i][j]) % 2 == 0 ? f[i][j] : (1 - f[i][j])) << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    const int di[4] = {1, -1, 0, 0};
    const int dj[4] = {0, 0, 1, -1};
    int n, m, t;
    cin >> n >> m >> t;
    vector<vector<int>> f(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        string line;
        cin >> line;
        for (int j = 0; j < m; ++j)
            f[i][j] = line[j] - '0';
    }
    vector<vector<int>> when(n, vector<int>(m, -1));
    deque<pair<int, int>> q;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int k = 0; k < 4; ++k) {
                int ni = i + di[k];
                int nj = j + dj[k];
                if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                    continue;
                if (f[i][j] == f[ni][nj]) {
                    when[i][j] = 0;
                    q.emplace_back(i, j);
                    break;
                }
            }
        }
    }
    while (!q.empty()) {
        int i, j;
        tie(i, j) = q.front(); q.pop_front();
        for (int k = 0; k < 4; ++k) {
            int ni = i + di[k];
            int nj = j + dj[k];
            if (ni < 0 || ni >= n || nj < 0 || nj >= m)
                continue;
            if (when[ni][nj] < 0) {
                when[ni][nj] = when[i][j] + 1;
                q.emplace_back(ni, nj);
            }
        }
    }
    while (t--) {
        int i, j, p;
        cin >> i >> j >> p;
        --i; --j;
        if (when[i][j] < 0 || p < when[i][j])
            cout << f[i][j] << "\n";
        else
            cout << ((p - when[i][j]) % 2 == 0 ? f[i][j] : (1 - f[i][j])) << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\n"
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e3 + 10;
constexpr int INF = 0x3f3f3f3f;
int n, m, q, dis[N][N];
char g[N][N];

inline bool ok(int x, int y) {
	if (x < 1 || x > n || y < 1 || y > m)
		return false;
	return true;
}

int Move[][2] = {
	1, 0,
	-1, 0,
	0, 1,
	0, -1,
};

void dfs(int x, int y) {
	dis[x][y] = 0;
	for (int i = 0; i < 4; ++i) {
		int nx = x + Move[i][0];
		int ny = y + Move[i][1];
		if (ok(nx, ny) && g[nx][ny] == g[x][y] && dis[nx][ny] == INF)
			dfs(nx, ny);
	}
}

void bfs() {
	queue <pII> que;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if (dis[i][j] == 0) {
				que.push(pII(i, j));
			}
		}
	}
	while (!que.empty()) {
		pII u = que.front(); que.pop();
		for (int i = 0; i < 4; ++i) {
			int x = u.fi + Move[i][0];
			int y = u.se + Move[i][1];
			if (ok(x, y) && dis[x][y] > dis[u.fi][u.se] + 1) {
				dis[x][y] = dis[u.fi][u.se] + 1;
				que.push(pII(x, y));
			}
		}
	}
}

void run() {
	rd(n, m, q);
	memset(dis, 0x3f, sizeof dis);
	for (int i = 1; i <= n; ++i) cin >> (g[i] + 1);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) if (dis[i][j] == INF) {
			int cnt = 0;
			for (int k = 0; k < 4; ++k) {
				int ni = i + Move[k][0];
				int nj = j + Move[k][1];
				if (ok(ni, nj) && g[ni][nj] == g[i][j]) {
					++cnt;
				}
			}
			if (cnt) dfs(i, j);
		}
	}
	bfs();
//	return;
	while (q--) {
		int i, j; ll p;
		rd(i, j, p);
		if (1ll * dis[i][j] >= p) pt(g[i][j]);
		else pt(char(((g[i][j] - '0') ^ ((p - dis[i][j]) & 1)) + '0'));
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = 1;
	while (_T--) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ": ";
//        run();
//    }
//	while (cin >> n) run();
//	run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\n"
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e3 + 10;
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3f;
int n, m, q; ll dis[N][N];
char g[N][N];

inline bool ok(int x, int y) {
	if (x < 1 || x > n || y < 1 || y > m)
		return false;
	return true;
}

int Move[][2] = {
	1, 0,
	-1, 0,
	0, 1,
	0, -1,
};

void dfs(int x, int y) {
	dis[x][y] = 0;
	for (int i = 0; i < 4; ++i) {
		int nx = x + Move[i][0];
		int ny = y + Move[i][1];
		if (ok(nx, ny) && g[nx][ny] == g[x][y] && dis[nx][ny] == INFLL)
			dfs(nx, ny);
	}
}

void bfs() {
	queue <pII> que;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if (dis[i][j] == 0) {
				que.push(pII(i, j));
			}
		}
	}
	while (!que.empty()) {
		pII u = que.front(); que.pop();
		for (int i = 0; i < 4; ++i) {
			int x = u.fi + Move[i][0];
			int y = u.se + Move[i][1];
			if (ok(x, y) && dis[x][y] > dis[u.fi][u.se] + 1) {
				dis[x][y] = dis[u.fi][u.se] + 1;
				que.push(pII(x, y));
			}
		}
	}
}

void run() {
	rd(n, m, q);
	memset(dis, 0x3f, sizeof dis);
	for (int i = 1; i <= n; ++i) cin >> (g[i] + 1);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) if (dis[i][j] == INFLL) {
			int cnt = 0;
			for (int k = 0; k < 4; ++k) {
				int ni = i + Move[k][0];
				int nj = j + Move[k][1];
				if (ok(ni, nj) && g[ni][nj] == g[i][j]) {
					++cnt;
				}
			}
			if (cnt) dfs(i, j);
		}
	}
	bfs();
//	return;
	while (q--) {
		int i, j; ll p;
		rd(i, j, p);
		if (1ll * dis[i][j] >= p) pt(g[i][j]);
		else pt(char(((g[i][j] - '0') ^ ((p - dis[i][j]) & 1)) + '0'));
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = 1;
	while (_T--) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ": ";
//        run();
//    }
//	while (cin >> n) run();
//	run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\n"
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e3 + 10;
constexpr int INF = 0x3f3f3f3f;
int n, m, q, dis[N][N];
char g[N][N];

inline bool ok(int x, int y) {
	if (x < 1 || x > n || y < 1 || y > m)
		return false;
	return true;
}

int Move[][2] = {
	1, 0,
	-1, 0,
	0, 1,
	0, -1,
};

void dfs(int x, int y) {
	dis[x][y] = 0;
	for (int i = 0; i < 4; ++i) {
		int nx = x + Move[i][0];
		int ny = y + Move[i][1];
		if (ok(nx, ny) && g[nx][ny] == g[x][y] && dis[nx][ny] == INF)
			dfs(nx, ny);
	}
}

void bfs() {
	queue <pII> que;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if (dis[i][j] == 0) {
				que.push(pII(i, j));
			}
		}
	}
	while (!que.empty()) {
		pII u = que.front(); que.pop();
		for (int i = 0; i < 4; ++i) {
			int x = u.fi + Move[i][0];
			int y = u.se + Move[i][1];
			if (ok(x, y) && dis[x][y] > dis[u.fi][u.se] + 1) {
				dis[x][y] = dis[u.fi][u.se] + 1;
				que.push(pII(x, y));
			}
		}
	}
}

void run() {
	rd(n, m, q);
	memset(dis, 0x3f, sizeof dis);
	for (int i = 1; i <= n; ++i) cin >> (g[i] + 1);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) if (dis[i][j] == INF) {
			int cnt = 0;
			for (int k = 0; k < 4; ++k) {
				int ni = i + Move[k][0];
				int nj = j + Move[k][1];
				if (ok(ni, nj) && g[ni][nj] == g[i][j]) {
					++cnt;
				}
			}
			if (cnt) dfs(i, j);
		}
	}
	bfs();
//	return;
	while (q--) {
		int i, j; ll p;
		rd(i, j, p);
		if (dis[i][j] >= p) pt(g[i][j]);
		else pt(char(((g[i][j] - '0') ^ ((p - dis[i][j]) & 1)) + '0'));
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = 1;
	while (_T--) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ": ";
//        run();
//    }
//	while (cin >> n) run();
//	run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\n"
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e3 + 10;
constexpr int INF = 0x3f3f3f3f;
int n, m, q, dis[N][N];
char g[N][N];

inline bool ok(int x, int y) {
	if (x < 1 || x > n || y < 1 || y > m)
		return false;
	return true;
}

int Move[][2] = {
	1, 0,
	-1, 0,
	0, 1,
	0, -1,
};

void dfs(int x, int y) {
	dis[x][y] = 0;
	for (int i = 0; i < 4; ++i) {
		int nx = x + Move[i][0];
		int ny = y + Move[i][1];
		if (ok(nx, ny) && g[nx][ny] == g[x][y] && dis[nx][ny] == INF)
			dfs(nx, ny);
	}
}

void bfs() {
	queue <pII> que;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if (dis[i][j] == 0) {
				que.push(pII(i, j));
			}
		}
	}
	while (!que.empty()) {
		pII u = que.front(); que.pop();
		for (int i = 0; i < 4; ++i) {
			int x = u.fi + Move[i][0];
			int y = u.se + Move[i][1];
			if (ok(x, y) && dis[x][y] > dis[u.fi][u.se] + 1) {
				dis[x][y] = dis[u.fi][u.se] + 1;
				que.push(pII(x, y));
			}
		}
	}
}

void run() {
	rd(n, m, q);
	memset(dis, 0x3f, sizeof dis);
	for (int i = 1; i <= n; ++i) cin >> (g[i] + 1);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) if (dis[i][j] == INF) {
			int cnt = 0;
			for (int k = 0; k < 4; ++k) {
				int ni = i + Move[k][0];
				int nj = j + Move[k][1];
				if (ok(ni, nj) && g[ni][nj] == g[i][j]) {
					++cnt;
				}
			}
			if (cnt) dfs(i, j);
		}
	}
	bfs();
//	return;
	while (q--) {
		int i, j, p;
		rd(i, j, p);
		if (dis[i][j] >= p) pt(g[i][j]);
		else pt(char(((g[i][j] - '0') ^ ((p - dis[i][j]) & 1)) + '0'));
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = 1;
	while (_T--) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ": ";
//        run();
//    }
//	while (cin >> n) run();
//	run();
	return 0;
}