#include <bits/stdc++.h>   using namespace std;   int a[1005][1005], used[1005][1005];   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   void DFS(int i, int j, int N, int M) {     if (i == 0 || i == N + 1 || j == 0 || j == M + 1 || used[i][j]) return;     used[i][j] = 1;     for (int d = 0; d < 4; ++d)       if (a[i + dx[d]][j + dy[d]] == 1) DFS(i + dx[d], j + dy[d], N, M);   }   int main() {     int N, M;     scanf( %d%d , &N, &M);     int numberBlacks = 0;     for (int i = 1; i <= N; ++i) {       char s[1005];       scanf( %s , s + 1);       for (int j = 1; j <= M; ++j)         if (s[j] ==  # ) {           a[i][j] = 1;           ++numberBlacks;         }     }     if (numberBlacks == 0) {       printf( 0 n );       return 0;     }     int emptyLines = 0;     for (int i = 1; i <= N; ++i) {       int cnt = 0, numberLineSegments = 0;       for (int j = 1; j <= M; ++j) {         cnt += a[i][j];         if (a[i][j - 1] == 0 && a[i][j] == 1) ++numberLineSegments;       }       if (numberLineSegments > 1) {         printf( -1 n );         return 0;       }       if (cnt == 0) ++emptyLines;     }     int emptyColumns = 0;     for (int j = 1; j <= M; ++j) {       int cnt = 0, numberColumnSegments = 0;       for (int i = 1; i <= N; ++i) {         cnt += a[i][j];         if (a[i - 1][j] == 0 && a[i][j] == 1) ++numberColumnSegments;       }       if (numberColumnSegments > 1) {         printf( -1 n );         return 0;       }       if (cnt == 0) ++emptyColumns;     }     if (emptyLines == 0 && emptyColumns > 0 ||         emptyLines > 0 && emptyColumns == 0) {       printf( -1 n );       return 0;     }     int connectedComponents = 0;     for (int i = 1; i <= N; ++i)       for (int j = 1; j <= M; ++j)         if (used[i][j] == 0 && a[i][j] == 1) {           ++connectedComponents;           DFS(i, j, N, M);         }     printf( %d n , connectedComponents);     return 0;   }   