#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char ch;
      cin >> ch;
      if (ch == '.') g[i][j] = 1;
    }
  }
  vector<int> hr(n, 0), hc(m, 0);
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    int f = -1;
    int l = -2;
    for (int j = 0; j < m; j++) {
      if (g[i][j] == 0) {
        cnt++;
        if (f == -1) f = j;
        l = j;
      }
    }
    if (cnt != (l - f + 1)) {
      cout << "-1" << endl;
      return 0;
    }
    if (cnt) hr[i] = 1;
  }
  for (int j = 0; j < m; j++) {
    int cnt = 0;
    int f = -1;
    int l = -2;
    for (int i = 0; i < n; i++) {
      if (g[i][j] == 0) {
        cnt++;
        if (f == -1) f = i;
        l = i;
      }
    }
    if (cnt != (l - f + 1)) {
      cout << "-1" << endl;
      return 0;
    }
    if (cnt) hc[j] = 1;
  }
  for (int i = 0; i < n; i++) {
    if (hr[i]) continue;
    int flag = 0;
    for (int j = 0; j < m; j++) {
      if (hc[j] == 0) flag = 1;
    }
    if (flag == 0) {
      cout << "-1" << endl;
      return 0;
    }
  }
  for (int j = 0; j < m; j++) {
    if (hc[j]) continue;
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (hr[i] == 0) flag = 1;
    }
    if (flag == 0) {
      cout << "-1" << endl;
      return 0;
    }
  }
  int ver[] = {0, 1, 0, -1};
  int hor[] = {1, 0, -1, 0};
  int it = 0;
  vector<vector<int>> vis(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (g[i][j]) continue;
      if (!vis[i][j]) {
        queue<pair<int, int>> q;
        vis[i][j] = 1;
        it++;
        q.push(pair<int, int>(i, j));
        while (!q.empty()) {
          pair<int, int> u = q.front();
          q.pop();
          int ur = u.first;
          int uc = u.second;
          for (int k = 0; k < 4; k++) {
            int vr = ur + ver[k];
            int vc = uc + hor[k];
            if (vr >= 0 && vr < n && vc >= 0 && vc < m) {
              if (g[vr][vc] == 0 && !vis[vr][vc]) {
                vis[vr][vc] = true;
                q.push(pair<int, int>(vr, vc));
              }
            }
          }
        }
      }
    }
  }
  cout << it << endl;
  return 0;
}
