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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
vector<vector<int> > tree;
vector<bool> vis;
vector<int> dis;
int depth = 0;
int len = 0;
void BFS(int x) {
        queue <pair <int,int> > q;
        q.push({x,depth});
        vis[x] = true;
        while(!q.empty()) {
                pair <int,int> a = q.front();
                dis[a.first] = a.second;
                q.pop();
                for(auto u:tree[a.first]) {
                        if(vis[u] == false) {
                                q.push({u,a.second+1});
                                vis[u] = true;
                        }
                }
        }
}
int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        int n,m,k;
        cin >> n >> m >> k;

        tree.resize(n*m);vis.resize(n*m);dis.resize(n*m);
        vector<vector <char> > grid(n,vector<char>(m));
        vector <LL> up(k),down(k);
        REP(i,n) REP(j,m)
                cin >> grid[i][j];
        REP(i,k)
                cin >> up[i] >> down[i];
        REP(i,n) REP(j,m) {
                if(grid[i][j] == '.') {
                        if(j != 0 && grid[i][j-1] != 'X') {
                                tree[i*m+j].emplace_back(i*m+(j-1));
                                tree[i*m+(j-1)].emplace_back(i*m+j);
                        }
                        if( i!= 0 && grid[i-1][j] != 'X') {
                                tree[i*m+j].emplace_back((i-1)*m+j);
                                tree[(i-1)*m+j].emplace_back(i*m+j);
                        }
                }
        }
        BFS(0);
        LL total = dis[n*m-1];
        LL wyn = 1e18,count = 1;
        REP(i,k) {
                LL time = (n+m-2)*up[i] + (total-(n+m-2))*(down[i]+up[i])/2 ;
                if(time < wyn) {
                        count = 1;
                        wyn = time;
                }
                else if(time == wyn) count++;
        }
        cout << wyn << " " << count;
}