n, m = map(int, input().split())
table = [list(input()) for _ in range(n)]

# Initialize a count for transformations
transformations = 0

# Iterate through the table to find the path
for i in range(n - 2):
    for j in range(m - 2):
        if (
            table[i][j] == "#" or
            table[i + 1][j] == "#" or
            table[i][j + 1] == "#" or
            table[i + 1][j + 1] == "#"
        ):
            # If there's a hole in the 2x2 block, the slime cannot move through
            print(-1)
            exit()
        else:
            # Increment transformations
            transformations += 1

# Output the total transformations
print(transformations + 4)
