func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, m;   const int N = 1005;   char grid[N][N];   int visited[N][N];   bool row[N];   bool col[N];   int drow[] = {0, 0, -1, 1};   int dcol[] = {1, -1, 0, 0};   bool fullrow = false;   bool fullcol = false;   bool inside(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= m); }   bool edge(int x, int y) { return (x == 1 || y == 1 || x == n || y == m); }   void ngang() {     for (int i = 1; i <= n; ++i) {       int cnt = 0;       for (int j = 1; j <= m; ++j) {         if (grid[i][j] ==  # ) {           if (grid[i][j - 1] !=  # ) ++cnt;         }         if (cnt > 1) {           cout << -1;           cerr <<   nngang ;           exit(0);         }       }       if (cnt == 0) {         fullrow = true;       }     }   }   void doc() {     for (int j = 1; j <= m; ++j) {       int cnt = 0;       for (int i = 1; i <= n; ++i) {         if (grid[i][j] ==  # ) {           if (grid[i - 1][j] !=  # ) ++cnt;         }         if (cnt > 1) {           cout << -1;           cerr <<   ndoc ;           exit(0);         }       }       if (cnt == 0) {         fullcol = true;       }     }   }   void input() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> grid[i][j];       }     }   }   void dfs(int i, int j) {     visited[i][j] = true;     for (int d = 0; d <= 3; ++d) {       int x = i + drow[d];       int y = j + dcol[d];       if (!inside(x, y)) continue;       if (grid[x][y] ==  # ) {         if (!visited[x][y]) dfs(x, y);       }     }   }   void solve() {     ngang();     doc();     if (fullrow != fullcol) {       cout << -1;       return;     }     int ans = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (!visited[i][j] && grid[i][j] ==  # ) {           dfs(i, j);           ++ans;         }       }     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     input();     solve();     return 0;   }   