#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() template inline bool chmax(A &a, B b) { if (a inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef unsigned long long ull; typedef long long ll; typedef pair pii; typedef pair pll; typedef pair P; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-10; int w, h; string maps[20]; int cost[20][20]; int main() { cin >> w >> h; REP(i, h) cin >> maps[i]; fill(cost[0], cost[h], INF); priority_queue

pq; REP(i, h) REP(j, w) { if (maps[i][j] == '.') { pq.push(P(0, pii(i, j))); i = h; break; } } while (!pq.empty()) { P pos = pq.top(); pq.pop(); int c = pos.first; int x = pos.second.second; int y = pos.second.first; if (!chmin(cost[y][x], c)) continue; int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; REP(i, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) continue; int nc = c + (maps[ny][nx] == '#'); if (cost[ny][nx] <= nc) continue; pq.push(P(nc, pii(ny, nx))); } } int ans = INF; REP(i, h) REP(j, w) if (maps[i][j] == '.' && cost[i][j] != 0) chmin(ans, cost[i][j]); cout << ans << endl; return 0; }