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

using namespace std;
const int N = 2 * 1000 + 7;
const int M = 1000 * 1000 + 7;
typedef long long ll;
ll odlp[N][N], odlz[N][N];
int o[N][N], czy[N][N];
pair<ll, ll> p[M];
queue<pair<int, int>> kol;
vector<pair<int, int>> roza;

void BFS(int n, int m)
{
	int i, j, ni, nj, k;
	o[1][1] = 1;
	odlp[1][1] = 0;
	odlz[1][1] = 0;
	kol.push(make_pair(1, 1));
	while(kol.size() > 0)
	{
		i = kol.front().first;
		j = kol.front().second;
		//cout << i << " " << j << " " << odlp[i][j] << " " << odlz[i][j] << "\n";
		kol.pop();
		for(k = 0; k < (int)roza.size(); ++k)
		{
			ni = roza[k].first + i;
			nj = roza[k].second + j;
			if(ni > n || ni < 1 || nj > m || nj < 1 || czy[ni][nj] == 1 || o[ni][nj] == 1)
				continue;
			odlp[ni][nj] = odlp[i][j];
			odlz[ni][nj] = odlz[i][j];
			o[ni][nj] = 1;
			if(roza[k].first == 1 || roza[k].second == 1)
				++odlp[ni][nj];
			else
				++odlz[ni][nj];
			kol.push(make_pair(ni, nj));
		}
	}
}

void Wynik(int k, int n, int m)
{
	int i, il;
	ll min;
	il = 0;
	min = 100000000000000000ll;
	for(i = 1; i <= k; ++i)
	{
		if(odlp[n][m] * p[i].first + odlz[n][m] * p[i].second == min)
			++il;
		if(odlp[n][m] * p[i].first + odlz[n][m] * p[i].second < min)
		{
			min = odlp[n][m] * p[i].first + odlz[n][m] * p[i].second;
			il = 1;
		}
	}
	cout << min << " " << il << "\n";
}

void WczytajLiczby(int &n, int &m, int &k)
{
	int i, j;
	char z;
	cin >> n >> m >> k;
	for(i = 1; i <= n; ++i)
	{
		for(j = 1; j <= m; ++j)
		{
			cin >> z;
			if(z == 'X')
				czy[i][j] = 1;
		}
	}
	for(i = 1; i <= k; ++i)
		cin >> p[i].first >> p[i].second;
	roza.push_back(make_pair(1, 0));
	roza.push_back(make_pair(0, 1));
	roza.push_back(make_pair(-1, 0));
	roza.push_back(make_pair(0, -1));
}

void Wycieczka()
{
	int n, m, k;
	WczytajLiczby(n, m, k);
	BFS(n, m);
	Wynik(k, n, m);
}

int main ()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	Wycieczka();
	
	return 0;
}