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
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 2005;
const int INF = 1e9;
const ll INFl = 1e18;
int ox[4] = {1, -1, 0, 0};
int oy[4] = {0, 0, 1, -1};
int path[N][N];
bool vis[N][N];
char grid[N][N];
int n, m, k;

bool good(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '.';
}

void bfs(int x, int y) {
	queue<pii> Q;
	Q.push({x, y});
	vis[x][y] = true;
	while (!Q.empty()) {
		x = Q.front().f;
		y = Q.front().s;
		Q.pop();
		for (int i = 0; i < 4; i ++) {
			if (good(x + ox[i], y + oy[i]) && !vis[x + ox[i]][y + oy[i]]) {
				vis[x + ox[i]][y + oy[i]] = true;
				path[x + ox[i]][y + oy[i]] = path[x][y] + 1;
				Q.push({x + ox[i], y + oy[i]});
			}
		}
	}
}

int LG = 0, PD = 0;

void count_directions(int x, int y) {
	queue<pii> Q;
	Q.push({x, y});
	while (true) {
		x = Q.front().f;
		y = Q.front().s;
		//cout << x << ' ' << y << '\n';
		Q.pop();
		if (x == 0 && y == 0) return;
		for (int i = 0; i < 4; i ++) {
			int X = ox[i] + x;
			int Y = oy[i] + y;
			if (good(X, Y) && path[X][Y] == path[x][y] - 1) {
				if (i == 1 || i == 3) PD ++;
				else LG ++;
				Q.push({X, Y});
				break;
			}
		}
	}
}

int main(){
	std::ios::sync_with_stdio(false); 
	cout.tie(0);
	cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 0; i < n; i ++)
		for (int j = 0; j < m; j ++) {
			cin >> grid[i][j];
			if (grid[i][j] == 'X') path[i][j] = INF;
		}
	bfs(0, 0);
	//for (int i = 0; i < n; i ++) {
		//for (int j = 0; j < m; j ++) {
			//cout << path[i][j] << ' ';
		//}
		//cout << '\n';
	//}
	count_directions(n - 1, m - 1);
	//cout << LG << ' ' << PD << '\n';
	map<ll, int> mapka;
	ll mini = INFl;
	for (int i = 0; i < k; i ++) {
		int a, b;
		cin >> a >> b;
		ll wyn = 1LL * LG * b + 1LL * PD * a;
		mini = min(mini, wyn);
		mapka[wyn] ++;
	}
	cout << mini << ' ' << mapka[mini] << '\n';
	return 0;
}