#include <bits/stdc++.h>
using namespace std;
string repeat(string second, int n) {
  string s1 = second;
  for (int i = 1; i < n; i++) second += s1;
  return second;
}
string getString(char x) {
  string second(1, x);
  return second;
}
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool solve(long long x) {
  if (x & (x - 1)) return false;
  return true;
}
vector<vector<int>> vis(1001, vector<int>(1001, false));
vector<vector<int>> adj(1001, vector<int>(1001));
vector<vector<pair<int, int>>> strng;
vector<pair<int, int>> x1;
int n, m;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool check(int x, int y) {
  if (x >= 0 && x < m && y >= 0 && y < n) return true;
  return false;
}
void dfs(int y, int x) {
  vis[y][x] = true;
  if (adj[y][x] == 0) return;
  x1.push_back({y, x});
  for (int i = 0; i < 4; i++) {
    if (check(x + dx[i], y + dy[i]) && !vis[y + dy[i]][x + dx[i]])
      dfs(y + dy[i], x + dx[i]);
  }
}
int main() {
  optimizeIO();
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string second;
    cin >> second;
    for (int j = 0; j < second.length(); j++) {
      if (second[j] == '.')
        adj[i][j] = 0;
      else
        adj[i][j] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!vis[i][j]) {
        x1.clear();
        dfs(i, j);
        if (x1.size() > 0) strng.push_back(x1);
      }
    }
  }
  int ans = strng.size();
  int flag = 0;
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }
  vector<bool> s2(n, 0), s3(m, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (adj[i][j]) {
        s3[j] = 1;
        s2[i] = 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    set<int> second;
    for (int j = 0; j < m; j++) {
      if (adj[i][j]) second.insert(j);
    }
    if (second.size() > 0) {
      int y = *second.begin() + 1;
      for (auto j : second) {
        if (j == *second.begin())
          continue;
        else if (j != y) {
          flag = 1;
          break;
        } else
          y++;
      }
    } else {
      int flag2 = 1;
      for (int j = 0; j < m; j++) {
        if (s3[j] == 0) flag2 = 0;
      }
      if (flag2) {
        flag = 1;
      }
    }
  }
  for (int j = 0; j < m; j++) {
    set<int> second;
    for (int i = 0; i < n; i++) {
      if (adj[i][j] == 1) {
        second.insert(i);
      }
    }
    int y = *second.begin() + 1;
    for (auto i : second) {
      if (i == *second.begin())
        continue;
      else if (i != y) {
        flag = 1;
        break;
      } else
        y++;
    }
    if (second.size() == 0) {
      int flag2 = 1;
      for (int i = 0; i < n; i++) {
        if (s2[i] == 0) flag2 = 0;
      }
      if (flag2) {
        flag = 1;
      }
    }
  }
  if (flag)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
