#define _CRT_SECURE_NO_WARNINGS //#define _GLIBCXX_DEBUG #include using namespace std; typedef long long ll; typedef vector vi; typedef vector vvi; typedef pair pii; #define all(c) (c).begin(), (c).end() #define loop(i,a,b) for(ll i=a; i ostream & operator<<(ostream & os, vector const &); template typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple const &){} template typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple const & t){ os << (n==0?"":" ") << get(t); _ot(os, t); } template ostream & operator<<(ostream & os, tuple const & t){ _ot<0>(os, t); return os; } template ostream & operator<<(ostream & os, pair const & p){ return os << "(" << p.first << ", " << p.second << ") "; } template ostream & operator<<(ostream & os, vector const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; } #ifdef DEBUG #define dump(...) (cerr<<#__VA_ARGS__<<" = "< f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; int const inf = 10000000; void dijkstra(Graph const & g, int s, vector &dist) { typedef tuple State; priority_queue q; dist.assign(g.size(), inf); dist[s] = 0; q.emplace(0,s); while (q.size()) { Weight d; int v; tie(d,v) = q.top(); q.pop(); d *= -1; if (dist[v] < d) continue; rep(i,g[v].size()){ const Edge &e = g[v][i]; if (dist[e.dst] > dist[v] + e.weight) { dist[e.dst] = dist[v] + e.weight; q.emplace(-dist[e.dst], e.dst); } } } } char grid[30][30]; int W,H; int f(int i, int j){ return i*W+j; } int main(){ while(cin >> W >> H){ Graph g(H*W); rep(i,H) cin >> grid[i]; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; rep(i,H)rep(j,W){ rep(d,4){ int ni = i+dy[d]; int nj = j+dx[d]; if(ni < 0 || nj < 0) break; if(ni >= H || nj >= W) break; if(grid[ni][nj] == '#'){ g[f(i,j)].eb(f(i,j),f(ni,nj),1); } else { g[f(i,j)].eb(f(i,j),f(ni,nj),0); } } } int ans = 1000000000; rep(i,H)rep(j,W){ rep(ii,H)rep(jj,W){ if(grid[i][j]=='.' && grid[ii][jj]=='.'){ vi v; dijkstra(g,f(i,j),v); int x = v[f(ii,jj)]; if(x==0) continue; ans = min(ans,x); } } } cout << ans << endl; } }