#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define MAX_MOD 1000000007 #define REP(i,n) for(int i = 0;i < n;++i) long long dijk[5000][5000] = {}; bool ok[5000][5000] = {}; int main() { int h, w; cin >> h >> w; queue> go[2]; for (int i = 1;i <= h;++i) { go[0].push(make_pair(i,0)); go[0].push(make_pair(i, w + 1)); } for (int i = 1;i <= w;++i) { go[0].push(make_pair(0, i)); go[0].push(make_pair(h + 1, i)); } REP(i, h) { string hoge; cin >> hoge; REP(q, w) { if (hoge[q] == '.') { ok[i + 1][q + 1] = 1; go[0].push(make_pair(i + 1, q + 1)); } else { dijk[i + 1][q + 1] = -1; } } } for (int i = 0;i >= 0;++i) { if (go[i % 2].empty() == true) { cout << i-1 << endl; return 0; } while (go[i % 2].empty() == false) { pair now = go[i % 2].front(); go[i % 2].pop(); if (dijk[now.first+1][now.second] == -1) { go[(i + 1) % 2].push(make_pair(now.first + 1, now.second)); dijk[now.first + 1][now.second] = i+1; } if (now.first != 0&&dijk[now.first - 1][now.second] == -1) { go[(i + 1) % 2].push(make_pair(now.first - 1, now.second)); dijk[now.first - 1][now.second] = i+1; } if (dijk[now.first][now.second + 1] == -1) { go[(i + 1) % 2].push(make_pair(now.first, now.second + 1)); dijk[now.first][now.second + 1] = i+1; } if (now.second != 0&&dijk[now.first][now.second - 1] == -1) { go[(i + 1) % 2].push(make_pair(now.first, now.second - 1)); dijk[now.first][now.second - 1] = i+1; } if (dijk[now.first + 1][now.second + 1] == -1) { go[(i + 1) % 2].push(make_pair(now.first + 1, now.second + 1)); dijk[now.first + 1][now.second + 1] = i + 1; } if (now.first != 0&&dijk[now.first - 1][now.second + 1] == -1) { go[(i + 1) % 2].push(make_pair(now.first - 1, now.second + 1)); dijk[now.first - 1][now.second + 1] = i + 1; } if (now.second != 0 && dijk[now.first + 1][now.second - 1] == -1) { go[(i + 1) % 2].push(make_pair(now.first + 1, now.second - 1)); dijk[now.first + 1][now.second - 1] = i + 1; } if (now.second != 0 && now.first != 0 && dijk[now.first - 1][now.second - 1] == -1) { go[(i + 1) % 2].push(make_pair(now.first - 1, now.second - 1)); dijk[now.first - 1][now.second - 1] = i + 1; } } } }