n, m = map(int, input().split())
table = [list(input()) for _ in range(n)]
if table[0][0] == '#':
    if table[0][1] == '#':
        starting_side = 'down'
    else:
        starting_side = 'right'
else:
    if table[0][1] == '#':
        starting_side = 'down'
    else:
        starting_side = 'right'
transformation_sequence = {
    'right': ['right', 'down', 'down', 'right'],
    'down': ['down', 'right', 'right', 'down']
}
transformations = transformation_sequence[starting_side]
for transformation in transformations:
    if x < n and y < m:
        if transformation == 'right':
            y += 1
        elif transformation == 'down':
            x += 1
        if x < n and y < m and table[x][y] == '#':
            print(-1)
            break
else:
    if x == n - 1 and y == m - 1:
        print(len(transformations))
    else:
        print(-1)