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

using ll = long long;

const int N = 2000 + 5;
const int INF = N * N + 1;

int n, m, k;
char mapa[N][N];
int dist[N][N];

const int M = 4;
int dx[M] = {-1, 0, 0, 1};
int dy[M] = {0, -1, 1, 0};

bool available(int x, int y) {
  return 0 <= x && x < n && 0 <= y && y < m && mapa[x][y] == '.';
}

void bfs() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dist[i][j] = INF;
    }
  }

  dist[0][0] = 0;
  queue<pair<int,int>> que;
  que.emplace(0, 0);
  
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();

    for (int i = 0; i < M; i++) {
      int x1 = x + dx[i];
      int y1 = y + dy[i];

      if (available(x1, y1) && dist[x][y] + 1 < dist[x1][y1]) {
        dist[x1][y1] = dist[x][y] + 1;
        que.emplace(x1, y1);
      }
    }
  }
}

void answer(vector<pair<int,int>> &queries) {
  ll best = 1e18;
  int cnt = 0;
  
  int min_d = n + m - 2;
  int d = dist[n - 1][m - 1];
  ll up = min_d + (d - min_d) / 2;
  ll down = (d - min_d) / 2;

  for (auto q : queries) {
    ll score = q.first * up + q.second * down;
    if (score < best) {
      best = score;
      cnt = 1;
    }
    else if (score == best) {
      cnt++;
    }
  }

  cout << best << " " << cnt << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> mapa[i];
  }

  vector<pair<int,int>> queries(k);
  for (int i = 0; i < k; i++) {
    cin >> queries[i].first >> queries[i].second;
  }

  bfs();
  answer(queries);
}