#include #include using namespace std; using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int h, w; cin >> h >> w; vector s(h); rep(i,h) cin >> s[i]; map> a, b; rep(i,h)rep(j,w){ a[i+j].push_back(-1); a[i+j].push_back(w); b[i-j].push_back(-1); b[i-j].push_back(w); if(s[i][j]=='.') { a[i+j].push_back(j); b[i-j].push_back(j); } } for(auto &p : a){ sort(p.second.begin(),p.second.end()); p.second.erase(unique(p.second.begin(),p.second.end()), p.second.end()); } for(auto &p : b){ sort(p.second.begin(),p.second.end()); p.second.erase(unique(p.second.begin(),p.second.end()), p.second.end()); } dbg(a,b); vector>> dp(4, vector>(h, vector(w))); rep(i,h)rep(j,w){ if(s[i][j]=='#'){ int p = lower_bound(a[i+j].begin(),a[i+j].end(), j) - a[i+j].begin(); int q = p-1; int r = lower_bound(b[i-j].begin(),b[i-j].end(), j) - b[i-j].begin(); int s = r-1; int d = min(a[i+j][p]-j, j-a[i+j][q]); d = min(d, b[i-j][r]-j); d = min(d, j-b[i-j][s]); if(d==1) continue; rep(k,4) dp[k][i][j] = max(dp[k][i][j], d); dbg(i,j,d); } } using R = tuple; priority_queue pq; rep(i,h)rep(j,w){ rep(k,4) pq.emplace(dp[k][i][j], i,j,k); } vector di = {-1,-1,1,1}; vector dj = {-1,1,-1,1}; while(!pq.empty()){ auto [d, x, y, k] = pq.top();pq.pop(); if(dp[k][x][y]!=d) continue; int nx = x + di[k]; int ny = y + dj[k]; if(nx<0 || ny<0 || nx>=h || ny>=w) continue; if(s[nx][ny]=='.') continue; if(dp[k][nx][ny]