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(); } HashSet lefts = new HashSet<>(); HashSet rights = new HashSet<>(); boolean used = false; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (field[i][j] == '.') { HashSet tmp; if (used) { tmp = rights; } else { tmp = lefts; used = true; } PriorityQueue queue = new PriorityQueue<>(); queue.add(i * w + j); while (queue.size() > 0) { int x = queue.poll(); if (field[x / w][x % w] == '#') { continue; } field[x / w][x % w] = '#'; queue.add(x + 1); queue.add(x - 1); queue.add(x + w); queue.add(x - w); tmp.add(x); } } } } int min = Integer.MAX_VALUE; for (int x : lefts) { for (int y : rights) { min = Math.min(min, getDist(x, y, w)); } } System.out.println(min); } static int getDist(int x, int y, int width) { return Math.abs(x / width - y / width) + Math.abs(x % width - y % width) - 1; } }