// Arup Guha
// 9/29/2023
// Solution to 2022 SER D1/D2 Problem: Chocolate Chip Fabrication

import java.util.*;

public class chocolatechip {
	
	final public static int NOTUSED = -1;

	// Directions of movement.
	final public static int[] DX = {-1,0,0,1};
	final public static int[] DY = {0,-1,1,0};
	
	// Store grid.
	public static int r;
	public static int c;
	public static boolean[][] g;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		r = stdin.nextInt();
		c = stdin.nextInt();
		g = new boolean[r][];
		
		// Store true in grid for where chips needs to be.
		for (int i=0; i<r; i++) {
			char[] line = stdin.next().toCharArray();
			g[i] = new boolean[c];
			for (int j=0; j<c; j++) 
				g[i][j] = (line[j] == 'X');
		}
		
		// Set up queue, distance array for BFS.
		LinkedList<Integer> q = new LinkedList<Integer>();
		int[][] dist = getInitDist(q);
		int res = q.size() > 0 ? 1 : 0;
		
		// Run BFS.
		while (q.size() > 0) {
			
			// Get next.
			int cur = q.poll();
			int cX = cur/c;
			int cY = cur%c;
			
			// Try all four.
			for (int i=0; i<DX.length; i++) {
				int nX = cX + DX[i];
				int nY = cY + DY[i];
				
				// Skip in these situations.
				if (!inbounds(nX, nY)) continue;
				if (!g[nX][nY]) continue;
				if (dist[nX][nY] != NOTUSED) continue;
				
				// nX, nY should be added to the queue...
				q.offer(nX*c+nY);
				dist[nX][nY] = dist[cX][cY]+1;
				res = Math.max(res, dist[nX][nY]);
			}
		}
	
		// Ta da!
		System.out.println(res);
	}
	
	public static int[][] getInitDist(LinkedList<Integer> q) {
	
		int[][] res = new int[r][c];
		for (int i=0; i<r; i++) Arrays.fill(res[i], NOTUSED);
		
		// Go through each square.
		for (int i=0; i<r; i++) {
			for (int j=0; j<c; j++) {
			
				if (!g[i][j]) continue;
				
				// Try each direction.
				for (int dir=0; dir<DX.length; dir++) {
					
					int nX = i+DX[dir];
					int nY = j+DY[dir];
					
					// Not fully covered, so we can start here.
					if (!inbounds(nX, nY) || !g[nX][nY]) {
						q.offer(i*c+j);
						res[i][j] = 1;
						break;
					}
				}
			}
		}
		
		// Initial distance array.
		return res;
	}
	
	public static boolean inbounds(int x, int y) {
		return x>=0 && x<r && y>=0 && y<c;
	}
}
