#include #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, a) type a;std::cin>>a; // VAR(int, x); #define OUT(d) std::cout<<(d); #define FOUT(n, d) std::cout< c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector> c(m, std::vector(n));for(auto& r:c)for(auto& i:r)std::cin>>i; #define ALL(a) (a).begin(),(a).end() #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) for (int i=0;i<(n);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define POS std::pair #define IN(a0, y, a1, b0, x, b1) (a0<=y && y dy{{-1, 0, 1, 0}}; std::array dx{{ 0, 1, 0, -1}}; signed main(){ //INIT; VAR(int, w)VAR(int, h); std::vector> P(h, std::vector(w)); REP(i, h)REP(j, w){ char ch; std::cin >> ch; P[i][j].y = i; P[i][j].x = j; P[i][j].visited = ch=='#'; } std::vector> cave(2); std::stack st; REP(ci, 2){ REP(i, h){ REP(j, w){ if(!P[i][j].visited){ st.push(P[i][j]); goto BFS; } } } BFS: while(!st.empty()){ Pos t = st.top(); st.pop(); Pos& now = P[t.y][t.x]; now.visited = true; cave[ci].push_back(POS(now.y, now.x)); REP(i, 4){ Pos& check = P[now.y+dy[i]][now.x+dx[i]]; if(!check.visited){ check.visited = true; st.push(check); } } } } int min = 40; for(auto& p0 : cave[0]){ for(auto& p1 : cave[1]){ min = std::min(min, static_cast(std::fabs(p0.first-p1.first)+std::fabs(p0.second-p1.second))); } } OUT(min-1)BR; return 0; }