// #pragma GCC target("avx") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define all(x) x.begin(),x.end() //#define mp make_pair #define vi vector #define vvi vector #define vvvi vector #define vvvvi vector #define pii pair #define vpii vector> template void chmax(T &a, const T &b) {a = (a > b? a : b);} template void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits::max() / 5000; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 998244353; int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; #define int long long template struct Edge { int to; T cap; int rev; Edge() = default; Edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {} }; template struct Dinic { const T e = numeric_limits::max(); const T zero = 0; int n; vector>> g; vector level, iter; vector> Edge_ID; Dinic(int n) : n(n), g(n) {} int add_edge(int from, int to, T cap) { Edge f(to, cap, g[to].size()); Edge t(from, zero, g[from].size()); int Now_Edge_ID = (int)Edge_ID.size(); Edge_ID.emplace_back(from, (int)g[from].size()); g[from].push_back(f); g[to].push_back(t); return Now_Edge_ID; } bool bfs(int s, int t) { level.assign(n, -1); level[s] = 0; queue que; que.push(s); while(que.size()) { int v = que.front(); que.pop(); for(auto e : g[v]) { if(e.cap > zero && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } return level[t] != -1; } T dfs(int v, int t, T f) { if(v == t) return f; for(int& i = iter[v]; i < g[v].size(); i++) { Edge& e = g[v][i]; if(e.cap > zero && level[v] < level[e.to]) { T d = dfs(e.to, t, min(f, e.cap)); if(d > zero) { e.cap -= d; g[e.to][e.rev].cap += d; return d; } } } return zero; } T flow(int s, int t) { T f = zero; while(bfs(s, t)) { iter.assign(n, 0); T d; while((d = dfs(s, t, e)) > zero) f += d; } return f; } //sから見たときのmin_cutにおいてs側に所属するかどうかを全頂点に対して返すO(N) vector min_cut(int s) { vector visited(n, false); queue que; visited[s] = true; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(auto e : g[v]) if(e.cap != 0 && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } return visited; } Edge get_edge(int edge_id) { return g[Edge_ID[edge_id].first][Edge_ID[edge_id].second]; } }; //bipartite matching //TODO: make faster Dinic using dynamic tree structure and current edge structure //燃やす埋めるref: https://qiita.com/ningenMe/items/69ed7ce43c9cd0a2de38 //add_edgeにintの返り値を持たせて、id = add_edge(), get_edge(id) = 元の辺 みたいにしたい。 //min_cut : 右側ならばtrueのvectorを返す。 //cut[from] = true && cut[to] = false --- from->toの辺が最小カットで切られる辺の一つ void solve() { int h, w; cin >> h >> w; vector s(h); FOR(h) cin >> s[i]; auto reach = [&]() -> bool { vector> can(h-2, vector(w-2, true)); rep(i, h-2) rep(j, w-2) { rep(px, 3) rep(py, 3) { if(s[i+px][j+py] == '#') can[i][j] = false; } } queue que; vector> vis(h-2, vector(w-2, false)); vis[0][0] = true; que.emplace(0, 0); while(!que.empty()) { int x, y; tie(x, y) = que.front(); que.pop(); rep(d, 8) { int nx = x + dx[d]; int ny = y + dy[d]; if(nx < 0 || ny < 0 || nx+2 >= h || ny+2 >= w) continue; if(!can[nx][ny] || vis[nx][ny]) continue; vis[nx][ny] = true; que.emplace(nx, ny); } } return vis[h-3][w-3]; }; if(!reach()) { cout << 0 << endl; return; } rep(i, h) rep(j, w) if(s[i][j] != '#') { if(i<3&&j<3) continue; if(i>=h-3&&j>=w-3) continue; s[i][j] = '#'; if(!reach()) { cout << 1 << endl; return; } s[i][j] = '.'; } cout << 2 << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }