#include using namespace std; #ifdef DEBUG int D_RECUR_DEPTH = 0; #define deb(x) \ { \ ++D_RECUR_DEPTH; \ auto x2 = x; \ --D_RECUR_DEPTH; \ cerr << string(D_RECUR_DEPTH, '\t') << "\e[91m" << __func__ << ":" << __LINE__ << "\t" << #x << " = " << x2 << "\e[39m" << endl; \ } template typename enable_if::value, O &>::type operator<<(O &ost, const C &v) { if (&ost == &cout) { cerr << "Warning, printing debugs on cout!" << endl; } ost << "["; bool firstIter = true; for (auto &x : v) { if (firstIter) firstIter = false; else ost << ", "; ost << x; } return ost << "]"; } template Ostream &operator<<(Ostream &ost, const pair &p) { if (&ost == &cout) { cerr << "Warning, printing debugs [pair] on cout!" << endl; } return ost << "{" << p.first << ", " << p.second << "}"; } #else #define deb(x) #endif template C reversed(C c) { reverse(c.begin(), c.end()); return c; } #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair pii; const vector D = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; const vector SD = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; const vector KN = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}}; int n; vector tab; bool inn(int i, int j) { return i >= 0 && j >= 0 && i < n && j < n; } bool isAnimal(int i, int j) { assert(inn(i, j)); return tab[i][j] == 'D' || tab[i][j] == 'v' || tab[i][j] == '!'; } ll sunsFrom(int i, int j) { if (tab[i][j] == '*' || tab[i][j] == ' ') return 0; for (auto a : SD) { for (int s = 1; s <= n; s++) { int i2 = i + s * a.st, j2 = j + s * a.nd; if (inn(i2, j2) == false || (tab[i2][j2] != ' ' && tab[i2][j2] != '*')) break; if (tab[i2][j2] == '*') return 100; } } return 0; } ll flock() { vector> flock(n, vector(n)); function dfs = [&](int i, int j, int k) { flock[i][j] = k; for (auto a : D) { int i2 = i + a.st, j2 = j + a.nd; if (inn(i2, j2) && flock[i2][j2] == 0 && tab[i2][j2] != ' ') dfs(i2, j2, k); } }; int k = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (flock[i][j] == 0 && (tab[i][j] == 'v' || tab[i][j] == 'D')) dfs(i, j, ++k); } } k++; vector wid(k), per(k); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int len = 0; for (; j + len < n && flock[i][j + len] == flock[i][j]; len++) wid[flock[i][j]] = max(wid[flock[i][j]], len + 1); } } // per for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (flock[i][j] == 0) continue; for (auto d : D) { int i2 = i + d.st, j2 = j + d.nd; if (!inn(i2, j2) || tab[i2][j2] == ' ') per[flock[i][j]]++; } } } wid[0] = per[0] = 0; return accumulate(wid.begin(), wid.end(), 0LL) * 500LL + accumulate(per.begin(), per.end(), 0LL) * 60LL; } ll houseUD(int i, int j) { if (tab[i][j] != '^') return 0; int iU = i, cU = 0; int res = 0; for (int s = 1; s <= n; s++) { if (iU < 0 || tab[iU][j] != ' ') break; cU++; iU--; } res += cU * 15; /*iU = i, cU = 0; for(int s=1;s<=n;s++) { if(!inn(iU,j) || tab[iU][j] != ' ') break; cU++; iU++; }*/ return res; // return res + cU*5; } ll b33() { return max(0, (n - 2)) * max(0, (n - 2)); } ll animalsI(int i, int j) { int res = 0; if (isAnimal(i, j)) { for (auto a : D) { int i2 = i + a.st, j2 = j + a.nd; if (inn(i2, j2) && tab[i2][j2] == ' ') res += 15; } } return res; } ll freedom() { vector> visited(n, vector(n)); function flood = [&](int i, int j) { if (visited[i][j] || tab[i][j] != ' ') return; visited[i][j] = 1; for (auto a : D) { int i2 = i + a.st, j2 = j + a.nd; if (inn(i2, j2) && visited[i2][j2] == 0 && tab[i2][j2] == ' ') flood(i2, j2); } }; for (int x = 0; x < n; x++) { flood(x, 0); flood(0, x); flood(x, n - 1); flood(n - 1, x); } int res = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (auto a : D) { int i2 = i + a.st, j2 = j + a.nd; if (tab[i][j] != ' ' && (!inn(i2, j2) || visited[i2][j2])) { res += 7; break; } } } } return res; } ll chup(int i, int j) { if (tab[i][j] != 'D' && tab[i][j] != 'v') return 0; for (auto a : KN) { int i2 = i + a.st, j2 = j + a.nd; if (inn(i2, j2) && tab[i2][j2] == '!') return 200; } return 0; } ll drakegrill(int i, int j) { int res = 0; if (tab[i][j] == 'D') { for (auto a : D) { int i2 = i + a.st, j2 = j + a.nd; if (inn(i2, j2) && tab[i2][j2] == 'G') res += 550; } } return res; } ll minFreqEmptiesAnimals2() { map cnt; ll res = 0; for (auto a : tab) for (auto b : a) { if (b != ' ') cnt[b]++; else res++; } ll mini = 1e18; for (auto a : cnt) mini = min(mini, a.nd); for (auto a : tab) for (auto b : a) if (cnt.count(b) && cnt[b] == mini) { res += 10; } res += cnt['!'] * (ll)cnt['D'] * cnt['v']; res += 3LL * min(cnt['^'], cnt['G']); return res; } ll peaks() { vector> pos; for (int i = 0; i < n; i++) { for (int j = 0; j + 1 < n; j++) { if (tab[i][j] == '/' && tab[i][j + 1] == '\\') { pos.push_back({i, j}); } } } ll res = 0; for (auto a : pos) { ll ress = 0; for (auto b : pos) { ress = max(ress, abs(a.st - b.st) + abs(a.nd - b.nd)); } cout << a.st << " " << a.nd << " " << ress << endl; res += ress; } return 50LL * res; } int32_t main() { ios::sync_with_stdio(false); cin >> n; tab.resize(n); string lin; getline(cin, lin); for (int i = 0; i < n; i++) { getline(cin, lin); tab[i] = lin; } ll res = 0; res += flock() + b33() + freedom() + minFreqEmptiesAnimals2() + peaks(); cout << "P = " << minFreqEmptiesAnimals2() << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << drakegrill(i, j) << " "; cout << endl; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) res += sunsFrom(i, j) + houseUD(i, j) + animalsI(i, j) + chup(i, j) + drakegrill(i, j); cout << res; }