#include #include #include #include #include using namespace std; template inline void hash_combine(std::size_t& seed, const T& v) { hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); } struct SimpleHash { size_t operator()(const std::pair& p) const { size_t seed = 0; hash_combine(seed, p.first); hash_combine(seed, p.second); return seed; } }; int main() { int H, W; cin >> H >> W; string s; std::unordered_set, SimpleHash> frontier; std::unordered_set, SimpleHash> new_frontier; std::unordered_set, SimpleHash> visited; for (int i = 0; i < H; i++) { cin >> s; for (int j = 0; j < W; j++) { if (s[j] == '.') frontier.emplace(i, j); } } for (int i = -1; i < H+1; i++) { frontier.emplace(i, -1); frontier.emplace(i, W); } for (int j = -1; j < W+1; j++) { frontier.emplace(-1, j); frontier.emplace(H, j); } int dr[8] = {-1, -1 , -1, 0, 0, 1, 1, 1}; int dc[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int nr, nc; pair nrnc; int ans = -1; while ((int)frontier.size() > 0) { ans += 1; new_frontier.clear(); for (const auto& elem: frontier) { for (int i = 0; i < 8; i++) { nr = elem.first + dr[i]; nc = elem.second+ dc[i]; if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue; nrnc = pair(nr, nc); if (visited.find(nrnc) != visited.end() || frontier.find(nrnc) != frontier.end()) continue; new_frontier.insert(nrnc); } } for (const auto& elem: frontier) visited.insert(elem); frontier.swap(new_frontier); } cout << ans << endl; return 0; }