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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define HASK(S, x) (S.find(x) != S.end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std; 

const int maxN = 1 << 11;
const long long INF = 10000000000000000ll;

char T[maxN][maxN];
int dist[maxN][maxN];
vector <pair <int, int> > dx = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
queue <pair <int, int> > Q;

void solve()
{
	int n, m, q;
	scanf ("%d%d%d", &n, &m, &q);
	FOR(i, 0, n)
		scanf ("%s", T[i]);
	#define INS(i, j) Q.push({i, j}), T[i][j] = 'X'
	for (INS(0, 0); !Q.empty(); Q.pop())
	{
		auto [i, j] = Q.front();
		for (auto [a, b] : dx)
		{
			a += i, b += j;
			if (0 <= a and a < n and 0 <= b and b < m and T[a][b] == '.')
				dist[a][b] = dist[i][j] + 1, INS(a, b);
		}
	}
	
	dbg(dist, n, m);
	
	int& x = dist[n-1][m-1];
	
	dbg(x);
	
	int up = n+m-2;
	x -= up;
	assert(x % 2 == 0);
	up += x / 2;
	int down = x / 2;
	
	dbg(up), dbg(down);
	
	
	long long mn = INF;
	int ile = 0;
		
	while (q--)
	{
		int a, b;
		scanf ("%d%d", &a, &b);
		long long prop = 1ll * a * up +  1ll * b * down;
		if (mn > prop)
			mn = prop, ile = 0;
		if (mn == prop)
			ile++;
	}
	printf("%lld %d\n", mn, ile);
}
 
int main()
{
	int t = 1;
	//scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}