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

n, m = [int(_) for _ in input().split()]

M = []
has_b = False
for i in range(n):
    row = input()[:-1]
    M.append(row)
    if '#' in row:
        has_b = True
if not has_b:
    print(0)
    sys.exit(0)

def check(line):
    if '#' not in line:
        return True
    i = 0
    while i < len(line) and line[i] != '#':
        i += 1
    i += 1
    while i < len(line) and line[i] != '.':
        i += 1
    i += 1
    while i < len(line):
        if line[i] == '#':
            print(-1)
            sys.exit(0)
        i += 1
    return False

dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]

def bfs(M, i, j, visited):
    Q = deque()
    Q.append((i, j))
    while Q:
        el = Q.pop()
        visited[el[0]][el[1]] = True
        for d in dirs:
            ni = el[0] + d[0]
            nj = el[1] + d[1]
            if ni < 0 or ni >= len(M) or nj < 0 or nj >= len(M[0]) or visited[ni][nj]:
                continue
            if M[ni][nj] == '#':
                Q.append((ni, nj))

has_empty_row = False
for row in range(len(M)):
    has_empty_row |= check(M[row])

has_empty_col = False
for col in range(len(M[0])):
    has_empty_col |= check([M[i][col] for i in range(len(M))])

if has_empty_row != has_empty_col:
    print(-1)
    sys.exit(0)

visited = [[False]*m for _ in range(n)]

answer = 0
for i in range(len(M)):
    for j in range(len(M[0])):
        if M[i][j] == '.' or visited[i][j]:
            continue
        bfs(M, i, j, visited)
        answer += 1
print(answer)
