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

using namespace std;

#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;

char g[2007][2007];
bool odw[2007][2007];
deque<pair<pair<int,int>,pair<ll,ll>>>q;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>g[i][j];
            if(g[i][j]=='X') odw[i][j]=1;
        }
    }
    q.pb({{1,1},{0,0}});
    odw[1][1]=1;
    ll ile1,ile2;
    while(!q.empty())
    {
        int x=q[0].st.st,y=q[0].st.nd;
        ll c1=q[0].nd.st,c2=q[0].nd.nd;
        q.pop_front();
        if(x==0||x>n||y==0||y>m) continue;
        if(x==n&&y==m)
        {
            ile1=c1;
            ile2=c2;
            break;
        }
        if(!odw[x+1][y])
        {
            odw[x+1][y]=1;
            q.pb({{x+1,y},{c1+1,c2}});
        }
        if(!odw[x-1][y])
        {
            odw[x-1][y]=1;
            q.pb({{x-1,y},{c1,c2+1}});
        }
        if(!odw[x][y+1])
        {
            odw[x][y+1]=1;
            q.pb({{x,y+1},{c1+1,c2}});
        }
        if(!odw[x][y-1])
        {
            odw[x][y-1]=1;
            q.pb({{x,y-1},{c1,c2+1}});
        }
    }
    ll ans=infl,a1=0,a,b;
    while(k--)
    {
        cin>>a>>b;
        if(a*ile1+b*ile2<ans)
        {
            ans=a*ile1+b*ile2;
            a1=1;
        }
        else if(a*ile1+b*ile2==ans) a1++;
    }
    cout<<ans<<" "<<a1;





    return 0;
}