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
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " " << x << endl;

// #define int long long // uważaj

const int N = 3002;
const int mod = (1000 * 1000 * 1000) + 7;
int xx[] = {0, 1, 0, -1};
int yy[] = {1, 0, -1, 0};
int t[N][N];
long long db[N * N];
long long da[N * N];

void bfs(int n, int m) {
    queue <pair<int, int> > q;
    q.push(make_pair(0, 0));

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(t[i][j] != -1) t[i][j] = mod;
        }
    }

    t[0][0] = 0;

    while(!q.empty()) {
        pair<int, int> v = q.front();
        q.pop();

        for(int i = 0; i < 4; i++) {
            pair<int, int> w = v;
            w.first += xx[i];
            w.second += yy[i];
            if(w.first < 0 or w.first >= n) continue;
            if(w.second < 0 or w.second >= m) continue;

            if(t[w.first][w.second] != -1 and t[w.first][w.second] > t[v.first][v.second] + 1) {
                q.push(w);
                t[w.first][w.second] = t[v.first][v.second] + 1;
            }
        }
    }
}

void solve() {
    int n, m, k;
    cin >> n >> m >> k;

    for(int i = 0; i < n; i++) {
        string str;
        cin >> str;
        for(int j = 0; j < m; j++) {
            if(str[j] == 'X') t[i][j] = -1;
        }
    }

    bfs(n, m);
    int b = (t[n - 1][m - 1] - (n - 1) - (m - 1)) / 2;
    int a = (n - 1) + (m - 1) + b;
    assert(a + b == t[n - 1][m - 1]);

    long long ans = mod * (long long)mod;
    for(int i = 0; i < k; i++) {
        cin >> da[i] >> db[i];
        ans = min(ans, da[i] * a + db[i] * b);
    }

    cout << ans << " ";

    int ile = 0;
    for(int i = 0; i < k; i++) {
        if(ans == da[i] * a + db[i] * b) ile++;
    }

    cout << ile << "\n";
}

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int test = 1;

	while(test--){
		solve();
	}

	return 0;
}