from collections import deque
import sys 
input = sys.stdin.readline

def compress(string):
    string.append("$")
    n = len(string)
    begin, end, cnt = 0, 1, 1
    ans = []
    while end < n:
        if string[begin] == string[end]:
            end, cnt = end + 1, cnt + 1
        else:
            ans.append((string[begin], cnt))
            begin, end, cnt = end, end + 1, 1
    return ans


def dfs(i, j):
    q = deque([(i, j)])
    visited[i][j] = True
    n_b[i] = True
    m_b[j] = True
    while q:
        i, j = q.pop()
        for nxt_i, nxt_j in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
            if not(0 <= nxt_i < n and 0 <= nxt_j < m):
                continue
            if visited[nxt_i][nxt_j] or s[nxt_i][nxt_j] == ".":
                continue
            visited[nxt_i][nxt_j] = True
            n_b[nxt_i] = True
            m_b[nxt_j] = True
            q.append((nxt_i, nxt_j))
    
n, m = map(int, input().split())
s = [input() for i in range(n)]

cnt = 0
visited = [[False] * m for i in range(n)]
n_b = [False] * n
m_b = [False] * m
for i in range(n):
    for j in range(m):
        if s[i][j] == "#" and not visited[i][j]:
            dfs(i, j)
            cnt += 1

n_flag = all(n_b)
m_flag = all(m_b)
if n_flag != m_flag:
    print(-1)
    exit()

# not convex
flag = True
for i in range(n):
    comp = compress(list(s[i]))
    if comp[0][0] == "." and len(comp) >= 4 + 1:
        flag = False
    if comp[0][0] == "#" and len(comp) >= 3 + 1:
        flag = False
ts = list(zip(*s))
for i in range(m):
    comp = compress(list(ts[i]))
    if comp[0][0] == "." and len(comp) >= 4:
        flag = False
    if comp[0][0] == "#" and len(comp) >= 3:
        flag = False
if not flag:
    print(-1)
    exit()
print(cnt)