def check_slime(n, m, table):
    if table[n - 1][m - 1] == '#' or table[n - 2][m - 2] == '#' or table[n - 1][m - 2] == '#' or table[n - 2][m - 1] == '#':
        return -1
    else:
        return 5
n, m = map(int, input().split())
table = [input() for _ in range(n)]
result = check_slime(n, m, table)
print(result)

