#include #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H, W; cin >> H >> W; vector C(H); for (int i = 0; i < H; ++i) cin >> C[i]; int RH = 2 * H - 1; int CW = 2 * W - 1; const int INF = 1e9; auto passable = [&](int r, int c) { if ((r & 1) == 0 && (c & 1) == 0) { return C[r / 2][c / 2] != '#'; } return true; }; vector> dp(RH, vector(CW, INF)); dp[0][0] = 0; for (int r = 0; r < RH; ++r) { for (int c = 0; c < CW; ++c) { if (dp[r][c] == INF || !passable(r, c)) continue; if (c + 1 < CW && passable(r, c + 1)) { dp[r][c + 1] = min(dp[r][c + 1], dp[r][c] + 1); } if ((c & 1) == 0 && c + 2 < CW && passable(r, c + 2)) { dp[r][c + 2] = min(dp[r][c + 2], dp[r][c] + 1); } if (r + 1 < RH && passable(r + 1, c)) { dp[r + 1][c] = min(dp[r + 1][c], dp[r][c] + 1); } if ((r & 1) == 0 && r + 2 < RH && passable(r + 2, c)) { dp[r + 2][c] = min(dp[r + 2][c], dp[r][c] + 1); } } } int ans = dp[RH - 1][CW - 1]; cout << (ans == INF ? -1 : ans) << '\n'; return 0; }