import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
sys.setrecursionlimit(10**9)
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime
def run_length_encode(s):
    count = 1
    code = []
    target = s[0]
    for i in range(1, len(s)):
        if target == s[i]: count += 1
        else:
            code.append((target, count))
            target = s[i]
            count = 1
    code.append((target, count))
    return code

H,W = imn()
S = [input() for _ in range(H)]
an = 10**10
def m(a, b, c, d, cnt, done):
    if a < 0 or a >= H or b < 0 or b >= W: return False
    if S[a][b] == '#': return False 
    do = copy.deepcopy(done)
    global an
    if a == c and b == d:
        an = min(an, cnt)
        return True
    else:
        if an <= cnt: return
        do[a][b] = True
        cnt += 1
        if a-1 >= 0 and not done[a-1][b]: m(a-1,b,c,d,cnt,do)
        if b-1 >= 0 and not done[a][b-1]: m(a,b-1,c,d,cnt,do)
        if b+1 < W  and not done[a][b+1]: m(a,b+1,c,d,cnt,do)
        if a+1 < H  and not done[a+1][b]: m(a+1,b,c,d,cnt,do)
        return 
ans = -1
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if i == k and j == l: continue
                done = [[False for _ in range(W)]for _ in range(H)]
                m(i,j,k,l,0, copy.deepcopy(done))
                if an != 10**10:
                    ans = max(ans, an)
                an = 10**10 
print(ans)

