#include #include #include using namespace std; void add_sibling(int row, int col, int dx, int dy, vector& table, vector>& graph) { int s_row = row + dy; int s_col = col + dx; int n = table.size(); int m = table[row].size(); if (0 <= s_row && s_row < n && 0 <= s_col && s_col < m && table[s_row][s_col] == '#') { graph[row * m + col].push_back(s_row * m + s_col); } } void dfs(int v, vector& colors, vector>& graph) { colors[v] = 1; for (int u : graph[v]) { if (colors[u] == 0) { dfs(u, colors, graph); } } colors[v] = 2; } int main() { int n, m; cin >> n >> m; vector table(n); for (string& row : table) { cin >> row; } vector> graph(n*m); for (int i = 0; i < table.size(); i++) { string& row = table[i]; for (int j = 0; j < row.size(); j++) { char elem = row[j]; if (elem == '#') { add_sibling(i, j, -1, 0, table, graph); add_sibling(i, j, 1, 0, table, graph); add_sibling(i, j, 0, -1, table, graph); add_sibling(i, j, 0, 1, table, graph); } } } vector colors(n * m); int ans = 0; for (int index = 0; index < n * m; index++) { int row = index / m; int col = index % m; if (table[row][col] == '#' && colors[index] == 0) { dfs(index, colors, graph); ans++; } } cout << ans; return 0; }