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
#include<iostream>
#include<vector>
#include<queue>
#include<utility> 
#include <algorithm>
using namespace std;

bool isOK (long long i, long long j, long long &m, long long &n, vector <vector <char> > &P, vector <vector < long long > > &W){
	if (i < 0 || j<0) return false;
	if(i >= m || j >=n)return false;
	if(W[i][j] != -1)return false;
	if(P[i][j] == 'X')return false;
	return true;
}

int main(){
	cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false);
		long long m, n,k; cin >> m >> n >> k;
		pair<long long, long long> start(0,0), end(m-1,n-1);
		vector< vector <char> > P(m);
		for (long long i=0;i<m;i++){
			for(long long j=0; j<n;j++){
				char temp; cin >> temp;
				P[i].push_back(temp);
			}
		}
		queue <pair< long long, long long > > Q;
		Q.push(start);
		vector <vector < long long > > W (m);
		for(long long i=0;i<m;i++){
			for(long long j=0;j<n;j++){
				W[i].push_back(-1);
			}
		}
		W[start.first][start.second]= 0;
		while(!Q.empty()){
			pair <long long, long long> temp; temp = Q.front(); Q.pop();
			if(isOK(temp.first+1, temp.second, m, n, P, W)){
				W[temp.first+1][temp.second] = W[temp.first][temp.second]+1;
				pair <long long, long long> nowy (temp.first+1, temp.second);
				Q.push(nowy);
			}
			if(isOK(temp.first-1, temp.second, m, n, P, W)){
				W[temp.first-1][temp.second] = W[temp.first][temp.second]+1;
				pair <long long, long long> nowy (temp.first-1, temp.second);
				Q.push(nowy);
			}
			if(isOK(temp.first, temp.second+1, m, n, P, W)){
				W[temp.first][temp.second+1] = W[temp.first][temp.second]+1;
				pair <long long, long long> nowy (temp.first, temp.second+1);
				Q.push(nowy);
			}
			if(isOK(temp.first, temp.second-1, m, n, P, W)){
				W[temp.first][temp.second-1] = W[temp.first][temp.second]+1;
				pair <long long, long long> nowy (temp.first, temp.second-1);
				Q.push(nowy);
			}
		}
		// cout << W[end.first][end.second] << ' ' <<  W1[end.first][end.second]<< '\n';
		vector <long long> K (k);
		for(long long i=0;i<k;i++){
			long long a,b; cin >> a >> b;
			K[i]= ((n+m -2) * a) + ((a+b)*((W[end.first][end.second]-n-m+2)/2));
		}
		long long min = *min_element(K.begin(),K.end());
		long long counter = 0;
		for(long long i=0;i<k;i++){
			if(K[i]==min)counter++;
		}
		cout << min << ' ' <<  counter << '\n';
		// for(long long i=0;i<m;i++){
		// 	for(long long j=0;j<n;j++){
		// 		cout << W1[i][j] << ' ';
		// 	}
		// 	cout << '\n';
		// }
}