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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

char map[2000][2000];

struct Node {
  vector<Node*> neighbours;
  int distance;
  bool visited;
  int x;
  int y;

  Node() {}

  void get_neighbours(int x, int y, int n, int m, Node graph[2000][2000]) {
    this->x = x;
    this->y = y;
    this->visited = false;
    this->distance = 0;

    if (map[y][x] == 'X') {
      return;
    }

    if (x > 0 && map[y][x-1] == '.') {
      // printf("x%dy%d -> x%dy%d\n", x, y, x-1, y);
      this->neighbours.push_back(&graph[y][x-1]);
    }
    if (x < m-1 && map[y][x+1] == '.') {
      // printf("x%dy%d -> x%dy%d\n", x, y, x+1, y);
      this->neighbours.push_back(&graph[y][x+1]);
    }

    if (y > 0 && map[y-1][x] == '.') {
      // printf("x%dy%d -> x%dy%d\n", x, y, x, y-1);
      this->neighbours.push_back(&graph[y-1][x]);
    }
    if (y < n-1 && map[y+1][x] == '.') {
      // printf("x%dy%d -> x%dy%d\n", x, y, x, y+1);
      this->neighbours.push_back(&graph[y+1][x]);
    }
  }
};

int bfs(Node &start, Node &destination) {
  queue<Node*> nodes;
  nodes.push(&start);
  start.visited = true;

  while (!nodes.empty()) {
    Node* node = nodes.front();
    nodes.pop();

    for (int i=0; i<node->neighbours.size(); i++) {
      if (!node->neighbours[i]->visited) {
        nodes.push(node->neighbours[i]);
        node->neighbours[i]->visited = true;
        node->neighbours[i]->distance = node->distance + 1;
      }
    }
  }

  return 0;
}

Node graph[2000][2000];

void print_map(char map[2000][2000], int n, int m) {
  for (int i=0; i<n; i++) {
    for (int j=0; j<m; j++) {
      printf("%c", map[i][j]);
    }
    puts("");
  }
}

void print_graph(int n, int m) {
  for (int i=0; i<n; i++) {
    for (int j=0; j<m; j++) {
      printf("%d ", graph[i][j].distance);
    }
    puts("");
  }
}

int main() {
  long long n, m, k;

  scanf("%lld %lld %lld", &n, &m, &k);

  for (int i=0; i<n; i++) {
    for (int j=0; j<m; j++) {
      scanf(" %c", &map[i][j]);
    }
  }

  for (int i=0; i<n; i++) {
    for (int j=0; j<m; j++) {
      graph[i][j].get_neighbours(j, i, n, m, graph);
    }
  }

  bfs(graph[0][0], graph[n-1][m-1]);

  int distance = graph[n-1][m-1].distance;
  long long min_time = -1;
  int min_amount = 0;

  // print_graph(n, m);
  // printf("%d ", distance);

  for (int i=0; i<k; i++) {
    long long a, b;

    scanf("%lld %lld", &a, &b);

    long long time = (n + m - 2)*a + ((distance - (n + m - 2))/2) * (a + b);

    if (min_time == -1 || min_time > time) {
      min_time = time;
      min_amount = 1;
    } else if (min_time == time) {
      min_amount++;
    }
  }
  printf("%lld %d", min_time, min_amount);
}