W,H=[int(i) for i in input().split()] C=[] for i in range(H): line=input() C.append(line) # print(C) #一つ目の穴の検索 ana1=[] for i in range(H): find=False for j in range(W): if C[i][j]=='.': ana1.append([i,j]) find=True break if find==True: break #左上から右下へ深さ優先探索 def ana1_dfs(now): y=now[0] x=now[1] if C[y+1][x]=="." and [y+1,x] not in ana1: ana1.append([y+1,x]) # C[y+1][x]="#" line=C[y+1] line=line[:x]+'#'+line[x+1:] C[y+1]=line ana1_dfs([y+1,x]) if C[y][x+1]=="." and [y,x+1] not in ana1: ana1.append([y,x+1]) # C[y][x+1]="#" line=C[y] line=line[:x+1]+'#'+line[x+2:] C[y]=line ana1_dfs([y,x+1]) y=ana1[-1][0] x=ana1[-1][1] line=C[y] line=line[:x]+'#'+line[x+1:] C[y]=line ana1_dfs(ana1[-1]) # print(ana1) #ふたつ目の穴の検索 ana2=[] for i in range(H): find=False for j in range(W): if C[i][j]=='.': ana2.append([i,j]) find=True break if find==True: break #左上から右下へ深さ優先探索 def ana2_dfs(now): y=now[0] x=now[1] if C[y+1][x]=="." and [y+1,x] not in ana2: ana2.append([y+1,x]) # C[y+1][x]="#" line=C[y+1] line=line[:x]+'#'+line[x+1:] C[y+1]=line ana2_dfs([y+1,x]) if C[y][x+1]=="." and [y,x+1] not in ana2: ana2.append([y,x+1]) # C[y][x+1]="#" line=C[y] line=line[:x+1]+'#'+line[x+2:] C[y]=line ana2_dfs([y,x+1]) y=ana2[-1][0] x=ana2[-1][1] line=C[y] line=line[:x]+'#'+line[x+1:] C[y]=line ana2_dfs(ana2[-1]) # print(ana2) #ana1とana2の各要素の距離で最小のものを検索 tmp_min=H+W for ana1_mass in ana1: for ana2_mass in ana2: dist=abs(ana1_mass[0]-ana2_mass[0])+abs(ana1_mass[1]-ana2_mass[1])-1 # print(ana1_mass, ana2_mass, dist) if dist