using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using sc = Scanner; class Program { static void Main(string[] args) { Solve(); #if DEBUG System.Console.WriteLine("続行するには何かキーを押してください..."); System.Console.ReadKey(); #endif } static int[,] Field; static int W; static int H; static int[] vx = { 1, 0, -1, 0 }; static int[] vy = { 0, 1, 0, -1 }; /// /// ここでとく /// static void Solve() { int w = sc.NextInt(); int h = sc.NextInt(); W = w; H = h; Field = new int[h, w]; int min_displace = int.MaxValue; HashSet> group1 = new HashSet>(); HashSet> group2 = new HashSet>(); for (int i = 0; i < h; i++) { string line = Console.ReadLine(); for (int j = 0; j < w; j++) { Field[i, j] = line[j] == '#' ? 1 : 0; } } // group wakeru for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (Field[i,j] == 0) { dfs(i, j, group1); #if DEBUG Console.WriteLine(1230000); #endif i = h; break; } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (Field[i, j] == 0) { dfs(i, j, group2); break; } } } //man hatten kyori wo zenkeisan foreach (var item in group1) { foreach (var itemm in group2) { min_displace = Math.Min(min_displace, Math.Abs(item.Item1 - itemm.Item1) + Math.Abs(item.Item2 - itemm.Item2) - 1); } } Console.WriteLine(min_displace); #if DEBUG Console.WriteLine("local check"); #endif } static void dfs(int x, int y, HashSet> group) { #if DEBUG Console.WriteLine(x+" "+y); #endif Field[x , y] = 1; group.Add(new Tuple(x , y )); for (int i = 0; i < 4; i++) { if ( x+vx[i] <0 || H <= x+vx[i] ) { continue; } if (y + vy[i] < 0 || W <= y + vy[i]) { continue; } if (Field[x+vx[i],y+vy[i]] == 0) { dfs(x + vx[i], y + vy[i], group); Field[x + vx[i], y + vy[i]] = 1; } } } } /// /// Java風のスキャナー,自作(某最速の人を参考) /// public static class Scanner { public static string NextString() { string tmp = ""; while (true) { string readData = char.ConvertFromUtf32(Console.Read()); if (readData == " " || readData == "\n") break; tmp += readData; } return tmp; } public static string[] NextStrArray() { return Console.ReadLine().Split(' '); } public static long[] NextLongArray() { string[] s = NextStrArray(); long[] a = new long[s.Length]; for (int i = 0; i < a.Length; i++) { a[i] = long.Parse(s[i]); } return a; } public static int[] NextIntArray() { string[] s = NextStrArray(); int[] a = new int[s.Length]; for (int i = 0; i < a.Length; i++) { a[i] = int.Parse(s[i]); } return a; } public static int NextInt() { string tmp = ""; while (true) { string readData = char.ConvertFromUtf32(Console.Read()); if (readData == " " || readData == "\n") break; tmp += readData; } return int.Parse(tmp); } public static double NextDouble() { string tmp = ""; while (true) { string readData = char.ConvertFromUtf32(Console.Read()); if (readData == " " || readData == "\n") break; tmp += readData; } return double.Parse(tmp); } public static long NextLong() { string tmp = ""; while (true) { string readData = char.ConvertFromUtf32(Console.Read()); if (readData == " " || readData == "\n") break; tmp += readData; } return long.Parse(tmp); } public static double[] NextDoubleArray() { string[] s = NextStrArray(); double[] a = new double[s.Length]; for (int i = 0; i < a.Length; i++) { a[i] = double.Parse(s[i]); } return a; } }