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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define FOR(x, b, e) for(long x = b; x <= (e); x++)
#define FORD(x, b, e) for(long x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
#define LL long long


using namespace std;

class Coord
{
public:
    long x,y;
    //Coord(long p_x, long p_y) {x=p_x; y=p_y;};
};

long n,m,k;

bool g[2020][2020];
long dist[2020][2020];
Coord coords1[5000000];
Coord coords2[5000000];

long findWay()
{
    FOR(y,1,n)FOR(x,1,m) dist[x][y]=-1;
    long ile2=0;
    long ile1=1; coords1[1].x=1; coords1[1].y=1;
    dist[1][1]=0;
    long x0,y0;
    long warstwa=0;
    while (ile1!=0)
    {
        // w coords1 mam cala ostatnio dodana warstwe
        warstwa++; ile2=0;
        FOR(i,1,ile1)
        {
            x0=coords1[i].x; y0=coords1[i].y;
            if ((y0!=1)&&(g[x0][y0-1])&&(dist[x0][y0-1]==-1)) { ile2++; coords2[ile2].x=x0; coords2[ile2].y=y0-1; dist[x0][y0-1]=warstwa; };
            if ((y0!=n)&&(g[x0][y0+1])&&(dist[x0][y0+1]==-1)) { ile2++; coords2[ile2].x=x0; coords2[ile2].y=y0+1; dist[x0][y0+1]=warstwa; };
            if ((x0!=1)&&(g[x0-1][y0])&&(dist[x0-1][y0]==-1)) { ile2++; coords2[ile2].x=x0-1; coords2[ile2].y=y0; dist[x0-1][y0]=warstwa; };
            if ((x0!=m)&&(g[x0+1][y0])&&(dist[x0+1][y0]==-1)) { ile2++; coords2[ile2].x=x0+1; coords2[ile2].y=y0; dist[x0+1][y0]=warstwa; };
        }
        ile1=ile2;
        FOR(i,1,ile2) coords1[i]=coords2[i];
    }
    return dist[m][n];
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	cin >>n; cin>>m; cin>>k;
	string s;
	FOR(y,1,n) {
	    cin >> s;
	    FOR(x,1,m) g[x][y]=(s[x-1]=='.');
	}
	long d = findWay();
	LL w_dol=(d-(n+m-2))/2;
	LL w_gore=n+m-2+w_dol;
	//cout << "W gore: " << w_gore << "  W dol: " << w_dol << "\n";
	LL a,b;
	LL best=-1; long ileBest=0;
	LL time;
	FOR(i,1,k)
	{
	    cin >> a; cin >> b;
        time=a*w_gore+b*w_dol;
        if ((best==-1)||(time<best)) { best=time; ileBest=1; }
        else if (time==best) ileBest++;
	}

	cout << best << " " << ileBest << "\n";
    return 0;
};