#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } bool solve(int h, int w, vector> &c){ vector> tansaku(h-2, vector(w-2)); tansaku[0][0] = true; rep(i,0,3){ rep(j,0,3){ if (c[i][j] == '#') return false; } } vector> good(h-2, vector(w-2)); rep(x,0,h-2){ rep(y,0,w-2){ bool mode = true; rep(xx,x,x+3){ rep(yy,y,y+3){ if (c[xx][yy] == '#'){ mode = false; } } } good[x][y] = mode; } } vector> mada = {pair(0, 0)}; while(!mada.empty()){ auto [i, j] = mada.back(); mada.pop_back(); rep(x,i-1,i+2){ rep(y,j-1,j+2){ if (!(0 <= x && x < h-2)) continue; if (!(0 <= y && y < w-2)) continue; if (tansaku[x][y]) continue; if (!good[x][y]) continue; tansaku[x][y] = true; mada.push_back(pair(x, y)); } } } return tansaku[h-3][w-3]; } int main(){ int h, w; cin >> h >> w; vector> c(h, vector(w)); rep(i,0,h){ rep(j,0,w){ cin >> c[i][j]; } } if (!solve(h, w, c)){ cout << 0 << '\n'; return 0; } rep(i,0,h-1){ rep(j,0,w-1){ vector> cc = c; cc[i][j] = '#'; bool mode = true; rep(i,0,3){ rep(j,0,3){ if (cc[i][j] == '#') mode = false; } } rep(i,h-3,h){ rep(j,w-3,w){ if (cc[i][j] == '#') mode = false; } } if (!mode) continue; if (!solve(h, w, cc)){ cout << 1 << '\n'; return 0; } } } cout << 2 << '\n'; }