func_code_string: #include <bits/stdc++.h>   using namespace std;   char A[1005][1005];   int P[1000005], MinX[1000005], MinY[1000005], MaxX[1000005], MaxY[1000005],       M[1005][1005];   int xr[] = {-1, 1, 0, 0};   int xc[] = {0, 0, -1, 1};   int n, m;   void dfs(int x, int y, int i) {     if ((x < 0) || (x >= n) || (y < 0) || (y >= m) || (A[x][y] ==  . ) ||         (M[x][y]))       return;     M[x][y] = 1;     MinX[i] = min(MinX[i], x);     MinY[i] = min(MinY[i], y);     MaxX[i] = max(MaxX[i], x);     MaxY[i] = max(MaxY[i], y);     for (int j = 0; j < 4; ++j) {       dfs(x + xr[j], y + xc[j], i);     }   }   bool check_convex_island(int len) {     set<int> X, Y;     for (int i = 0; i < n; ++i) X.insert(i);     for (int i = 0; i < m; ++i) Y.insert(i);     int x, i, j, ans = 1;     for (x = 0; x < len; ++x) {       int minx = MinX[x];       int miny = MinY[x];       int maxx = MaxX[x];       int maxy = MaxY[x];       for (i = minx; i <= maxx; ++i) {         if (!X.count(i)) return false;         X.erase(i);         for (j = miny; A[i][j] ==  . ; ++j)           ;         for (; (j <= maxy) && (A[i][j] ==  # ); ++j)           ;         for (; (j <= maxy) && (A[i][j] ==  . ); ++j)           ;         ans &= (j > maxy);       }       for (j = miny; j <= maxy; ++j) {         if (!Y.count(j)) return false;         Y.erase(j);         for (i = minx; A[i][j] ==  . ; ++i)           ;         for (; (i <= maxx) && (A[i][j] ==  # ); ++i)           ;         for (; (i <= maxx) && (A[i][j] ==  . ); ++i)           ;         ans &= (i > maxx);       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     int i, j, k, x, y, z, a, b;     for (i = 0; i <= 1000000; ++i) MinX[i] = MinY[i] = 1000000007;     cin >> n >> m;     for (i = 0; i < n; ++i) cin >> A[i];     int ans = 1, len = 0;     set<int> X, Y;     for (i = 0; i < n; ++i) X.insert(i);     for (i = 0; i < m; ++i) Y.insert(i);     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         if (A[i][j] ==  . ) continue;         if (X.find(i) != X.end()) X.erase(i);         if (Y.find(j) != Y.end()) Y.erase(j);         if (!M[i][j]) dfs(i, j, len++);       }     }     ans &= ((X.size() == 0 && Y.size() == 0) || (X.size() && Y.size()));     ans &= check_convex_island(len);     if (ans)       cout << len << endl;     else       cout << -1 << endl;     return 0;   }   