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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pii = pair<int,int>;
using Pli = pair<ll, int>;
using Vi = vector<int>;
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define rep(i,b,e) for (int i = (b); i < (e); i++)
#define each(a,x) for (auto& a : (x))
#define all(x) (x).begin(),(x).end()
#define sz(x) int((x).size())
#define endl '\n'

Pii ori[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int inf = 1e9;
   
int n, m, k;
bool check(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void solve() {
    cin >> n >> m >> k;
    vector<string> grid(n);
    vector<vector<int>> dist(n, Vi(m, inf));
    for (int i = 0; i < n; i++) {
        cin >> grid[i];
    }
    dist[0][0] = 0;
    queue<Pii> Q;
    Q.push({0,0});
    while (!Q.empty()) {
        int x = Q.front().x;
        int y = Q.front().y;
        Q.pop();
        for (Pii o : ori) {
            int nx = x + o.x;
            int ny = y + o.y;
            if (check(nx, ny) && grid[nx][ny] == '.' && dist[nx][ny] == inf) {
                Q.push({nx, ny});
                dist[nx][ny] = dist[x][y] + 1;
            }
        }
    }
    Pli res = {1e18, 0};
    for (int i = 0; i < k; i++) {
        ll a, b;
        cin >> a >> b;
        ll movesA = n - 1 + m - 1;
        ll cand = movesA * a + (dist[n-1][m-1] - movesA)/2 * (a + b);
        if (cand < res.x) {
            res = {cand, 1};
        } else if (cand == res.x) {
            res.y++;
        }
    }
    cout << res.x << " " << res.y << endl;
}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cout << fixed << setprecision(10);
    int t = 1; 
    //cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
}