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
#include "bits/stdc++.h"
using namespace std;
template <class A, class B> ostream& operator<<(ostream &os, pair<A, B> p) {
  return os << '{' << p.first << ", " << p.second << '}';
}
template <class T> ostream& operator<<(ostream &os, vector<T> v) {
  os << '{';
  for (T i : v) os << i << ", ";
  return os << '}';
}
#ifdef DEBUG
#define D(x...) x
#else
#define D(x...)
#endif
#define LN(x) D(cerr << #x << ": " << x << ' ')
#define LOG(x) D(cerr << #x << ": " << x << '\n')
#define MES(x) D(cerr << x << ' ')
#define MESL(x) D(cerr << x << '\n')
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
#define EB emplace_back
#define PB pop_back
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)

// Ignacy Boehlke

#define mp make_pair
int main() {
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);

    n += 2; m += 2;
    char in;
    vector<vb> M(n, vb(m)), vis(n, vb(m));
    vvi dis(n, vi(m));
    FOR(y, 1, n - 2) FOR(x, 1, m - 2) scanf(" %c", &in), M[y][x] = (in == '.');
    LOG(M);

    queue<pii> Q;
    Q.emplace(1, 1);
    vis[1][1] = true;
    while (!Q.empty()) {
        auto [y, x] = Q.front();
        LN(y); LOG(x);
        Q.pop();
        for (pii c : {mp(1, 0), mp(-1, 0), mp(0, 1), mp(0, -1)}) {
            if (!vis[y + c.fi][x + c.se] && M[y + c.fi][x + c.se]) {
                vis[y + c.fi][x + c.se] = true;
                dis[y + c.fi][x + c.se] = dis[y][x] + 1;
                Q.emplace(y + c.fi, x + c.se);
            }
        }
    }
    int bdis = dis[n - 2][m - 2], a, b, c = 0;
    LOG(bdis);
    ll x, mn = LONG_LONG_MAX;
    REP(i, k) {
        scanf("%d%d", &a, &b);
        x = ((ll)(bdis - n - m + 6) * (b + a)) / 2 + (ll)(n + m - 6) * a;
        if (x < mn) {
            mn = x;
            c = 1;
        } else if (x == mn) ++c;
    }
    printf("%lld %d\n", mn, c);
}