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
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int NM_MAX = 2000;

int n, m;

bool polePoprawne(int a, int b) {
  return 0 <= a && a < n && 0 <= b && b < m;
}

queue<pair<int, int>> kolejka;

bool odwiedzone[NM_MAX][NM_MAX];
int ilosc[NM_MAX][NM_MAX][2];

int offsetN[] = {1, -1, 0, 0};
int offsetM[] = {0, 0, 1, -1};
int rodzaj[] = {0, 1, 0, 1};

void BFS() {
  odwiedzone[0][0] = true;
  kolejka.push(make_pair(0, 0));

  while (!kolejka.empty()) {
    pair<int, int> v = kolejka.front();
    kolejka.pop();

    for (int i = 0; i < 4; i++) {
      pair<int, int> w = make_pair(v.first + offsetN[i], v.second + offsetM[i]);
      if (!polePoprawne(w.first, w.second) || odwiedzone[w.first][w.second])
        continue;

      odwiedzone[w.first][w.second] = true;
      kolejka.push(make_pair(w.first, w.second));

      for (int j = 0; j < 2; j++)
        ilosc[w.first][w.second][j] = ilosc[v.first][v.second][j];

      ilosc[w.first][w.second][rodzaj[i]]++;
    }
  }
}

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

  int k;
  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == 'X')
        odwiedzone[i][j] = true;
    }
  }

  BFS();

  long long naj = -1;
  int iloscNaj = 0;

  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    long long wynik = (long long)ilosc[n - 1][m - 1][0] * a +
                      (long long)ilosc[n - 1][m - 1][1] * b;
    if (wynik < naj || naj == -1) {
      naj = wynik;
      iloscNaj = 1;
    }
    else if (wynik == naj) {
      iloscNaj++;
    }
  }

  cout << naj << " " << iloscNaj << "\n";
  return 0;
}