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
#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
#define S 2007

using namespace std;
typedef long long ll;
int odw[S][S];

queue < pair < int , int > > Q;

int n,m;

void BFS(){
    Q.push({1,1});
    while(!Q.empty()){
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();

        if(x > n || x < 1 || y > m || y < 1)
            continue;

        if(!odw[x+1][y]){
            odw[x+1][y] = odw[x][y] + 1;
            Q.push({x+1,y});
        }
        if(!odw[x-1][y]){
            odw[x-1][y] = odw[x][y] + 1;
            Q.push({x-1,y});
        }
        if(!odw[x][y+1]){
            odw[x][y+1] = odw[x][y] + 1;
            Q.push({x,y+1});
        }
        if(!odw[x][y-1]){
            odw[x][y-1] = odw[x][y] + 1;
            Q.push({x,y-1});
        }
    }
}
string s;
int main(void){
    int q;
    scanf("%d %d %d",&n,&m,&q);
    for(int i = 1; i <= n;i++){
        cin >> s;
        for(int j = 1; j <= m;j++){
            if(s[j-1] == 'X'){
                odw[i][j] = 1;
            }
        }
    }
    BFS();
    int x = 0,y = 0;
    odw[n][m] -= (n-1+m-1);
    x = (n-1+m-1);
    x += odw[n][m]/2;
    y += odw[n][m]/2;
    ll a,b;
    ll mi = 1e18;
    int lic = 0;
    for(int i = 1; i <= q;i++){
        scanf("%lld %lld",&a,&b);
        ll p = a*x + b*y;
        if(p < mi){
            mi = p;
            lic = 1;
        }else if(p == mi){
            lic++;
        }
        //printf("%lld\n",a*x + b*y);
    }
    printf("%lld %d\n",mi,lic);
    return 0;
}