#include #include #include #include #include #include #include #include using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define array2(type, x, y) array, x> #define vector2(type) vector > #define out(...) //printf(__VA_ARGS__) typedef pair pos; int pos::*x = &pos::first; int pos::*y = &pos::second; int dxy[] = {0, 1, 0, -1, 0}; /*================================*/ #define MAX 20 int W, H; pos start; static bool isCombine(bool map[MAX][MAX]) { // マップ内の空洞数 int space = 0; REP(h,H)REP(w,W) { if (map[w][h]) space++; } // スタートから繋がってる場所カウント queue que; que.push(start); vector checked; checked.push_back(start); int space2 = 1; while (que.size()) { pos p = que.front(); que.pop(); REP(i,4) { pos np; np.*x = p.first + dxy[i]; np.*y = p.second + dxy[i+1]; if (np.*x < 0 || W <= np.*x) continue; if (np.*y < 0 || H <= np.*y) continue; if (CONTAIN(checked, np)) continue; if (map[np.*x][np.*y]) { checked.push_back(np); que.push(np); space2++; } } } //cout << "space: " << space << "," << space2 << endl; return (space == space2); } void expand(bool map[MAX][MAX]) { // スタートから繋がってる場所 queue que; que.push(start); vector checked; checked.push_back(start); while (que.size()) { pos p = que.front(); que.pop(); REP(i,4) { pos np; np.*x = p.first + dxy[i]; np.*y = p.second + dxy[i+1]; if (np.*x < 0 || W <= np.*x) continue; if (np.*y < 0 || H <= np.*y) continue; if (CONTAIN(checked, np)) continue; if (map[np.*x][np.*y]) { checked.push_back(np); que.push(np); } } } // 空洞の一つ隣を全部空ける REP(j,checked.size()) { pos p = checked[j]; REP(i,4) { pos np; np.*x = p.first + dxy[i]; np.*y = p.second + dxy[i+1]; if (np.*x < 0 || W <= np.*x) continue; if (np.*y < 0 || H <= np.*y) continue; if (CONTAIN(checked, np)) continue; map[np.*x][np.*y] = true; // 無条件で空洞に } } } signed main() { cin >> W >> H; bool map[MAX][MAX]; REP(h,H) { REP(w,W) { char c; cin >> c; // 空洞はtrue map[w][h] = (c == '.'); if (start.*x == 0 && map[w][h]) { start.*x = w; start.*y = h; } } } int ccnt = 1; while (true) { expand(map); if (isCombine(map)) break; ccnt++; if (ccnt > 20) break; } cout << ccnt << endl; return 0; }