H, W = list(map(int, input().split()))
S = []
data = []
for i in range(H):
  S.append(input())

for i in range(H):
  row = []
  for j in range(W):
    count = 0
    if S[i][j] == '#':
      row.append(10)
    else:
      if i != 0 and S[i-1][j] == '.':
        count += 1
      if i != (H-1) and S[i+1][j] == '.':
        count += 1
      if j != 0 and S[i][j-1] == '.':
        count += 1
      if j != (W-1) and S[i][j+1] == '.':
        count += 1
      row.append(count)
  data.append(row)
min = min([flatten for inner in data for flatten in inner])
startList = []
for i in range(H):
  for j in range(W):
    if data[i][j] == min:
      startList.append([i, j])
import random
now = startList[0]
end = startList[-1]
turn = 0
while(now != end):
  min = 10
  i, j = now[0], now[1]
  if i != 0 and data[i-1][j] != 10 and data[i-1][j] < min:
    min = data[i-1][j]
    next = [i-1, j]
  if i != (H-1) and data[i+1][j] != 10 and data[i+1][j] < min:
    min = data[i+1][j]
    next = [i+1, j]
  if j != 0 and data[i][j-1] != 10 and data[i][j-1] < min:
    min = data[i][j-1] 
    next = [i, j-1]
  if j != (W-1) and data[i][j+1] != 10 and data[i][j+1] < min:
    min = data[i][j+1]
    next = [i, j+1]
  data[i][j] = 10
  now = next
  turn += 1
print(turn)