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
#include <bits/stdc++.h>
#define ii pair<int, int>
#define ff first
#define ss second
using namespace std;

struct W {
  vector<int> sa;
  vector<int> sb;
  int oa;
  int ob;
  bool o;
};

void bfs(vector<W>& v, int a, int b) {

  queue<pair<int, ii>> q;
  q.push({a, {0, 0}});

  while(!q.empty()) {
    int i = q.front().ff;
    ii d = q.front().ss;
    q.pop();
    if(v[i].o) continue;
    v[i].o = true;
    v[i].oa = d.ff;
    v[i].ob = d.ss;
    if(i == b) return;
    for(int j: v[i].sa) {
      q.push({j, {d.ff+1, d.ss}});
    }
    for(int j: v[i].sb) {
      q.push({j, {d.ff, d.ss+1}});
    }
  }

}

int main() {

  int n, m, k, x, y;
  scanf("%d %d %d\n", &n, &m, &k);

  vector<bool> pop(m, false);

  vector<W> v(n*m);

  char c;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      scanf("%c", &c);
      if(c == 'X') {
        pop[j] = false;
        continue;
      }
      if(j > 0 && pop[j-1]) {
        v[i*m+j-1].sa.push_back(i*m+j);
        v[i*m+j].sb.push_back(i*m+j-1);
      }
      if(i > 0 && pop[j]) {
        v[(i-1)*m+j].sa.push_back(i*m+j);
        v[i*m+j].sb.push_back((i-1)*m+j);
      }
      pop[j] = true;
    }
    scanf("\n");
  }

  bfs(v, 0, n*m-1);

  int a = v[n*m-1].oa;
  int b = v[n*m-1].ob;

  long long ma = LLONG_MAX;;
  int mi = 0;

  for(int i = 0; i < k; i++) {
    scanf("%d %d", &x, &y);
    long long w = (long long)a*x + (long long)b*y;
    if(w == ma) {
      mi++;
    }
    if(w < ma) {
      ma = w;
      mi = 1;
    }
  }

  printf("%lld %d", ma, mi);

}