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

typedef long long int LL;

const int MAXN = 2005;
const int INF = 1000000001;
const LL INFLL = 1000000000000000001LL;
const int DX[4] = {-1, 1, 0, 0};
const int DY[4] = {0, 0, 1, -1};

int n, m, q;
string M[MAXN];
int dist[MAXN][MAXN];
queue<pair<int,int>> Q;

inline bool inside(int x, int y) {
  return (x >= 0) && (x < n) && (y >= 0) && (y < m);
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);

  cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    cin >> M[i];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      dist[i][j] = INF;
  dist[0][0] = 0;
  Q.push(make_pair(0, 0));

  while (!Q.empty()) {
    int x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (int mv = 0; mv < 4; mv++) {
      int xp = x + DX[mv], yp = y + DY[mv];
      if (!inside(xp, yp)) continue;
      if (M[xp][yp] == 'X') continue;
      if (dist[xp][yp] != INF) continue;
       dist[xp][yp] = dist[x][y] + 1;
       Q.push(make_pair(xp, yp));
    }
  }

  int opt_path_a = (n + m - 2) + (dist[n-1][m-1] - (n + m - 2)) / 2;
  int opt_path_b = (dist[n-1][m-1] - (n + m - 2)) / 2;

  LL best_cost = INFLL;
  int count_best_cost = 0;
  for (int i = 0; i < q; i++) {
    LL a, b;
    cin >> a >> b;
    LL cost = opt_path_a * a + opt_path_b * b;
    if (cost < best_cost) {
      best_cost = cost;
      count_best_cost = 0;
    }
    if (cost == best_cost) count_best_cost++;
  }

  cout << best_cost << " " << count_best_cost << "\n";

  return 0;
}