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

int n, m, k;
bool tab[2009][2009];
int dis[2009][2009];
bool vis[2009][2009];
queue<pair<int, int>> Q;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};

bool check(int x, int y) {
	return x>=0 and x<n and y>=0 and y<m and tab[x][y] and !vis[x][y];
}

void bfs() {
	dis[0][0] = 0;
	vis[0][0] = true;
	Q.push({0, 0});
	while(Q.size()) {
		auto w = Q.front();
		Q.pop();
		for(int i=0;i<4;i++) {
			int x = w.first + dx[i];
			int y = w.second + dy[i];
			if(check(x, y)) {
				vis[x][y] = true;
				dis[x][y] = dis[w.first][w.second] + 1;
				Q.push({x, y});
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m >> k;
	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++) {
			char c;
			cin >> c;
			if(c == '.')
				tab[i][j] = true;
		}
	}
	bfs();
	int G = n+m-2 + (dis[n-1][m-1]-n-m+2)/2;
	int D = dis[n-1][m-1] - G;
	int ilosc = 0;
	long long suma = LLONG_MAX;
	for(int i=0;i<k;i++) {
		int a, b;
		cin >> a >> b;
		long long wynik = 1LL*a*G + 1LL*b*D;
		if(wynik == suma)
			ilosc++;
		if(wynik < suma) {
			suma = wynik;
			ilosc = 1;
		}
	}
	cout << suma << " " << ilosc << "\n";
}