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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdbool.h>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <inttypes.h>
using namespace std;


bool bPause = !true;

char tab[2048][2048];
int dx, dy;


class Pair
{
public:
	static int64_t cx;
	static int64_t cy;
	int64_t x;
	int64_t y;

	Pair()
	{
		x = y = 0;
	}

	Pair(int64_t px, int64_t py)
	{
		x = px;
		y = py;
	}

	int64_t cost()
	{
		return x * cx + y * cy;
	}

	int64_t cost(int64_t cxl, int64_t cyl)
	{
		return x * cxl + y * cyl;
	}

	Pair right()	{	return Pair{ x+1, y   };	}
	Pair left()		{	return Pair{ x-1, y   };	}
	Pair up()		{	return Pair{ x  , y-1 };	}
	Pair down()  	{	return Pair{ x  , y+1 };  	}

	bool min(Pair b)
	{
		if (x == -1 && y == -1) return true;
		if (x > b.x && y > b.y) return true;
		if (cost(x, y) > cost(b.x, b.y)) return true;
		
		return false;
	}

};

int64_t Pair::cx;
int64_t Pair::cy;

vector <Pair> pos;
vector <Pair> cos_t;

Pair cost[2048][2048];



#define MAX_V 9000000000000000000


inline bool field_ok(Pair p)
{
	return p.x >= 0 && p.y >= 0 && p.x < dx && p.y < dy && tab[p.y][p.x] == '.';
}

Pair solve(int cx, int cy, int x, int y)
{
	pos.clear();
	cos_t.clear();
	pos.push_back(Pair(0, 0));
	cos_t.push_back(Pair(0, 0));
	
	int id = 0;
	while (id < (int)pos.size())
	{
		Pair p = pos[id];
		Pair c = cos_t[id++];

		if (p.x >= 0 && p.y >= 0 && p.x < dx && p.y < dy)
		if (tab[p.y][p.x] == '.')
		{
			if (cost[p.y][p.x].min(c))
			{
				cost[p.y][p.x] = c;
				if (field_ok(p.right())) { pos.push_back(p.right()); cos_t.push_back(Pair{ c.x + cx,c.y }); }
				if (field_ok(p.down()))  { pos.push_back(p.down());  cos_t.push_back(Pair{ c.x + cx,c.y }); }

				if (field_ok(p.left()))  { pos.push_back(p.left());  cos_t.push_back(Pair{ c.x,c.y + cy }); }
				if (field_ok(p.up()))    { pos.push_back(p.up());	 cos_t.push_back(Pair{ c.x,c.y + cy }); }
			}
		}

	}

	return cost[dy - 1][dx - 1];
}


Pair solve(int cx, int cy)
{
	for (int y = 0; y < dy; y++)
	for (int x = 0; x < dx; x++)
	{
		cost[y][x].x = cost[y][x].y = -1;
	}
	Pair::cx = cx;
	Pair::cy = cy;
	return solve(cx, cy, 0, 0);
}

int64_t min_cost(vector <Pair> &sols, int64_t x, int64_t y)
{
	int64_t min_v = MAX_V;
	for (int i = 0; i < (int)sols.size(); i++)
	{
		int64_t c = sols[i].x*x + sols[i].y*y;
		if (c < min_v) min_v = c;
	}
	return min_v;
}

int main()
{
	int n;
	scanf("%d %d %d\n", &dy,&dx,&n);

	for (int y = 0; y < dy; y++)
	{
		scanf("%s\n", &tab[y][0]);
	}

	vector <Pair> sols;

	for (int vx = 1; vx < 3; vx++)
	for (int vy = 1; vy < 3; vy++)
	if (vx!=vy || (vx==1 && vy==1))
	{
		sols.push_back(solve(vx, vy));
	}
	sols.push_back(solve(7, 1));
	sols.push_back(solve(1, 7));
	sols.push_back(solve(70, 1));
	sols.push_back(solve(1, 70));
	sols.push_back(solve(700, 1));
	sols.push_back(solve(1, 700));
	sols.push_back(solve(7000, 1));
	sols.push_back(solve(1, 7000));
	/*
	sols.push_back(solve(70000, 1));
	sols.push_back(solve(1, 70000));
	sols.push_back(solve(700000, 1));
	sols.push_back(solve(1, 700000));
	sols.push_back(solve(7000000, 1));
	sols.push_back(solve(1, 7000000));
	*/

	map<int64_t, int> a_map;
	int64_t min_c = MAX_V;

	for (int i = 0; i < n; i++)
	{
		int xx, yy;
		scanf("%d %d\n", &xx,&yy);
		int64_t c = min_cost(sols, xx, yy);
		a_map[c]++;
		if (c < min_c) min_c= c;
	}

	printf("%" PRId64 "", min_c);
	printf(" %d\n", a_map[min_c]);

#ifdef WIN32
	if (bPause)	while (true);    	
#endif
	return 0;
}