#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf64 = 1e18;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<vector<int>> used(n, vector<int>(m));
  int res = 0;
  vector<int> row_used(n), col_used(m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (used[i][j] || a[i][j] == '.') continue;
      queue<pair<int, int>> q;
      used[i][j] = 1;
      vector<pair<int, int>> comp = {{i, j}};
      q.push({i, j});
      while (!q.empty()) {
        int vi, vj;
        tie(vi, vj) = q.front();
        q.pop();
        for (int di = -1; di <= 1; di++) {
          for (int dj = -1; dj <= 1; dj++) {
            if (di && dj || !di && !dj) continue;
            int ti = vi + di;
            int tj = vj + dj;
            if (ti < 0 || ti >= n || tj < 0 || tj >= m) continue;
            if (used[ti][tj] || a[ti][tj] == '.') continue;
            used[ti][tj] = 1;
            q.push({ti, tj});
            comp.emplace_back(ti, tj);
          }
        }
      }
      res++;
      unordered_map<int, int> lcol, rcol, lrow, rrow;
      unordered_set<int> cols, rows;
      for (pair<int, int> p : comp) {
        cols.insert(p.second);
        rows.insert(p.first);
        auto it = lcol.find(p.first);
        if (it == lcol.end() || it->second > p.second) lcol[p.first] = p.second;
        it = rcol.find(p.first);
        if (it == rcol.end() || it->second < p.second) rcol[p.first] = p.second;
        it = lrow.find(p.second);
        if (it == lrow.end() || it->second > p.first) lrow[p.second] = p.first;
        it = rrow.find(p.second);
        if (it == rrow.end() || it->second < p.first) rrow[p.second] = p.first;
      }
      int s1 = 0, s2 = 0;
      for (int row : rows) {
        row_used[row]++;
        s1 += rcol[row] - lcol[row] + 1;
      }
      for (int col : cols) {
        col_used[col]++;
        s2 += rrow[col] - lrow[col] + 1;
      }
      if (s1 != s2 || s1 != (int)comp.size()) {
        cout << "-1\n";
        return 0;
      }
    }
  }
  vector<int> ar, cr;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!row_used[i] && !col_used[j]) {
        ar.push_back(i);
        cr.push_back(j);
      }
    }
  }
  for (int i : ar) row_used[i] = 1;
  for (int j : cr) col_used[j] = 1;
  for (int i = 0; i < n; i++) {
    if (row_used[i] != 1) {
      cout << "-1\n";
      return 0;
    }
  }
  for (int j = 0; j < m; j++) {
    if (col_used[j] != 1) {
      cout << "-1\n";
      return 0;
    }
  }
  cout << res << "\n";
  return 0;
}
