#include <array>
#include <iostream>
#include <vector>

using namespace std;

int dx[4]{-1,0,1,0};
int dy[4]{0,1,0,-1};
int main() {
  int r, c;
  cin >> r >> c;
  vector<string> g(r);
  vector<vector<int>> dp(r);
  for(int i = 0; i < r; i++) {
    cin >> g[i];
    dp[i].assign(c, -1);
  }
  int ret = 1;
  vector<array<int, 2>> q;
  for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) {
    if(g[i][j] != 'X') continue;
    bool bad = true;
    for(int k = 0; k < 4 && bad; k++) {
      int nx = i + dx[k];
      int ny = j + dy[k];
      if(nx < 0 || nx >= r || ny < 0 || ny >= c || g[nx][ny] != 'X') bad = false;
    }
    if(!bad) {
      q.push_back({i, j});
      dp[i][j] = ret;
    }
  }
  while(q.size()) {
    vector<array<int, 2>> nq;
    for(auto [x, y]: q) {
      ret = dp[x][y];
      for(int k = 0; k < 4; k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if(nx >= 0 && nx < r && ny >= 0 && ny < c && dp[nx][ny] == -1 && g[nx][ny] == 'X') {
          dp[nx][ny] = ret + 1;
          nq.push_back({nx, ny});
        }
      }
    }
    q.swap(nq);
  }
  cout << ret << "\n";
}