1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=2e6+5;
int p[MAX];
vector<vector<string>>s;
vector<vector<bool>>O;
vector<vector<bool>>xd;
int n,m,q;

bool check(int x,int y){ // plac
	return x>=0 && x<=n && y>=0 && y<=m;
}

int conv(int x,int y){
	return x*(m+1)+y;
}

void ustaw(int nn,int nm){
	s.resize(nn+1);for (int i=0;i<=nn;i++)s[i].resize(nm+1);
	O.resize(nn+1);for (int i=0;i<=nn;i++)O[i].resize(nm+1);
	xd.resize(nn+1);for (int i=0;i<=nn;i++)xd[i].resize(nm+1);
	for (int i=0;i<=n;i++)for (int j=0;j<=m;j++)O[i][j]=false;
	for (int i=0;i<=n;i++)for (int j=0;j<=m;j++)xd[i][j]=false;
}

vector<pii>stos;
vector<pii>stos2;
int ans;

int px[4]={0,1,0,-1};
int py[4]={1,0,-1,0};
// prawo dol lewo gora
bool open(int x,int y,int wsk){
  int tox=px[wsk]+x;
  int toy=py[wsk]+y;
  int a=x,b=y,c=tox,d=toy;
	for (int portalx=x;portalx<=x+1;portalx++){
		for (int portaly=y;portaly<=y+1;portaly++){
			if (portalx<1 || portalx>n || portaly<1 || portaly>m)continue;
			if (s[portalx][portaly][ans%sz(s[portalx][portaly])]=='0'){
				int ap=portalx-1,bp=portaly-1,cp=portalx,dp=portaly-1;
				if (mp(a,b)==mp(ap,bp) && mp(c,d)==mp(cp,dp))return true;
				if (mp(a,b)==mp(cp,dp) && mp(c,d)==mp(ap,bp))return true;
				ap=portalx-1,bp=portaly,cp=portalx,dp=portaly;
				if (mp(a,b)==mp(ap,bp) && mp(c,d)==mp(cp,dp))return true;
				if (mp(a,b)==mp(cp,dp) && mp(c,d)==mp(ap,bp))return true;
			}
			else{
				int ap=portalx-1,bp=portaly-1,cp=portalx-1,dp=portaly;
				if (mp(a,b)==mp(ap,bp) && mp(c,d)==mp(cp,dp))return true;
				if (mp(a,b)==mp(cp,dp) && mp(c,d)==mp(ap,bp))return true;
				ap=portalx,bp=portaly-1,cp=portalx,dp=portaly;
				if (mp(a,b)==mp(ap,bp) && mp(c,d)==mp(cp,dp))return true;
				if (mp(a,b)==mp(cp,dp) && mp(c,d)==mp(ap,bp))return true;
			}
		}
	}
	return false;
}
void dfs(int x,int y){
	O[x][y]=true;
	stos2.pb(mp(x,y));
	for (int i=0;i<4;i++){
		int tox=px[i]+x;
		int toy=py[i]+y;
		if (check(tox,toy) && !O[tox][toy] && open(x,y,i)){
			dfs(tox,toy);
		}	 
	}
}
void solve(){
	int a,b,c,d;
	cin>>ans>>a>>b>>c>>d;
	stos.pb(mp(a,b));
	vector<pii>pies;
	pies.clear();
	while (true){
		for (auto it:stos){
			if (!O[it.st][it.nd]){
				dfs(it.st,it.nd);
			}
		}
		stos=stos2;
		bool flaga=false;
		for (auto it:stos){
			if (it.st==c && it.nd==d){flaga=true;}
			bool umiemzyc=true;
			for (int i=0;i<4;i++){
				int tox=px[i]+it.st;
				int toy=py[i]+it.nd;
				if (check(tox,toy) && !O[tox][toy]){umiemzyc=false;break;}
			}
			if (umiemzyc)xd[it.st][it.nd]=true;
		}
		for (auto it:stos){
		  if (xd[it.st][it.nd])pies.pb(mp(it.st,it.nd));
			else O[it.st][it.nd]=false;
		}
		stos2.clear();
		if (flaga)break;
		ans++;
	}
	for (auto it:stos)O[it.st][it.nd]=false;
	for (auto it:pies)O[it.st][it.nd]=false,xd[it.st][it.nd]=false;
	stos.clear();
	stos2.clear();
	cout<<ans<<"\n";
}


int32_t main(){
  BOOST;
  cin>>n>>m>>q;
  ustaw(n,m);
  for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cin>>s[i][j];
		}
  }
  for (int z=0;z<q;z++){
		solve();
  }
  return 0; 
}