#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; } int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; vector s(h); rep(i, h) cin >> s[i]; s[0][0] = s[h-1][w-1] = '.'; auto dpf = vector(h, vector(w)); auto dpb = vector(h, vector(w)); dpf[0][0] = dpb[h-1][w-1] = true; rep(i, h) rep(j, w) if (dpf[i][j]) { if (i+1 < h && s[i+1][j] == '.') dpf[i+1][j] = true; if (j+1 < w && s[i][j+1] == '.') dpf[i][j+1] = true; } rrep(i, h) rrep(j, w) if (dpb[i][j]) { if (i && s[i-1][j] == '.') dpb[i-1][j] = true; if (j && s[i][j-1] == '.') dpb[i][j-1] = true; } int ans = h + w; if (dpf[h-1][w-1]) chmin(ans, h + w - 2); rep(i, h-1) { int jf = 0, jb = w - 1; while (jf < w && !dpf[i][jf]) jf++; while (jb >= 0 && !dpb[i+1][jb]) jb--; if (jf <= jb) chmin(ans, h + w - 1); } rep(j, w-1) { int i1 = 0, i2 = h - 1; while (i1 < h && !dpf[i1][j]) i1++; while (i2 >= 0 && !dpb[i2][j+1]) i2--; if (i1 <= i2) chmin(ans, h + w - 1); } cout << ans << '\n'; }