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
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int>> D(n+2, vector<int>(m+2, -1));
    for (int i=1; i<=n; i++) {
        string str;
        cin >> str;
        for (int j=1; j<=m; j++) {
            if (str[j-1]=='.') D[i][j]=INT_MAX;
        }
    }
    queue<array<int, 2>> Q;
    D[1][1]=0;
    Q.push({1, 1});
    
    auto go = [&](int i, int j, int d) {
        if (d<D[i][j]) {
            D[i][j]=d;
            Q.push({i, j});
        }
    };
    
    while (!Q.empty()) {
        auto [i, j] = Q.front();
        Q.pop();
        go(i-1, j, D[i][j]+1);
        go(i+1, j, D[i][j]+1);
        go(i, j-1, D[i][j]+1);
        go(i, j+1, D[i][j]+1);
    }

    long long resM=LLONG_MAX, resC=0;
    while (k--) {
        long long a, b;
        cin >> a >> b;
        long long r=a*(n-1+m-1) + (a+b)*(D[n][m]-(n-1+m-1))/2;
        if (r<resM) resM=r, resC=1;
        else if (r==resM) resC++;
    }
    cout << resM << ' ' << resC << endl;
    return 0;
}