#include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { constexpr int L = 3; int h, w; cin >> h >> w; vector s(h); REP(i, h) cin >> s[i]; const auto can_reach = [&]() -> bool { static vector is_visited(h - L + 1, vector(w - L + 1, 0)); REP(i, h - L + 1) ranges::fill(is_visited[i], false); is_visited[0][0] = true; for (queue> que({{0, 0}}); !que.empty(); que.pop()) { const auto [i, j] = que.front(); REP(k, 8) { const int y = i + DY8[k], x = j + DX8[k]; if (0 <= y && y + L - 1 < h && 0 <= x && x + L - 1 < w && !is_visited[y][x]) { bool is_valid = true; REP(r, L) REP(c, L) is_valid &= s[y + r][x + c] == '.'; if (is_valid) { is_visited[y][x] = true; que.emplace(y, x); } } } } return is_visited[h - L][w - L]; }; if (!can_reach()) { cout << 0 << '\n'; return 0; } REP(i, h) REP(j, w) { if ((i < L && j < L) || (i >= h - L && j >= w - L) || s[i][j] == '#') continue; s[i][j] = '#'; if (!can_reach()) { cout << 1 << '\n'; return 0; } s[i][j] = '.'; } cout << 2 << '\n'; return 0; }