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
#define _CRT_SECURE_NO_WARNINGS 1
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <map>

using namespace std;

int mapa[2002][2002];

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

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

	char nl;
	scanf("%c", &nl);

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			char c;
			scanf("%c", &c);
			mapa[i][j] = c == 'X' ? -1 : 0;
		}
		scanf("%c", &nl);
	}

	/*n = 2000;
	m = 2000;
	k = 0;*/

	int length;
	queue<pair<int, int>> q;
	q.push(make_pair(0, 0));
	mapa[0][0] = -2;
	while (!q.empty())
	{
		auto t = q.front();
		q.pop();

		if (t.first > 0)
		{
			if (mapa[t.first - 1][t.second] == 0) 
			{
				mapa[t.first - 1][t.second] = 1;
				q.push(make_pair(t.first - 1, t.second));
			}
		}
		if (t.first < n - 1)
		{
			if ((mapa[t.first + 1][t.second] == 0) )
			{
				mapa[t.first + 1][t.second] = 3;
				q.push(make_pair(t.first + 1, t.second));
			}
		}
		if (t.second > 0)
		{
			if ((mapa[t.first][t.second - 1] == 0) )
			{
				mapa[t.first][t.second - 1] = 4;
				q.push(make_pair(t.first, t.second - 1));
			}
		}
		if (t.second < m - 1)
		{
			if ((mapa[t.first][t.second + 1] == 0) )
			{
				mapa[t.first][t.second + 1] = 2;
				q.push(make_pair(t.first, t.second + 1));
			}
		}
	}

	int si = n - 1;
	int sj = m - 1;
	long long upr = 0;
	long long dol = 0;

	while (!((si == 0) && (sj == 0)))
	{
		int d = mapa[si][sj];
		switch (d)
		{
		case 1:
			si++;
			dol++;
			break;
		case 2:
			sj--;
			upr++;
			break;
		case 3:
			si--;
			upr++;
			break;
		case 4:
			sj++;
			dol++;
			break;
		}
	}

	long long min = 922337203685477580;
	long long count =0 ;
	while (k-- > 0)
	{
		long long a, b;
		scanf("%lld%lld", &a, &b);
		long long result = upr * a + dol * b;
		if (result < min)
		{
			min = result;
			count = 0;
		}
		if (result == min)
		{
			count++;
		}
	}
	printf("%lld %lld", min, count);
}