#include #include #include #include #include #include #include #include #include #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) typedef long long ll; using namespace std; int inputValue(){ int a; cin >> a; return a; }; void inputArray(int * p, int a){ rep(i, a){ cin >> p[i]; } }; void inputVector(vector * p, int a){ rep(i, a){ int input; cin >> input; p -> push_back(input); } } template void output(T a, int precision) { if(precision > 0){ cout << setprecision(precision) << a << "\n"; } else{ cout << a << "\n"; } } bool mark[20][20] = {false}; char c[20][20]; int vx[] = {1, -1, 0, 0}; int vy[] = {0, 0, 1, -1}; void dfs(int x, int y){ mark[x][y] = true; rep(i, 4){ if(mark[x + vx[i]][y + vy[i]] == false && c[x + vx[i]][y + vy[i]] == '.' && x + vx[i] >= 0 && y + vy[i] >= 0){ dfs(x + vx[i], y + vy[i]); } } } int dist(pair p, pair q){ return abs(p.first - q.first) + abs(p.second - q.second) - 1; } int main(int argc, const char * argv[]) { // source code int width = inputValue(); int height = inputValue(); rep(i, height){ string str; cin >> str; rep(j, width){ c[i][j] = str[j]; } } int start[2] = {0}; // define start rep(i, height){ rep(j, width){ if(c[i][j] == '.'){ start[0] = i; start[1] = j; break; } } if(start[0] != 0 && start[1] != 0) break; } dfs(start[0], start[1]); vector> pos; vector> neg; rep(i, height){ rep(j, width){ if(mark[i][j] == true){ pos.push_back(make_pair(i, j)); } } } rep(i, height){ rep(j, width){ if(mark[i][j] == false && c[i][j] == '.'){ neg.push_back(make_pair(i, j)); } } } int distance = 1000; rep(p, pos.size()){ rep(q, neg.size()){ distance = min(dist(pos[p], neg[q]), distance); } } output(distance, 0); return 0; }