#include //#include using namespace std; //using namespace atcoder; using ll = long long; using ull = unsigned long long; using ld = long double; //using mint = modint998244353; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll h,w; cin >> h >> w; vector mass(h); for(ll i = 0; i < h; i++){ cin >> mass[i]; } ll ans = h+w; vector dx = {1,0,-1,0}; vector dy = {0,1,0,-1}; vector> come(h,vector(w,0)); queue que; ll maxx = 0; ll maxy = 0; que.push(0); come[0][0] = 1; while(!que.empty()){ ll x = que.front() / 10000; ll y = que.front() % 10000; que.pop(); for(ll i = 0; i < 2; i++){ ll xx = x + dx[i]; ll yy = y + dy[i]; if(xx >= h)continue; if(yy >= w)continue; if(come[xx][yy])continue; if(mass[xx][yy] == '#')continue; come[xx][yy] = 1; maxx = max(maxx,xx); maxy = max(maxy,yy); que.push(xx * 10000 + yy); } } if(come[h-1][w-1])ans -= 2; else{ ll minx = h-1; ll miny = w-1; que.push((h-1)*10000 + w-1); come[h-1][w-1] = 1; while(!que.empty()){ ll x = que.front() / 10000; ll y = que.front() % 10000; que.pop(); for(ll i = 2; i < 4; i++){ ll xx = x + dx[i]; ll yy = y + dy[i]; if(xx < 0)continue; if(yy < 0)continue; if(come[xx][yy])continue; if(mass[xx][yy] == '#')continue; come[xx][yy] = 1; minx = min(minx,xx); miny = min(miny,yy); que.push(xx * 10000 + yy); } } if(minx - maxx < 2 || miny - maxy < 2)ans--; } cout << ans << '\n'; }