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

const int INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000;

void BFS(vector<vector<int>> &gora, vector<vector<int>> &dol,
         const vector<vector<bool>> &moszna, int n, int m) {
  queue<pair<int, int>> q;
  vector<vector<bool>> vis(n, vector<bool>(m));

  q.push({0, 0});
  gora[0][0] = 0;
  dol[0][0] = 0;
  vis[0][0] = 1;
  while (!q.empty()) {
    auto &p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;
    vector<pair<int, int>> nei = {
        {x, y - 1}, {x, y + 1}, {x - 1, y}, {x + 1, y}};
    for (const auto &p : nei) {
      int new_x = p.first;
      int new_y = p.second;
      if (0 <= new_x && new_x < n && 0 <= new_y && new_y < m &&
          moszna[new_x][new_y] && !vis[new_x][new_y]) {
        vis[new_x][new_y] = true;
        q.push(p);
        if (new_x > x || new_y > y) {
          gora[new_x][new_y] = gora[x][y] + 1;
          dol[new_x][new_y] = dol[x][y];
        } else {
          gora[new_x][new_y] = gora[x][y];
          dol[new_x][new_y] = dol[x][y] + 1;
        }
      }
    }
  }
}
signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<bool>> mapa(n, vector<bool>(m));
  vector<vector<int>> odl1(n, vector<int>(m));
  vector<vector<int>> odl2(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char x;
      cin >> x;
      mapa[i][j] = (x == '.');
      odl1[i][j] = INF;
      odl2[i][j] = INF;
    }
  }
  BFS(odl1, odl2, mapa, n, m);
  int best_ziomki = 0;
  int best_res = INF;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    if (odl1[n - 1][m - 1] * a + odl2[n - 1][m - 1] * b < best_res) {
      best_res = odl1[n - 1][m - 1] * a + odl2[n - 1][m - 1] * b;
      best_ziomki = 1;
    } else if (odl1[n - 1][m - 1] * a + odl2[n - 1][m - 1] * b == best_res) {
      best_ziomki++;
    }
  }
  cout << best_res << " " << best_ziomki << endl;
  return 0;
}