func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, m;   string in[1003];   int a[1003][1003];   int X[] = {-1, 0, 0, 1};   int Y[] = {0, -1, 1, 0};   bool d[1003][1003];   int getSm(int i, int j, int u, int v) {     return a[u][v] - a[i - 1][v] - a[u][j - 1] + a[i - 1][j - 1];   }   bool check() {     for (int i = 1; i <= n; i++) {       bool ok = 0;       int cnt = 0;       for (int j = 1; j <= m; j++) {         if (in[i][j] ==  # ) {           if (!ok) {             cnt++;             ok = 1;           }         } else {           ok = 0;         }       }       if (cnt > 1) {         return 0;       }       if (cnt == 0) {         ok = 0;         for (int j = 1; j <= m; j++) {           if (getSm(1, j, n, j) == 0) {             ok = 1;             continue;           }         }         if (!ok) {           return 0;         }       }     }     for (int j = 1; j <= m; j++) {       bool ok = 0;       int cnt = 0;       for (int i = 1; i <= n; i++) {         if (in[i][j] ==  # ) {           if (!ok) {             cnt++;             ok = 1;           }         } else {           ok = 0;         }       }       if (cnt > 1) {         return 0;       }       if (cnt == 0) {         ok = 0;         for (int i = 1; i <= n; i++) {           if (getSm(i, 1, i, m) == 0) {             ok = 1;             continue;           }         }         if (!ok) {           return 0;         }       }     }     return 1;   }   bool canC(int u, int v) {     return (1 <= u) && (u <= n) && (1 <= v) && (v <= m) && (in[u][v] ==  # ) &&            (d[u][v] == 0);   }   void DFS(int u, int v) {     d[u][v] = 1;     for (int k = 0; k < 4; k++) {       int nu = u + X[k];       int nv = v + Y[k];       if (canC(nu, nv)) {         DFS(nu, nv);       }     }   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> in[i];       in[i].insert(0,    );       for (int j = 1; j <= m; j++)         a[i][j] = (in[i][j] ==  # ) + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];     }     if (a[n][m] == 0) {       cout << 0;       return 0;     }     if (n == 1) {       int cnt = 0;       for (int j = 1; j <= m; j++) {         cnt += (in[1][j] ==  # );       }       if ((0 < cnt) && (cnt < m)) {         cout << -1;         return 0;       }     }     if (m == 1) {       int cnt = 0;       for (int i = 1; i <= n; i++) {         cnt += (in[i][1] ==  # );       }       if ((0 < cnt) && (cnt < n)) {         cout << -1;         return 0;       }     }     if (!check()) {       cout << -1;       return 0;     }     memset(d, 0, sizeof d);     int res = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if ((in[i][j] ==  # ) && (d[i][j] == 0)) {           res++;           DFS(i, j);         }       }     cout << res;     return 0;   }   