#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60; const int IINF = (1 << 30) - 1; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector S(H); for(int i=0; i> S[i]; function&, int, int)> isexist = [&](vector &T, int si, int sj){ if(si==0||sj==0||si==H-1||sj==W-1) return false; bool ok = true; for(int i=si-1; i<=si+1; i++) for(int j=sj-1; j<=sj+1; j++){ if(T[i][j]=='#') ok = false; } return ok; }; function&)> isreach = [&](vector &T){ int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; vector> isvisited(H, vector(W, false)); isvisited[1][1] = true; queue> Q; Q.push({1, 1}); while(!Q.empty()){ int now_i = Q.front().fi; int now_j = Q.front().se; Q.pop(); for(int k=0; k<8; k++){ int nxt_i = now_i + di[k]; int nxt_j = now_j + dj[k]; if(!isexist(T, nxt_i, nxt_j)) continue; if(T[nxt_i][nxt_j]=='#') continue; if(isvisited[nxt_i][nxt_j]) continue; isvisited[nxt_i][nxt_j] = true; Q.push({nxt_i, nxt_j}); } } if(isvisited[H-2][W-2]) return true; else return false; }; //答えが0の場合 if(!isreach(S)){ cout << 0 << endl; return 0; } //答えが1の場合 for(int i=0; i=H-3&&j>=W-3)) continue; if(S[i][j]=='#') continue; vector T = S; T[i][j] = '#'; if(!isreach(T)){ cout << 1 << endl; return 0; } } cout << 2 << endl; return 0; }