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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define int long long
#define st first
#define nd second
using namespace std;

const int N(2e3 + 13), M(2e3 + 13), K(1e6 + 13);
pair<int, int> odl[N][M], field;
pair<int, int> speed[K], best;
queue<pair<int, int>> Q;

int hill[N][M];

int n, q, m, k, res = LLONG_MAX, bestGuy, countBesties;
char c;

bool checkField(int i, int j) {
  return i >= 1 && i <= n && j >= 1 && j <= m &&
         odl[i][j].st + odl[i][j].nd == 0 && hill[i][j];
}

void processAdj(int i, int j) {

  if (checkField(i + 1, j)) {
    odl[i + 1][j] = odl[i][j];
    odl[i + 1][j].st++;
    Q.push({i + 1, j});
  }
  if (checkField(i - 1, j)) {
    odl[i - 1][j] = odl[i][j];
    odl[i - 1][j].nd++;
    Q.push({i - 1, j});
  }
  if (checkField(i, j + 1)) {
    odl[i][j + 1] = odl[i][j];
    odl[i][j + 1].st++;
    Q.push({i, j + 1});
  }
  if (checkField(i, j - 1)) {
    odl[i][j - 1] = odl[i][j];
    odl[i][j - 1].nd++;
    Q.push({i, j - 1});
  }
}

int32_t main() {
  boost;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c;
      hill[i][j] = (c == 'X' ? 0 : 1);
    }
  }

  for (int i = 1; i <= k; i++) {
    cin >> speed[i].st >> speed[i].nd;
  }

  Q.push({1, 1});
  odl[1][1] = {1, 1};

  while (!Q.empty()) {
    field = Q.front();
    int i = field.st, j = field.nd;
    Q.pop();
    processAdj(i, j);
  }

  best = odl[n][m];
  best.st--;
  best.nd--;

  for (int i = 1; i <= k; i++) {
    int time = speed[i].st * best.st + speed[i].nd * best.nd;
    if (time < res) {
      res = time;
      countBesties = 1;
    } else if (time == res) {
      countBesties++;
    }
  }

  cout << res << " " << countBesties << endl;

  /*
  5 7 1
......X
X.X..X.
..X.X.X
.X.X...
.....X.
2 1

2 5 4
.X...
...X.
2 1
2 2
1 7
2 1





  */
}