#include <bits/stdc++.h>
using namespace std;
long long int dx[4] = {-1, 0, 1, 0};
long long int dy[4] = {0, 1, 0, -1};
bool visited[1005][1005];
long long int n, m;
char a[1005][1005];
void dfs(long long int x, long long int y) {
  visited[x][y] = 1;
  for (long long int i = 0; i < 4; i++) {
    long long int nx = x + dx[i];
    long long int ny = y + dy[i];
    if (1 <= nx <= n && 1 <= ny <= m) {
      if (!visited[nx][ny] && a[nx][ny] == 'B') {
        dfs(nx, ny);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= m; j++) {
      char ch;
      cin >> ch;
      a[i][j] = ((ch == '#') ? 'B' : 'W');
    }
  }
  bool flgr = 0;
  long long int cntwr = 0;
  for (long long int i = 1; i <= n; i++) {
    long long int j = 1;
    while (j <= m && a[i][j] == 'W') {
      j++;
    }
    bool temp = 0;
    while (j <= m && a[i][j] == 'B') {
      j++;
      temp = 1;
    }
    while (j <= m && a[i][j] == 'W') {
      j++;
    }
    if (j <= m && a[i][j] == 'B') {
      flgr = 1;
      break;
    }
    if (temp == 0) {
      cntwr++;
    }
  }
  if (flgr) {
    cout << "-1"
         << "\n";
    return 0;
  }
  bool flgc = 0;
  long long int cntwc = 0;
  for (long long int j = 1; j <= m; j++) {
    long long int i = 1;
    while (i <= n && a[i][j] == 'W') {
      i++;
    }
    bool temp = 0;
    while (i <= n && a[i][j] == 'B') {
      i++;
      temp = 1;
    }
    while (i <= n && a[i][j] == 'W') {
      i++;
    }
    if (i <= n && a[i][j] == 'B') {
      flgc = 1;
      break;
    }
    if (temp == 0) {
      cntwc++;
    }
  }
  if (flgc) {
    cout << "-1"
         << "\n";
    return 0;
  }
  if (cntwr > 0 && cntwc == 0) {
    cout << "-1";
    return 0;
  }
  if (cntwc > 0 && cntwr == 0) {
    cout << "-1";
    return 0;
  }
  long long int res = 0;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= m; j++) {
      if (!visited[i][j] && a[i][j] == 'B') {
        dfs(i, j);
        res++;
      }
    }
  }
  cout << res << "\n";
}
