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 <cstdio>
#include <queue>
using namespace std;

struct nextpoint {
	int x;
	int y;
	int w;
	
	nextpoint(int x, int y, int w): x(x), y(y), w(w) {}
};

queue<nextpoint> punkty;
bool trasa[2001][2001];
int ks[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
int n,m,k;

int main() {
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 0; i < n; ++i){
		char x[5000];
		scanf("%s", x);
		for (int j = 0; j < m; ++j){
			if (x[j] == '.'){
				trasa[i][j] = true;
			}
		}
	}
	
	punkty.push(nextpoint(0,0,0));
	trasa[0][0] = false;
	int res_length = 0;
	bool found = false;
	while(!found){
		nextpoint p = punkty.front();
		punkty.pop();
		for(int i = 0; i < 4; ++i){
			int newx = p.x + ks[i][0];
			int newy = p.y + ks[i][1];
			int neww = p.w + 1;
			//printf("CHECKIN: %d %d: %d ... ", newx, newy, neww);
			if (newx == n-1 && newy == m-1){
				//printf(" FOUND! ");
				res_length = neww;
				found = true;
			}
			if (newx >= 0 && newx < n && newy >= 0 && newy < m && trasa[newx][newy]) {
				//printf("PUSHIN");
				trasa[newx][newy] = false;
				punkty.push(nextpoint(newx,newy,neww));
			}
			//printf("\n");
		}
	}
	
	int str_route = n+m-2;
	int overroute = res_length - str_route;
	overroute /= 2;
	long long total_a = str_route + overroute;
	long long total_b = overroute;
	int rescount = 0;	
	long long bestres = 1000000000000000000LL;
	
	for (int i = 0; i < k; ++i){
		long long a,b;
		scanf("%lld %lld", &a, &b);
		long long res = total_a*a + total_b*b;
		if (res < bestres) {
			bestres = res;
			rescount = 1;
		} else if (res == bestres) {
			rescount++;
		}
	}
	
	printf("%lld %d\n", bestres, rescount);
}