#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 2e18;
const int fx[] = {1, 0, -1, 0};
const int fy[] = {0, 1, 0, -1};
const int N = 1007;
string s[N];
bool vis[N][N];
int row, col;
bool check() {
  int br = 0, bc = 0;
  for (int i = 0; i < row; ++i) {
    int cnt = 0;
    for (char c : s[i]) cnt += (c == '.');
    if (cnt == col) ++br;
  }
  for (int j = 0; j < col; ++j) {
    int cnt = 0;
    for (int i = 0; i < row; ++i) cnt += (s[i][j] == '.');
    if (cnt == row) ++bc;
  }
  if ((br and !bc) or (bc and !br)) return false;
  for (int i = 0; i < row; ++i) {
    vector<bool> flag(col, false);
    for (int j = 0; j < col; ++j) {
      flag[j] = (s[i][j] == '#');
      if (j) flag[j] = (flag[j] or flag[j - 1]);
    }
    for (int j = col - 1, black = 0; j > 0; --j) {
      if (s[i][j] == '#')
        ++black;
      else if (j < col - 1 and black and flag[j - 1])
        return false;
    }
  }
  for (int j = 0; j < col; ++j) {
    vector<bool> flag(row, false);
    for (int i = 0; i < row; ++i) {
      flag[i] = (s[i][j] == '#');
      if (i) flag[i] = (flag[i] or flag[i - 1]);
    }
    for (int i = row - 1, black = 0; i > 0; --i) {
      if (s[i][j] == '#')
        ++black;
      else if (i < row - 1 and black and flag[i - 1])
        return false;
    }
  }
  return true;
}
void dfs(int x, int y) {
  vis[x][y] = true;
  for (int i = 0; i < 4; ++i) {
    int tx = x + fx[i], ty = y + fy[i];
    if (tx >= 0 and tx < row and ty >= 0 and ty < col and !vis[tx][ty] and
        s[tx][ty] == s[x][y]) {
      dfs(tx, ty);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ;
  cin >> row >> col;
  for (int i = 0; i < row; ++i) cin >> s[i];
  if (!check()) {
    cout << "-1\n";
    return 0;
  }
  memset(vis, false, sizeof vis);
  int res = 0;
  for (int i = 0; i < row; ++i) {
    for (int j = 0; j < col; ++j) {
      if (s[i][j] == '#' and !vis[i][j]) {
        dfs(i, j);
        ++res;
      }
    }
  }
  cout << res << "\n";
  return 0;
}
