my_stack = []


def find_depth(mass, i, j, n, m, step):
    if i > 0:
        if mass[i - 1][j] == '*':
            mass[i - 1][j] = step
            my_stack.append((mass, i - 1, j, n, m, step))
    if j > 0:
        if mass[i][j - 1] == '*':
            mass[i][j - 1] = step
            my_stack.append((mass, i, j - 1, n, m, step))
    if i < n - 1:
        if mass[i + 1][j] == '*':
            mass[i + 1][j] = step
            my_stack.append((mass, i + 1, j, n, m, step))
    if j < m - 1:
        if mass[i][j + 1] == '*':
            mass[i][j + 1] = step
            my_stack.append((mass, i, j + 1, n, m, step))


size = input().split(" ")
n = int(size[0])
m = int(size[1])

field = []
for i in range(n):
    string = input()
    field.append([])
    for j in range(m):
        field[i].append(string[j])

result = 1

# проверка по строкам
row_n = 0
for i in range(n):
    current_row = 0
    point = -1
    for j in range(m):
        if field[i][j] == '#':
            current_row += 1
            if point == -1:
                point = j
            else:
                if point == j - 1:
                    point = j
                else:
                    result = 0
                    break
    if result != 0:
        if current_row == 0:
            row_n += 1
    else:
        break

# проверка по столбцам
row_m = 0
for j in range(m):
    current_row = 0
    point = -1
    for i in range(n):
        if field[i][j] == '#':
            current_row += 1
            if point == -1:
                point = i
            else:
                if point == i - 1:
                    point = i
                else:
                    result = 0
                    break
    if result != 0:
        if current_row == 0:
            row_m += 1
    else:
        break

if (row_n == 0 and row_m > 0) or (row_n > 0 and row_m == 0):
    result = 0

if result == 0:
    print(-1)
else:
    step = 0
    for i in range(n):
        for j in range(m):
            if field[i][j] == '#':
                if i == 0 or i == n-1 or j == 0 or j == m-1:
                    field[i][j] = '*'
                else:
                    test = 0
                    for x in range(3):
                        for y in range(3):
                            if field[i - 1 + x][j - 1 + y] == '.':
                                test = 1
                                break
                        if test == 1:
                            field[i][j] = '*'
                            break

    for i in range(n):
        for j in range(m):
            if field[i][j] == '*':
                step += 1
                field[i][j] = step
                my_stack.append((field, i, j, n, m, step))
                while my_stack:
                    find_depth(*my_stack.pop())
    print(step)

