using System.Linq; using System.Collections.Generic; using System; public class P { public int x { get; set; } public int y { get; set; } } public class Hello { public static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var w = int.Parse(line[0]); var h = int.Parse(line[1]); var map = new int[h, w]; var fx = 0; var fy = 0; for (int i = 0; i < h; i++) { var s = Console.ReadLine().Trim(); for (int j = 0; j < w; j++) if (s[j] == '.') map[i, j] = 1; } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (map[i, j] == 1) { fx = i; fy = j; goto next; } next:; var a1 = goDfs(map, fx, fy); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (map[i, j] == 1) { fx = i; fy = j; goto next2; } next2:; var a2 = goDfs(map, fx, fy); var ans = getAns(a1, a2); Console.WriteLine(ans - 1); } public static int getAns(List

a, List

b) { var ans = int.MaxValue; foreach (var x in a) foreach (var y in b) { var w = Math.Abs(x.x - y.x) + Math.Abs(x.y - y.y); ans = Math.Min(ans, w); } return ans; } public static List

goDfs(int[,] map, int fx, int fy) { var ans = new List

(); var h = map.GetLength(0); var w = map.GetLength(1); var dx = new int[] { 0, 1, 0, -1 }; var dy = new int[] { 1, 0, -1, 0 }; var q = new Queue

(); q.Enqueue(new P { x = fx, y = fy }); while (q.Count() > 0) { var a = q.Dequeue(); if (map[a.x, a.y] != 2) { ans.Add(new P { x = a.x, y = a.y }); map[a.x, a.y] = 2; for (int i = 0; i < 4; i++) { var nx = a.x + dx[i]; var ny = a.y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx, ny] == 1) q.Enqueue(new P { x = nx, y = ny }); } } } return ans; } }