#include using namespace std; vector>> squares; bool rec(int x, int y, set> ®ion) { auto &[c, g] = squares[x][y]; if (g) { return false; } g = true; region.insert({x, y}); vector> checks = { {x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}}; for (auto [xx, yy] : checks) { if (xx < 0 || yy < 0 || xx >= squares.size() || yy >= squares[0].size()) { continue; } if (get<0>(squares[xx][yy]) == c) { rec(xx, yy, region); } } return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); string line; while (getline(cin, line) && !line.empty()) { vector> l; for (char c : line) { l.push_back({c, false}); } squares.push_back(l); } vector>> regions; for (int x = 0; x < squares.size(); x++) { for (int y = 0; y < squares[0].size(); y++) { set> region; if (rec(x, y, region)) { regions.push_back(region); } } } int total = 0; for (auto ®ion : regions) { int area = region.size(); int perim = 0; for (auto [x, y] : region) { for (auto &d : vector>( {{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}})) { if (!region.contains(d)) { perim++; } } } total += area * perim; } cout << total << '\n'; }