#include <bits/stdc++.h>
namespace cplib {
using namespace std;
template <typename InputIt,
          typename T = typename iterator_traits<InputIt>::value_type>
void read_n(InputIt it, int n) {
  copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIt,
          typename T = typename iterator_traits<InputIt>::value_type>
void read(InputIt first, InputIt last) {
  read_n(first, distance(first, last));
}
template <typename InputIt,
          typename T = typename iterator_traits<InputIt>::value_type>
void write(InputIt first, InputIt last, const char *delim = "\n") {
  copy(first, last, ostream_iterator<T>(cout, delim));
}
}  // namespace cplib
using namespace std;
using namespace cplib;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int solve(vector<string> grid) {
  int n = grid.size(), m = grid[0].size();
  vector<vector<int>> brow(n), bcol(m);
  vector<vector<bool>> vis(n, vector<bool>(m, false));
  function<int(int, int)> dfs = [&](int r, int c) {
    if (!(0 <= r and r < n and 0 <= c and c < m) or grid[r][c] == '.' or
        vis[r][c])
      return 0;
    vis[r][c] = true;
    return 1 + dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c + 1) + dfs(r, c - 1);
  };
  int ans = 0;
  for (int r = 0; r < n; ++r) {
    for (int c = 0; c < m; ++c) {
      if (grid[r][c] == '.') continue;
      brow[r].push_back(c);
      bcol[c].push_back(r);
      if (!vis[r][c]) ans += (dfs(r, c) > 0);
    }
  }
  int empty_rows = 0, empty_cols = 0;
  for (int r = 0; r < n; ++r) {
    if (brow[r].empty())
      empty_rows++;
    else {
      int prev = brow[r].front() - 1;
      for (auto c : brow[r]) {
        if (c - prev > 1) return -1;
        prev = c;
      }
    }
  }
  for (int c = 0; c < m; ++c) {
    if (bcol[c].empty())
      empty_cols++;
    else {
      int prev = bcol[c].front() - 1;
      for (auto r : bcol[c]) {
        if (r - prev > 1) return -1;
        prev = r;
      }
    }
  }
  if ((empty_rows > 0) != (empty_cols > 0)) return -1;
  return ans;
}
int main(void) {
  ios::sync_with_stdio(false), cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<string> grid(n);
  read(begin(grid), end(grid));
  cout << solve(grid) << endl;
  return 0;
}
