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
#include <iostream>

//#include <chrono> 
//using namespace std::chrono; 

using namespace std;

int mapa[2000][2000];
int n, m;
//long powroty_max;

long szukaj(int x, int y, long powroty_max) {
    long ret, i;

//    cout << "szukaj x=" << x << " y=" << y << " powroty=" << powroty_max << endl;
//cerr << endl;
    //if (mapa[x][y] == -2 || mapa[x][y] == -3) return -2;
    //if (mapa[x][y] >= 0) return mapa[x][y];
    if (powroty_max < 0) return -2;
    if (x == n - 1 && y == m - 1) {
        //if (powroty_max > powroty) powroty_max = powroty;
        mapa[x][y] = 0;
/*        cout << "bingo" << endl;
        for (i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << mapa[i][j] << " ";
                if (mapa[i][j] >= 0) cout << " ";
            }
            cout << endl;
        }
*/        return 0;
    }
    mapa[x][y] = -3;
    ret = -2;
    if (y < m - 1) {
        switch (mapa[x][y + 1]) {
            case -3: break;
            case -2: break;
            case -1: ret = szukaj(x, y + 1, powroty_max); break;
            case 0: mapa[x][y] = 0; return 0;
            default: ret = mapa[x][y + 1];
        }
        if (ret >= 0) powroty_max = ret - 1;
        //if (powroty_max <= powroty) { mapa[x][y] = ret; return ret; }
    }
    if (x < n - 1) {
        switch (mapa[x + 1][y]) {
            case -3: i = -2; break;
            case -2: i = -2; break;
            case -1: i = szukaj(x + 1, y, powroty_max); break;
            case 0: mapa[x][y] = 0; return 0;
            default: i = mapa[x + 1][y];
        }
        if (i != -2 && (ret == -2 || i < ret)) ret = i;
        if (ret >= 0) powroty_max = ret - 1;
    }
    //if (powroty_max <= powroty + 1) { mapa[x][y] = ret; return ret; }
    if (x > 0) {
        switch (mapa[x - 1][y]) {
            case -3: i = -2; break;
            case -2: i = -2; break;
            case -1: i = szukaj(x - 1, y, powroty_max - 1); break;
            default: i = mapa[x - 1][y];
        }
        if (i != -2 && (ret == -2 || i + 1 < ret)) ret = i + 1;
        if (ret >= 0) powroty_max = ret - 1;
        //if (powroty_max <= powroty + 1) { mapa[x][y] = ret; return ret; }
    }
    if (y > 0) {
        switch (mapa[x][y - 1]) {
            case -3: i = -2; break;
            case -2: i = -2; break;
            case -1: i = szukaj(x, y - 1, powroty_max - 1); break;
            default: i = mapa[x][y - 1];
        }
        if (i != -2 && (ret == -2 || i + 1 < ret)) ret = i + 1;
    }
    mapa[x][y] = ret;
    return ret;
}

int main()
{
    long k, gora, dol;
    long i, j, nr;
    unsigned long long t, ret, v1, v2, v1_min, v2_min;
    string s;

//    auto start = high_resolution_clock::now();

//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);

    cin >> n >> m >> k;

    for (i = 0; i < n; i++) {
        cin >> s;
        for (j = 0; j < m; j++)
            mapa[i][j] = (s[j] == '.' ? -1 : -2);
    }
    
//    powroty_max = 10000000;
    dol = szukaj(0, 0, 10000000);
    gora = n - 1 + m - 1 + dol;

/*    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            cout << mapa[i][j] << " ";
            if (mapa[i][j] >= 0) cout << " ";
        }
        cout << endl;
    }
*///    cout << gora << " - " << dol << endl;

    cin >> v1 >> v2;
    ret = v1 * gora + v2 * dol;
    nr = 1;
    v1_min = v1; v2_min = v2;
//    n = 1; //////////////////////////////
    if (dol > 0) {
        for (i = 1; i < k; i++) {
            cin >> v1 >> v2;
            if (v1 == v1_min && v2 == v2_min) nr++;
            else if (v1 < v1_min || v2 < v2_min) {
                t = v1 * gora + v2 * dol;
    //            n++;
                if (t < ret) {
                    ret = t; nr = 1; v1_min = v1; v2_min = v2;
                } else if (ret == t) nr++;
            }
        }
    } else {
        for (i = 1; i < k; i++) {
            cin >> v1 >> v2;
            if (v1 == v1_min) nr++;
            else if (v1 < v1_min) {
                t = v1 * gora;
    //            n++;
                if (t < ret) {
                    ret = t; nr = 1; v1_min = v1;
                } else if (ret == t) nr++;
            }
        }
    }


    cout << ret << " " << nr;

//    auto stop = high_resolution_clock::now(); 
//    auto duration = duration_cast<microseconds>(stop - start);
//    cout << endl << "Timer=" << duration.count() << endl;
//    cout << "liczba mnożeń=" << n << endl;

    return 0;
}