#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,m,n) for(int i = m;i < n;++i) using namespace std; using ll = long long; using R = double; const ll inf = 1LL << 50; const ll MOD = 1e9 + 7; string B[22]; int vis[22][22]; int dx[4] = { -1,0,1,0 }, dy[4] = {0,1,0,-1}; int w, h; int cnt = 0; vector< vector< pair > >wh(2); void check(int sx,int sy) { stack>st; vis[sy][sx] = 1; st.push({sx,sy}); wh[cnt].push_back({sx,sy}); while (!st.empty()) { int x, y; bool ok = 0; auto s = st.top(); tie(x, y) = s; rep(d, 0, 4) { int xx = x + dx[d]; int yy = y + dy[d]; if (0 <= xx && xx <= w - 1 && 0 <= yy && yy <= h - 1) { if (vis[yy][xx] == 0 && B[yy][xx] != '#') { vis[yy][xx] = 1; st.push({xx,yy}); wh[cnt].push_back({ xx,yy }); ok = 1; break; } } } if (!ok) { st.pop(); } } return; } int main() { cin >> w >> h; rep(i, 0, h)cin >> B[i]; rep(i, 0, h) { rep(j, 0, w) { if (B[i][j] == '.' && vis[i][j] == 0 && cnt == 0) { check(j,i); cnt++; } else if (B[i][j] == '.' && vis[i][j] == 0 && cnt == 1) { check(j, i); } } } int ans = 101010; rep(i, 0, wh[0].size()) { rep(j, 0, wh[1].size()) { ans = min(abs(wh[0][i].first - wh[1][j].first) + abs(wh[0][i].second - wh[1][j].second)-1, ans); } } cout << ans << endl; return 0; }