using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int w, h; mul(out w, out h); var c = new char[w, h]; for(int i=0;i< h; i++) { var s = rs(); for (int j = 0; j < w; j++) c[j, i] = s[j]; } int num = 1; var hole = new int[w, h]; Action dfs = null; dfs = (a, b) => { c[b, a] = '#'; hole[b, a] = num; int[] dx = { 0, 0, 1, -1 }, dy = { 1, -1, 0, 0 }; for(int i = 0; i < 4; i++) { int nx = a + dx[i], ny = b + dy[i]; if (c[ny, nx] == '.') { hole[ny, nx] = num; dfs(nx, ny); } } }; for(int i=0;i< h; i++) { for(int j=0;j< w; j++) { if (c[j, i] == '.') { dfs(i, j); num++; } } } int ans = int.MaxValue; for(int i = 0; i < h; i++) for(int j=0;j< w; j++) for (int k = 0; k < h; k++) for(int l=0;l< w; l++) if (hole[j, i] == 1 && hole[l, k] == 2) ans = Math.Min(ans, Math.Abs(i - k) + Math.Abs(j - l) - 1); Console.WriteLine(ans); } #region Scan static int ri() { return int.Parse(Console.ReadLine().Trim()); } static long rl() { return long.Parse(Console.ReadLine().Trim()); } static double rd() { return double.Parse(Console.ReadLine().Trim()); } static string rs() { return Console.ReadLine().Trim(); } static int[] ria() { return Console.ReadLine().Trim().Split().Select(int.Parse).ToArray(); } static long[] rla() { return Console.ReadLine().Trim().Split().Select(long.Parse).ToArray(); } static double[] rda() { return Console.ReadLine().Trim().Split().Select(double.Parse).ToArray(); } static string[] rsa() { return Console.ReadLine().Trim().Split(); } static void mul(out int a, out int b) { var arr = ria(); a = arr[0]; b = arr[1]; } static void mul(out int a, out int b, out int c) { var arr = ria(); a = arr[0]; b = arr[1]; c = arr[2]; } static void mul(out int a, out int b, out int c, out int d) { var arr = ria(); a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3]; } static void mul(out long a, out long b) { var arr = rla(); a = arr[0]; b = arr[1]; } static void mul(out long a, out long b, out long c) { var arr = rla(); a = arr[0]; b = arr[1]; c = arr[2]; } static void mul(out long a, out long b, out long c, out long d) { var arr = rla(); a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3]; } #endregion #region Math const int mod = 1000000007; static bool isprime(long n){ if (n < 2) return false; for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } static long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); } static long lcm(long a, long b) { return a * b / gcd(a, b); } #endregion }