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
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define IOS cin.tie(0); ios_base::sync_with_stdio(0)
 
using ll = long long;
using namespace std;

const int N = 2100;

int dr[] = {1, 0, 0, -1};
int dc[] = {0, 1, -1, 0};
int dx[] = {0, 0, 1, 1};
int n, m, k, dis[N][N];
char s[N][N];
deque<pair<int, int>> q;

bool in(int r, int c) {
	return 1 <= r && r <= n && 1 <= c && c <= m;
}

int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; ++i) 
		scanf("%s", s[i] + 1);
	memset(dis, 0x3f, sizeof dis);
	q.push_back({1, 1});
	dis[1][1] = 0;
	while (!q.empty()) {
		auto [r, c] = q.front();
		q.pop_front();
		for (int i = 0; i < 4; ++i) {
			int nr = r + dr[i];
			int nc = c + dc[i];
			if (!in(nr, nc) || s[nr][nc] == 'X') continue;
			int ndis = dis[r][c] + dx[i];
			if (ndis < dis[nr][nc]) {
				dis[nr][nc] = ndis;
				if (dx[i] == 0)
					q.push_front({nr, nc});
				else
					q.push_back({nr, nc});
			}
		}
	}
	ll res = 1e18, cnt = 0;
	while (k--) {
		ll a, b;
		scanf("%lld%lld", &a, &b);
		ll cost = (n + m - 2 + dis[n][m]) * a + dis[n][m] * b;
		if (cost < res) {
			res = cost;
			cnt = 0;
		}
		if (cost == res)
			cnt++;
	}
	printf("%lld %lld\n", res, cnt);
	return 0;
}