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
#include <bits/stdc++.h>

using namespace std;
vector<vector<pair<int, int>>> cost;
vector<vector<bool>> tab;
int n, m, z;

bool wolny(int i, int j){
	if(i<0) return false;
	if(j<0) return false;
	if(i>=n) return false;
	if(j>=m) return false;
	if(tab[i][j]) return false;
	return true;	
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
	cin>>n>>m>>z;
	cost.resize(n, vector<pair<int, int>>(m, make_pair(-1, -1)));	
	tab.resize(n, vector<bool>(m));
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			char c; cin>>c;
			tab[i][j] = (c=='X');	
		}	
	}


	cost[n-1][m-1] = {0, 0};
	queue<pair<int, int>> q;
	q.push({n-1, m-1});
	
	while(q.size()){
		int i = q.front().first;
		int j = q.front().second;		
		q.pop();

		if(wolny(i-1, j)) if(cost[i-1][j].first==-1){	
			cost[i-1][j] = cost[i][j];
			cost[i-1][j].first++;	
			q.push(make_pair(i-1, j));
		}
		if(wolny(i+1, j)) if(cost[i+1][j].first==-1){	
			cost[i+1][j] = cost[i][j];
			cost[i+1][j].second++;	
			q.push(make_pair(i+1, j));
		}
		if(wolny(i, j-1)) if(cost[i][j-1].first==-1){	
			cost[i][j-1] = cost[i][j];
			cost[i][j-1].first++;	
			q.push(make_pair(i, j-1));
		}
		if(wolny(i, j+1)) if(cost[i][j+1].first==-1){	
			cost[i][j+1] = cost[i][j];
			cost[i][j+1].second++;	
			q.push(make_pair(i, j+1));
		}
	}

	map<long long, int> mp; 
	for(int i = 0; i < z; i++){
		long long a, b; cin>>a>>b;
		long long tmp = b*cost[0][0].second+a*cost[0][0].first;
		mp[tmp]++;
	}

	cout<<mp.begin()->first<<" "<<mp.begin()->second;

	return 0;
}