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
#include <bits/stdc++.h>
#define nd second
#define mk std::make_pair
#define st first
#define ll long long


const int MAX_N = 2000;
const int inf = 1e9;

ll n, m, k;
char t[MAX_N+3][MAX_N+3];
int d[MAX_N+3][MAX_N+3];

void input() {
    std::cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            std::cin >> t[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            d[i][j] = inf;
}

int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1};

void bfs() {
    d[1][1] = 0;
    std::queue<std::pair<int, int> > q;
    q.push(mk(1, 1));
    while (!q.empty()) {
        int x = q.front().st;
        int y = q.front().nd;
        q.pop();
        for (int l = 0; l < 4; l++) {
            int xx = x + dx[l];
            int yy = y + dy[l];
            if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && d[xx][yy] == inf && t[xx][yy] == '.') {
                d[xx][yy] = d[x][y] + 1;
                q.push(mk(xx, yy));
            } 
        }
    }
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
    input();
    bfs();
    ll L = d[n][m];

    ll dol = (L - (n+m-2)) / 2;
    ll gora = L - dol;

    ll min = LLONG_MAX;
    int cnt = 0;
    for (int i = 1; i <= k; i++) {
        ll a, b;
        std::cin >> a >> b;
        ll wyn = gora * a + dol * b;
        if (wyn < min) {
            min = wyn;
            cnt = 1;
        } else if (wyn == min) 
            cnt++;
    }
    std::cout << min << " " << cnt << "\n";
}