import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); PriorityQueue queue = new PriorityQueue<>(); short[][] values = new short[h][w]; for (int i = 0; i < h; i++) { char[] field = sc.next().toCharArray(); for (int j = 0; j < w; j++) { if (field[j] == '.') { queue.add(new Path(i, j, (short)0)); } } Arrays.fill(values[i], Short.MAX_VALUE); queue.add(new Path(i, 0, (short)1)); queue.add(new Path(i, w - 1, (short)1)); } for (int i = 0; i < w; i++) { queue.add(new Path(0, i, (short)1)); queue.add(new Path(h - 1, i, (short)1)); } int max = 0; while (queue.size() > 0) { Path p = queue.poll(); if (values[p.h][p.w] <= p.value) { continue; } values[p.h][p.w] = p.value; max = Math.max(max, p.value); if (p.h > 0 && values[p.h - 1][p.w] > p.value + 1) { queue.add(new Path(p.h - 1, p.w, (short)(p.value + 1))); } if (p.h < h - 1 && values[p.h + 1][p.w] > p.value + 1) { queue.add(new Path(p.h + 1, p.w, (short)(p.value + 1))); } if (p.w > 0 && values[p.h][p.w - 1] > p.value + 1) { queue.add(new Path(p.h, p.w - 1, (short)(p.value + 1))); } if (p.w < w - 1 && values[p.h][p.w + 1] > p.value + 1) { queue.add(new Path(p.h, p.w + 1, (short)(p.value + 1))); } if (p.h > 0 && p.w > 0 && values[p.h - 1][p.w - 1] > p.value + 1) { queue.add(new Path(p.h - 1, p.w - 1, (short)(p.value + 1))); } if (p.h > 0 && p.w < w - 1 && values[p.h - 1][p.w + 1] > p.value + 1) { queue.add(new Path(p.h - 1, p.w + 1, (short)(p.value + 1))); } if (p.h < h - 1 && p.w > 0 && values[p.h + 1][p.w - 1] > p.value + 1) { queue.add(new Path(p.h + 1, p.w - 1, (short)(p.value + 1))); } if (p.h < h - 1 && p.w < w - 1 && values[p.h + 1][p.w + 1] > p.value + 1) { queue.add(new Path(p.h + 1, p.w + 1, (short)(p.value + 1))); } } System.out.println(max); } static class Path implements Comparable { int h; int w; short value; public Path(int h, int w, short value) { this.h = h; this.w = w; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }