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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define st first
#define nd second
#define vt vector
#define DEBUG if(1)
#define VAR(__var)  #__var << ": " << __var << " "
#define PARA(__var) #__var << ": " << __var.first << ", " << __var.second << " " 
#define FOR(__var, __start, __end)  for(int __var=__start; __var<__end; ++__var)
#define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var)
#define all(__var)    (__var).begin(),(__var).end()
#define rall(__var)   (__var).rbegin(),(__var).rend()
#define sz(__var)     (int)(__var).size()
#define satori(__var) int __var; cin>>__var; while(__var--)

using namespace std;

using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const ll INF = 1e18+2137;

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 	
	int n, m, k;
	cin >> n >> m >> k;
	
	vt<vt<bool>> a(n+1, vt<bool>(m+1, 0));
	vt<vt<bool>> vis(n+1, vt<bool>(m+1, 0));
	unordered_map<ll, int> s;
	
	FOR(i, 0, n)
		FOR(j, 0, m)
		{
			char tmp;
			cin >> tmp;
			if(tmp == 'X')
				a[i][j] = 1;
		}
	
	queue<tuple<int, int, int, int>> q;
	q.push({0,0,0,0});
	pll ans;
	ll res = INF;
	
	auto go = [&](int x, int y, int rx, int ry)
	{
		if(x < 0 || y < 0 || x >= n || y >= m)
			return;
		
		if(vis[x][y])
			return;
		
		if(x == n-1 && y == m-1)
		{
			ans.st = rx;
			ans.nd = ry;
			vis[x][y] = 1;
		} 
		
		if(a[x][y])
			return;
		
		vis[x][y] = 1;
		q.push({x, y, rx, ry});
	};
	
	while(!q.empty())
	{	
		int x = get<0>(q.front());
		int y = get<1>(q.front());
		int rx = get<2>(q.front());
		int ry = get<3>(q.front());
		q.pop();
		go(x+1, y, rx+1, ry);
		go(x-1, y, rx, ry+1);
		go(x, y+1, rx+1, ry);
		go(x, y-1, rx, ry+1);
	}
	
	FOR(i, 0, k)
	{
		ll x, y;
		cin >> x >> y;
		res = min(res, x*ans.st+y*ans.nd);
		s[x*ans.st+y*ans.nd]++;
	}
	
	cout << res << ' ' << s[res] << '\n';
	
	return 0;
}