#include using namespace std; template istream &operator>>(istream &is, vector &v) { for (T &in : v) is >> in; return is; } template ostream &operator<<(ostream &os, const vector &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } #define OVERLOAD_REP(_1, _2, _3, name, ...) name #define REP1(i, n) for (auto i = std::decay_t{}; (i) != (n); ++(i)) #define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i)) #define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__) #define sum(l) accumulate(l.begin(), l.end(), 0) #define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__) #define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__) using ull = unsigned long long; using ll = long long; using vi = vector; using vl = vector; using vll = vector; using vvi = vector; using vvl = vector; using vvll = vector; using vs = vector; using pii = pair; using vpii = vector; using vc = vector; using vvc = vector; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vs C(H); cin >> C; vvc from(H, vc(W, 0)); vvc to(H, vc(W, 0)); rep(i, H) { rep(j, W) { if (C[i][j] == '#') continue; if (i == 0 && j == 0) from[i][j] = 1; if (i > 0 && from[i - 1][j]) from[i][j] = 1; if (j > 0 && from[i][j - 1]) from[i][j] = 1; } } for (int i = H - 1; i >= 0; --i) { for (int j = W - 1; j >= 0; --j) { if (C[i][j] == '#') continue; if (i == H - 1 && j == W - 1) to[i][j] = 1; if (i + 1 < H && to[i + 1][j]) to[i][j] = 1; if (j + 1 < W && to[i][j + 1]) to[i][j] = 1; } } if (from[H - 1][W - 1]) { cout << H + W - 2 << '\n'; return 0; } bool ok = false; rep(i, H - 1) { bool pref = false; rep(j, W) { if (from[i][j]) pref = true; if (pref && to[i + 1][j]) { ok = true; break; } } if (ok) break; } if (!ok) { rep(j, W - 1) { bool pref = false; rep(i, H) { if (from[i][j]) pref = true; if (pref && to[i][j + 1]) { ok = true; break; } } if (ok) break; } } if (ok) cout << H + W - 1 << '\n'; else cout << H + W << '\n'; return 0; }