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

bool vis[5000005];
int k, n, m;
int fat[5000005], dis[5000005];
char inp[2005][2005];
long long A, B;
vector <int> G[5000005];
queue <int> Q;
map <long long, int> M;

void bfs(int w)
{
	vis[w] = true;
	fat[w] = -1;
	Q.push(w);
	
	while (!Q.empty())
	{
		int v = Q.front();
		Q.pop();
		
		for (auto u: G[v])
			if(!vis[u])
			{
				vis[u] = true;
				fat[u] = v;
				dis[u] = dis[v]+1;
				Q.push(u);
			}
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> inp[i][j];
	
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (inp[i][j] == '.')
			{
				if (inp[i+1][j] == '.')
				{
					G[(i-1)*m+j].push_back(i*m+j);
					G[i*m+j].push_back((i-1)*m+j);
				}
				if (inp[i][j+1] == '.')
				{	
					G[(i-1)*m+j].push_back((i-1)*m+j+1);
					G[(i-1)*m+j+1].push_back((i-1)*m+j);
				}
			}
	
	bfs(1);
	
	int v = n*m;
	while (fat[v] > 0)
	{
		if (v > fat[v])
			A++;
		else
			B++;
		
		v = fat[v];
	}
	
	for (int i = 0; i < k; i++)
	{
		long long a, b;
		cin >> a >> b;
		M[A*a + B*b]++;
	}
	
	auto res = M.begin();
	cout << res->first << ' ' << res->second << '\n';
}