using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); int h = inpt[0]; int w = inpt[1]; bool[,] map = new bool[h, w]; for (int i = 0; i < h; i++) { bool[] row = Reader.ReadLine().Select(a => a == '1').ToArray(); for (int j = 0; j < row.Length; j++) { map[i, j] = row[j]; } } for (int i = 0; i < w; i++) { this.Min = Math.Min(this.Min, GetCost(0, i + 1, map)); this.Min = Math.Min(this.Min, GetCost(h + 1, i + 1, map)); } for (int i = 0; i < h; i++) { this.Min = Math.Min(this.Min, GetCost(i + 1, 0, map)); this.Min = Math.Min(this.Min, GetCost(i + 1, w + 1, map)); } Console.WriteLine(this.Min); } private double Min = double.MaxValue; private double GetCost(int h, int w, bool[,] map) { double ret = 0; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { if(!map[i,j]) { continue; } ret += Math.Sqrt(Math.Pow(Math.Abs(h - (i + 1)), 2) + Math.Pow(Math.Abs(w - (j + 1)), 2)); if(ret > Min) { break; } } if(ret > this.Min) { break; } } return ret; } public class Reader { static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 1 1 1 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }