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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;
//#undef _HOME_
#ifdef _HOME_
    #define DEBUG(x) x
#else
    #define DEBUG(x)
#endif
#define REP(x,n) for(int x=0;x<(n);++x)
#define FOREACH(x,n) for(__typeof(n.begin()) x = (n).begin(); x != (n).end(); ++x)
#define RESULT(x) {cout<<(x);return 0;}

int n,m,q;

template<typename _T> ostream& operator<<(ostream& os, const vector<_T>& v) {
	FOREACH(it, v)
		os<<*it<<" ";
	return os;
}
struct DATA {
	string str;
	bool operator[](int i) const {
		return str[i%str.size()]=='1';
	}
};
istream& operator>>(istream& is, DATA& d) {
	return is>>d.str;
}
ostream& operator<<(ostream& os, const DATA& d) {
	return os<<d.str;
}
template<typename _T, typename _U> ostream& operator<<(ostream& os, const pair<_T, _U>& p) {
	return os<<"{"<<p.first<<","<<p.second<<"}";
}
vector<vector<DATA> > board;
bool present[9];
#define INVALID {valid=false; break;}
pair<char,vector<int>> separations[1000];
void calculateBoard(int i) {
	DEBUG(
		cerr<<"calculate board ["<<i<<"]"<<endl;
		cerr<<"board is:"<<endl;
		REP(x,n) {
			REP(y,m)
				cerr<<board[x][y][i];
			cerr<<endl;
		}
	)
	vector<int> separateRows;
	vector<int> separateCols;
	bool valid=true;
	REP(x,n) {
		valid=true;
		REP(y,m)
			if (!board[x][y][i])
				INVALID;
		if (valid)
			separateRows.push_back(x);
	}
	REP(y,m) {
		valid=true;
		REP(x,n)
			if (board[x][y][i])
				INVALID;
		if (valid)
			separateCols.push_back(y);
	}

	if (separateCols.empty() && separateRows.empty()) {
		DEBUG(cerr<<"no separations"<<endl;)
		separations[i] = make_pair('N', separateCols);
	}
	else if (separateCols.empty()) {
		DEBUG(cerr<<"separate rows: "<<separateRows<<endl;)
		separations[i] = make_pair('R', separateRows);
	}
	else if (separateRows.empty()) {
		DEBUG(cerr<<"separate cols: "<<separateCols<<endl;)
		separations[i] = make_pair('C', separateCols);
	}
}

void move(const vector<int>& v,int& from, int& to) {
	if (from < to) {
		auto iter = lower_bound(v.begin(), v.end(), from);
		DEBUG(cerr<<"lower_bound is "<<(iter==v.end()?-1:*iter)<<endl;)
		if (iter == v.end())
			from = to;
		else if (from != *iter)
			from = min(to, (*iter));
	} else if (from > to) {
		auto iter = lower_bound(v.begin(), v.end(), from);
		DEBUG(cerr<<"lower_bound is "<<(iter==v.end()?-1:*iter)<<endl;)
		if (iter == v.begin())
			from = to;
		else if (from != *(iter-1))
			from = max(to, 1+*(iter-1));
	}
}

int inline solve(int& lcm, int& t, int& a, int& b, int& c, int& d) {
	if (a==c && b==d)
		return t;
	while(a!=c || b!=d) {
		DEBUG(cerr<<"time "<<t<<" / "<<(t%lcm)<<", get from "<<make_pair(a,b)<<" to "<<make_pair(c,d)<<endl;)
		const pair<char,vector<int>>& sep = separations[t++ % lcm];
		const vector<int>& v = sep.second;
		DEBUG(cerr<<"separators are: "<<sep<<endl;)
		switch(sep.first) {
			case 'N':
				return t-1;
			case 'C':
				a=c;
				move(v,b,d);
				break;
			case 'R':
				b=d;
				move(v,a,c);
				break;
		}
	}
	return t-1;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin>>n>>m>>q;
	board=vector<vector<DATA> >(n,vector<DATA>(m));
	REP(x,n) {
		REP(y,m) {
			cin>>board[x][y];
			present[board[x][y].str.size()] = true;
		}
	}
	DEBUG(
		REP(x,n) {
			cerr << board[x] << endl;
		}
	)
	int lcm=1;
	if (present[8]) lcm*=8; else if (present[4]) lcm *= 4; else if (present[2]||present[6]) lcm *= 2;
	if (present[7]) lcm*=7;
	if (present[5]) lcm *= 5;
	if (present[6] || present[3]) lcm *= 3;
	DEBUG(cerr<<"LCM="<<lcm<<endl;)
	REP(x,lcm)
		calculateBoard(x);
	DEBUG(
		REP(x,lcm)
			cerr<<x<<" -> "<<separations[x]<<endl;
	)
	int t,a,b,c,d;
	REP(x,q) {
		cin>>t>>a>>b>>c>>d;
		cout<<solve(lcm,t,a,b,c,d)<<endl;
	}
	return 0;
}