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
#define pb push_back
#define mp make_pair
#define fi first
#define se second 
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 2969, INF = 2e17+6969;

int n,m,k;
char b[nax][nax];
PII t[nax][nax];
bool vis[nax][nax];
queue <PII> q;

bool good(int x, int y)
{
	if(x < 1 || x > n || y < 1 || y > m) return false;
	return true;
}

int main()
{
	//  scanf("%d%d%d", &n, &m, &k);
	boost;
	cin >> n >> m >> k;
	for(int i=1;i<=n;++i)
	{
		string S;
		cin >> S;
		for(int j=0;j<m;++j) b[i][j+1] = S[j];
	}
	t[1][1] = mp(0,0);
	vis[1][1] = 1;
	q.push(mp(1,1));
	while(!q.empty())
	{
		int A,B;
		A = q.front().fi;
		B = q.front().se;
		q.pop();
		int X[] {1,-1,0,0};
		int Y[] {0,0,1,-1};
		for(int opt=0;opt<4;++opt)
		{
			int x = A + X[opt];
			int y = B + Y[opt];
			if(vis[x][y] == 1 || (!good(x,y)) || b[x][y] == 'X') continue;
			vis[x][y] = 1;
			q.push(mp(x,y));
			PII tmp = t[A][B];
			if((x+y) > (A+B)) t[x][y] = mp(tmp.fi, tmp.se+1);
			else t[x][y] = mp(tmp.fi + 1, tmp.se);
		}
	}
	//  for(int i=1;i<=n;++i)
	//  {
		//  for(int j=1;j<=m;++j) printf("(%d, %d) ", t[i][j].fi, t[i][j].se);
		//  puts("");
	//  }
	ll ans = INF;
	int cnt = 0;
	while(k--)
	{
		ll a,b;
		//  scanf("%lld%lld", &a, &b);
		cin >> a >> b;
		swap(a,b);
		ll tmp = a*t[n][m].fi + b*t[n][m].se;
		if(tmp < ans)
		{
			ans = tmp;
			cnt = 1;
		}
		else if(tmp == ans)
		{
			cnt++;
		}
	}
	//  printf("%lld %d\n", ans, cnt);
	cout << ans << " " << cnt << "\n";
	return 0;
}