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, map)); this.Min = Math.Min(this.Min, GetCost(h + 1, i, map)); } for (int i = 0; i < h; i++) { this.Min = Math.Min(this.Min, GetCost(i, 0, map)); this.Min = Math.Min(this.Min, GetCost(i, 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 = @" 30 27 101111001101011000001011000 111100011101011110100101010 010001110101011101010100101 000001101000010000110001110 100010011101011111111110110 101100101110001010101011110 001010000010111110011011010 111110011010011100000001110 010101010011001000111001001 100001011100100000100000100 000110011110100111101110010 000000001101010001100010110 111110110110100000010100000 011010001000011100100111001 001011110110010000010001000 000101110001001000110111110 010010110001000100000111100 101101110101101101111100111 100110100011001001111101011 000010010000000000100101011 110001000101101101100000010 011111011101111101011010010 011110011111001110000111110 111111000011000110000010001 110001111011100001110110111 010101000101001011001110101 000101111110110010110000111 111011001101110101101100100 100011000100100011001010100 100010000100110100001111110 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }