#include int dmap[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; int main () { int h = 0; int w = 0; char s[400][401] = {}; int res = 0; int is_ok = 0; int r[400][400] = {}; int org_flag[400][400] = {}; int flag[400][400] = {}; int q[2400][2] = {}; int q_hd = 0; int q_tl = 0; res = scanf("%d", &h); res = scanf("%d", &w); for (int i = 0; i < h; i++) { res = scanf("%s", s[i]); } for (int i = 0; i < h-2; i++) { for (int j = 0; j < w-2; j++) { for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { if (s[i+k][j+l] == '#') { flag[i][j] = 1; org_flag[i][j] = 1; } } } } } r[0][0] = 1; q[q_tl][0] = 0; q[q_tl][1] = 0; q_tl++; while (q_hd < q_tl) { int ci = q[q_hd][0]; int cj = q[q_hd][1]; q_hd++; for (int i = 0; i < 4; i++) { int ni = ci+dmap[i][0]; int nj = cj+dmap[i][1]; if (ni >= 0 && ni < h-2 && nj >= 0 && nj < w-2 && flag[ni][nj] == 0 && r[ni][nj] <= 0) { r[ni][nj] = 1; q[q_tl][0] = ni; q[q_tl][1] = nj; q_tl++; } } } if (r[h-3][w-3] <= 0) { printf("0\n"); return 0; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if ((i > 2 || j > 2) && (i < h-3 || j < w-3) && s[i][j] == '.') { for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { if (i-k >= 0 && i-k < h-2 && j-l >= 0 && j-l < w-2) { flag[i-k][j-l] = 1; } } } for (int i = 0; i < h-2; i++) { for (int j = 0; j < w-2; j++) { r[i][j] = 0; } } q_hd = 0; q_tl = 0; r[0][0] = 1; q[q_tl][0] = 0; q[q_tl][1] = 0; q_tl++; while (q_hd < q_tl) { int ci = q[q_hd][0]; int cj = q[q_hd][1]; q_hd++; for (int i = 0; i < 4; i++) { int ni = ci+dmap[i][0]; int nj = cj+dmap[i][1]; if (ni >= 0 && ni < h-2 && nj >= 0 && nj < w-2 && flag[ni][nj] == 0 && r[ni][nj] <= 0) { r[ni][nj] = 1; q[q_tl][0] = ni; q[q_tl][1] = nj; q_tl++; } } } if (r[h-3][w-3] <= 0) { is_ok = 1; } for (int i = 0; i < h-2; i++) { for (int j = 0; j < w-2; j++) { flag[i][j] = org_flag[i][j]; } } } } } if (is_ok > 0) { printf("1\n"); } else { printf("2\n"); return 1; } return 0; }