#include <bits/stdc++.h>
using namespace std;
function<void(void)> ____ = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
};
const int MAXN = 1111;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
char s[MAXN][MAXN];
bool ox[MAXN], oy[MAXN], vis[MAXN][MAXN];
int cntx[MAXN], cnty[MAXN], lmax[MAXN], rmax[MAXN], umax[MAXN], dmax[MAXN];
int n, m, ckx[MAXN], cky[MAXN];
bool checkok() {
  for (int i = 1; i <= n; i++)
    if (!ckx[i]) return false;
  for (int i = 1; i <= m; i++)
    if (!cky[i]) return false;
  return true;
}
void bfs(int x, int y) {
  queue<pair<int, int> > que;
  que.push(make_pair(x, y));
  vis[x][y] = true;
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int cx = p.first, cy = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = cx + dir[i][0];
      int ny = cy + dir[i][1];
      if (nx < 1 or ny < 1 or nx > n or ny > m or vis[nx][ny] or
          s[nx][ny] == '.')
        continue;
      vis[nx][ny] = true;
      que.push(make_pair(nx, ny));
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  vector<pair<int, int> > vec;
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '.') continue;
      ox[i] = oy[j] = true;
      ckx[i] = cky[j] = true;
      vec.emplace_back(make_pair(i, j));
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (!ox[i] and !oy[j]) ckx[i] = cky[j] = true;
    }
  if (!checkok()) {
    cout << -1 << endl;
    return 0;
  }
  for (auto p : vec) {
    cntx[p.first] = cnty[p.second] = 0;
    lmax[p.first] = umax[p.second] = MAXN;
    rmax[p.first] = dmax[p.second] = 0;
  }
  for (auto p : vec) {
    cntx[p.first]++;
    cnty[p.second]++;
    lmax[p.first] = min(lmax[p.first], p.second);
    rmax[p.first] = max(rmax[p.first], p.second);
    umax[p.second] = min(umax[p.second], p.first);
    dmax[p.second] = max(dmax[p.second], p.first);
  }
  for (auto p : vec) {
    int x = p.first, y = p.second;
    if (cntx[x] != rmax[x] - lmax[x] + 1) {
      cout << -1 << endl;
      return 0;
    }
    if (cnty[y] != dmax[y] - umax[y] + 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  int tot = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '.' or vis[i][j]) continue;
      tot++;
      bfs(i, j);
    }
  cout << tot << endl;
  return 0;
}
