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
#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

inline int id(int x, int y, int m) {
    return x + y * m;
}

inline int left(int i, int n, int m) {
    int x = i % m;
    int y = i / m;
    if (x == 0) return -1;

    return id(x - 1, y, m);
}

inline int right(int i, int n, int m) {
    int x = i % m;
    int y = i / m;
    if (x == m - 1) return -1;

    return id(x + 1, y, m);
}

inline int top(int i, int n, int m) {
    int x = i % m;
    int y = i / m;
    if (y == 0) return -1;

    return id(x, y - 1, m);
}

inline int bottom(int i, int n, int m) {
    int x = i % m;
    int y = i / m;
    if (y == n - 1) return -1;

    return id(x, y + 1, m);
}

// m - szerokość
// n - wysokość

pair<int, int> findPath(vector<char> &map, int n, int m) {
    queue<pair<int, pair<int, int>>> q;
    vector<bool> visited(n * m, false);

    q.emplace(0, make_pair(0, 0));
    visited[0] = true;

    while (!q.empty()) {
        int index = q.front().first;
        auto path = q.front().second;

        if (index == id(m - 1, n - 1, m)) {
            return path;
        }

        int neighbors[4] = {right(index, n, m), bottom(index, n, m), left(index, n, m), top(index, n, m)};

        for (int ni = 0; ni < 4; ni++) {
            int neighbor = neighbors[ni];
            if (neighbor != -1 && !visited[neighbor] && map[neighbor] == '.') {
                visited[neighbor] = true;
                auto newPath = ni < 2 ?
                               make_pair(path.first + 1, path.second) :
                               make_pair(path.first, path.second + 1);

                q.emplace(neighbor, newPath);
            }
        }

        q.pop();
    }
    return make_pair(-1, -1);
}

int main() {
    int n, m, k;
    cin >> n >> m >> k;

    vector<char> map(n * m);

    char c;
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < m; x++) {
            cin >> c;
            map[id(x, y, m)] = c;
        }
    }

    auto path = findPath(map, n, m);

    long long record = -1;
    int count = 0;

    long long a, b;
    for (int i = 0; i < k; i++) {
        cin >> a >> b;
        long long value = a * path.first + b * path.second;
        if (record == -1 || value < record) {
            count = 1;
            record = value;
        } else if (value == record) {
            count++;
        }
    }


    cout << record << " " << count << endl;

    return 0;
}