import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); char[][] field = new char[h][]; for (int i = 0; i < h; i++) { field[i] = sc.next().toCharArray(); } ArrayDeque deq = new ArrayDeque<>(); for (int i = 1; i < h - 1 && deq.size() == 0; i++) { for (int j = 1; j < w - 1 && deq.size() == 0; j++) { if (field[i][j] == '.') { deq.add(i * w + j); } } } HashSet holeA = new HashSet<>(); while (deq.size() > 0) { int x = deq.poll(); if (field[x / w][x % w] == '.' && !holeA.contains(x)) { holeA.add(x); deq.add(x - 1); deq.add(x + 1); deq.add(x - w); deq.add(x + w); } } int min = Integer.MAX_VALUE; for (int i = 1; i < h - 1; i++) { for (int j = 1; j < w - 1; j++) { if (field[i][j] == '.' && !holeA.contains(i * w + j)) { for (int x : holeA) { min = Math.min(min, Math.abs(x / w - i) + Math.abs(x % w - j)); } } } } System.out.println(min - 1); } }