#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i,n) for( int i = 0; i < n; i++ ) #define rrep(i,n) for( int i = n; i >= 0; i-- ) #define REP(i,s,t) for( int i = s; i <= t; i++ ) #define RREP(i,s,t) for( int i = s; i >= t; i-- ) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 int dx[4] = { 0, 1, 0, -1 }; int dy[4] = { 1, 0, -1, 0 }; char c[25][25]; typedef pair P; vector

v1; vector

v2; void dfs(int num, int x, int y) { //cout << x << " " << y << endl; if (num == 0) v1.push_back({ x, y }); else v2.push_back({ x, y }); c[y][x] = '#'; rep(i, 4) { int xx = x + dx[i]; int yy = y + dy[i]; if (c[yy][xx] == '.') dfs(num, xx, yy); } } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int W, H; cin >> W >> H; rep(i, H) rep(j, W) cin >> c[i][j]; int i = 0; int num = 0; while (num < 2) { int j = 0; while (j < W) { //cout << i << " " << j << endl; if (c[i][j] == '.') { dfs(num, j, i); num++; } j++; if (num == 2) break; } i++; } int ans = 1000; rep(i, v1.size()) { rep(j, v2.size()) { int dx = abs(v1[i].first - v2[j].first); int dy = abs(v1[i].second - v2[j].second); ans = min(ans, dx + dy); } } cout << ans - 1<< endl; return 0; }