#include using namespace std; const int INF = 1000000; int main(){ int H, W; cin >> H >> W; vector> S(H + 2, vector(W + 2, '#')); for (int i = 1; i <= H; i++){ for (int j = 1; j <= W; j++){ cin >> S[i][j]; } } vector> d(H + 2, vector(W + 2, -1)); deque> dq; for (int i = 4; i < H + 2; i++){ dq.push_back(make_tuple(0, i, 0)); } for (int i = 1; i < W - 2; i++){ dq.push_back(make_tuple(0, H + 1, i)); } while (!dq.empty()){ int c = get<0>(dq.front()); int x = get<1>(dq.front()); int y = get<2>(dq.front()); dq.pop_front(); if (d[x][y] == -1){ d[x][y] = c; for (int i = -3; i <= 3; i++){ for (int j = -3; j <= 3; j++){ if (abs(i) + abs(j) < 6){ int x2 = x + i; int y2 = y + j; if (0 <= x2 && x2 < H + 2 && 0 <= y2 && y2 < W + 2){ if (d[x2][y2] == -1){ if (!(x2 < 4 && y2 < 4) && !(x2 >= H - 2 && y2 >= W - 2)){ if (S[x2][y2] == '#'){ dq.push_front(make_tuple(c, x2, y2)); } else { dq.push_back(make_tuple(c + 1, x2, y2)); } } } } } } } } } int ans = INF; for (int i = 4; i < W + 2; i++){ ans = min(ans, d[0][i]); } for (int i = 1; i < H - 2; i++){ ans = min(ans, d[i][W + 1]); } cout << ans << endl; }