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
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=2005;
const int inf=1e18+9;
char tab[MAX][MAX];
int dp[MAX][MAX];
bool O[MAX][MAX];
const int px[4]={0,0,1,-1};
const int py[4]={1,-1,0,0};
int n,m,z;
queue<pii>q;
bool check(int x,int y){
  return x>=1 && x<=n && y>=1 && y<=m && tab[x][y]!='X' && !O[x][y];
}
int ans1=inf,ans2=0;
int32_t main(){
  BOOST;
  cin>>n>>m>>z;
  for (int i=1;i<=n;i++){
    for (int j=1;j<=m;j++){
      cin>>tab[i][j];
      dp[i][j]=inf;
    } 
  } 
  dp[1][1]=0;
  O[1][1]=true;
  q.push(mp(1,1));
  while (!q.empty()){
    pii u=q.front();
    int x=u.st;
    int y=u.nd;
    q.pop();
    for (int i=0;i<4;i++){
      int tox=x+px[i];
      int toy=y+py[i];
      if (check(tox,toy)){
        dp[tox][toy]=dp[x][y]+1;
        q.push({tox,toy});
        O[tox][toy]=true;
      }
    }
  }
  int dist=dp[n][m];
  int dobrych=n+m-2;
  dist-=dobrych;
  dobrych+=dist/2;
  int zlych=dist/2;
  for (int t=0;t<z;t++){
    int a,b;
    cin>>a>>b;
    if (a*dobrych+b*zlych<ans1){
      ans1=a*dobrych+b*zlych;
      ans2=1;
    }
    else if (a*dobrych+b*zlych==ans1){
      ans2++;
    }
  } 
  cout<<ans1<<" "<<ans2;
  return 0; 
}