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
#include <iostream>
#include <queue>
using namespace std;
bool mapa[2001][2001];
pair<int, int> znajomi[1000001];
queue < pair<pair<int, int>, int>>trasy;
pair<bool, int> visited[2001][2001];
int wyniki[1000001];
int n, m;
int bfs(int z) {
	while (trasy.size() != 0) {
		trasy.pop();
	}
	bool czy=0;
	int min;
	for (int a = 0; a < 2000; a++) {
		for (int b = 0; b < 2000; b++) {
			visited[a][b].first = 0;
		}
	}
	trasy.push(make_pair(make_pair(0, 0), 0));
	while (trasy.size() != 0) {
		visited[trasy.front().first.first][trasy.front().first.second].first = 1;
		visited[trasy.front().first.first][trasy.front().first.second].second = trasy.front().second;
		if (trasy.front().first.first == n-1&&trasy.front().first.second==m-1) {
			if (czy==1) {
				if (trasy.front().second < min) {
					min = trasy.front().second;
				}
			}
			else { min = trasy.front().second; czy = 1; }
		}
		else {
			if (trasy.front().first.first > 0) {
				if (mapa[trasy.front().first.first - 1][trasy.front().first.second] == 1) {
					if (visited[trasy.front().first.first - 1][trasy.front().first.second].first == 0) {
						trasy.push(make_pair(make_pair(trasy.front().first.first - 1, trasy.front().first.second), trasy.front().second + znajomi[z].second));
					}
					else {
						if (visited[trasy.front().first.first - 1][trasy.front().first.second].second > trasy.front().second+znajomi[z].second) {
							trasy.push(make_pair(make_pair(trasy.front().first.first - 1, trasy.front().first.second), trasy.front().second + znajomi[z].second));
						}
					}
				}
			}
			if (trasy.front().first.first < n-1) {
				if (mapa[trasy.front().first.first + 1][trasy.front().first.second] == 1) {
					if (visited[trasy.front().first.first + 1][trasy.front().first.second].first == 0) {
						trasy.push(make_pair(make_pair(trasy.front().first.first + 1, trasy.front().first.second), trasy.front().second + znajomi[z].first));
					}
					else {
						if (visited[trasy.front().first.first + 1][trasy.front().first.second].second > trasy.front().second + znajomi[z].first) {
							trasy.push(make_pair(make_pair(trasy.front().first.first + 1, trasy.front().first.second), trasy.front().second + znajomi[z].first));
						}
					}
				}
			}
			if (trasy.front().first.second > 0) {
				if (mapa[trasy.front().first.first][trasy.front().first.second-1] == 1) {
					if (visited[trasy.front().first.first][trasy.front().first.second - 1].first == 0) {
						trasy.push(make_pair(make_pair(trasy.front().first.first, trasy.front().first.second - 1), trasy.front().second + znajomi[z].second));
					}
					else {
						if (visited[trasy.front().first.first][trasy.front().first.second - 1].second > trasy.front().second + znajomi[z].second) {
							trasy.push(make_pair(make_pair(trasy.front().first.first, trasy.front().first.second-1), trasy.front().second + znajomi[z].second));
						}
					}
				}
			}
			if (trasy.front().first.second < m-1) {
				if (mapa[trasy.front().first.first][trasy.front().first.second + 1] == 1) {
					if (visited[trasy.front().first.first][trasy.front().first.second+1].first == 0) {
						trasy.push(make_pair(make_pair(trasy.front().first.first, trasy.front().first.second + 1), trasy.front().second + znajomi[z].first));
					}
					else {
						if (visited[trasy.front().first.first][trasy.front().first.second + 1].second > trasy.front().second + znajomi[z].first) {
							trasy.push(make_pair(make_pair(trasy.front().first.first, trasy.front().first.second + 1), trasy.front().second + znajomi[z].first));
						}
					}
				}
			}
		}
		trasy.pop();
	}
	return min;
}
int main() {
	int k;
	char z;
	cin >> n >> m >> k;
	for (int a = 0; a < n; a++) {
		for (int b = 0; b < m; b++) {
			cin >> z;
			if (z == '.') { mapa[a][b] = 1; }
			else {
				mapa[a][b] = 0;
			}
		}
	}
	for (int a = 0; a < k; a++) {
		cin >> znajomi[a].first >> znajomi[a].second;
	}
	for (int a = 0; a < k; a++) {
		wyniki[a] = bfs(a);
	}
	int min=0, il=0;
	min = wyniki[0];
	il = 1;
	for (int a = 1; a < k; a++) {
		if (wyniki[a] < min) {
			min = wyniki[a];
			il = 1;
		}
		else {
			if (wyniki[a] == min) {
				il++;
			}
		}
	}
	cout << min << ' ' << il;
}