#include <bits/stdc++.h>   inline long long rd() {     long long _x = 0;     int _ch = getchar(), _f = 1;     for (; !isdigit(_ch) && (_ch !=  - ) && (_ch != EOF); _ch = getchar())       ;     if (_ch ==  - ) {       _f = 0;       _ch = getchar();     }     for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch -  0 ;     return _f ? _x : -_x;   }   void write(long long _x) {     if (_x >= 10)       write(_x / 10), putchar(_x % 10 +  0 );     else       putchar(_x +  0 );   }   inline void wrt(long long _x, char _p) {     if (_x < 0) putchar( - ), _x = -_x;     write(_x);     if (_p) putchar(_p);   }   int n, m;   char s[1005][1005];   bool flag1, flag2;   bool vis[1005][1005];   void dfs(int x, int y) {     if (vis[x][y] || s[x][y] !=  #  || x == 0 || y == 0 || x == n + 1 ||         y == m + 1)       return;     vis[x][y] = 1;     dfs(x + 1, y), dfs(x, y + 1);     dfs(x - 1, y), dfs(x, y - 1);   }   int main() {     n = rd(), m = rd();     for (int i = 1; i <= n; i++) {       scanf( %s , s[i] + 1);     }     for (int i = 1; i <= n; i++) {       bool tag = 0;       for (int j = 1; j <= m; j++) {         if (s[i][j] ==  # ) tag = 1;       }       flag1 |= !tag;     }     for (int i = 1; i <= m; i++) {       bool tag = 0;       for (int j = 1; j <= n; j++) {         if (s[j][i] ==  # ) tag = 1;       }       flag2 |= !tag;     }     if (((!flag1) && (flag2)) || ((flag1) && (!flag2))) {       printf( -1 n );       return 0;     }     bool flag = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (s[i][j] ==  # ) {           int t = j;           while (s[i][t] ==  # ) t++;           for (int k = t; k <= m; k++)             if (s[i][k] ==  # ) flag = 1;         }       }     }     for (int i = 1; i <= m; i++) {       for (int j = 1; j <= n; j++) {         if (s[j][i] ==  # ) {           int t = j;           while (s[t][i] ==  # ) t++;           for (int k = t; k <= n; k++)             if (s[k][i] ==  # ) flag = 1;         }       }     }     if (flag) {       printf( -1 n );       return 0;     }     int cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++)         if (s[i][j] ==  #  && !vis[i][j]) {           dfs(i, j);           cnt++;         }     }     wrt(cnt,   n );   }   