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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define PB push_back
#define mp make_pair
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using ll = long long;
#define debug() cout<<"!XD"<<endl
constexpr ll inf = 1e18+2;


int t[2005][2005], n, m, k;
ll a, b;
ll res1, res2;
bool vis[2005][2005];
vector<pair<ll, ll> > kraw;
string c;

void bfs(int x, int y, int r1, int r2){
    queue< pair< pair<int, int>, pair<int, int> > > q;
    q.push({{x, y}, {r1, r2}});
    while(!q.empty()){
        pair<int, int> wsp=q.front().st;
        pair<int, int> il=q.front().nd;
        vis[wsp.st][wsp.nd]=1;
        q.pop();
        if(wsp.st==n && wsp.nd==m){
            res1=il.st;
            res2=il.nd;
            break;
        }
        if(!vis[wsp.st-1][wsp.nd] && !t[wsp.st-1][wsp.nd]){
            q.push({{wsp.st-1, wsp.nd}, {il.st, il.nd+1}});
        }
        if(!vis[wsp.st+1][wsp.nd] && (!t[wsp.st+1][wsp.nd])){
            q.push({{wsp.st+1, wsp.nd}, {il.st+1, il.nd}});
        }
        if(!vis[wsp.st][wsp.nd-1] && (!t[wsp.st][wsp.nd-1])){
            q.push({{wsp.st, wsp.nd-1}, {il.st, il.nd+1}});
        }
        if(!vis[wsp.st][wsp.nd+1] && (!t[wsp.st][wsp.nd+1])){
            q.push({{wsp.st, wsp.nd+1}, {il.st+1, il.nd}});
        }
    }
}

int main()
{
    fastio
    cin>>n>>m>>k;
    for(int i=1; i<=n; ++i){
        cin>>c;
        for(int j=0; j<m; ++j){
            if(c[j]=='X'){
                t[i][j+1]=1;
            }
        }
    }
    for(int i=0; i<=n+1; ++i){
        t[i][0]=1;
        t[i][m+1]=1;
    }
    for(int i=1; i<=m; ++i){
        t[0][i]=1;
        t[n+1][i]=1;
    }


    for(int i=1; i<=k; ++i){
        cin>>a>>b;
        kraw.PB({a, b});
    }


    bfs(1, 1, 0, 0);

    ll wyn = inf;
    int cnt=0;
    ll safe;
    for(auto x: kraw){
        safe=(res1*x.st);
        safe+=(res2*x.nd);
        wyn=min(wyn, safe);
    }
    for(auto x: kraw){
        safe=(res1*x.st);
        safe+=(res2*x.nd);
        if(safe==wyn){
            cnt++;
        }
    }
    cout<<wyn<<" "<<cnt<<'\n';
    return 0;
}

/**
5 5 3
.X...
.X.X.
.X.X.
.X.X.
...X.
1 3
3 1
3 3


5 5 10
.x.x.
...x.
..x..
x....
...x.
214 515
213 6236
26362 32616
236236 266262
26362 26367
23667 85452
2136 71438
16631 452845
13461346 51377
1346643 631461
**/