package main import ( "bufio" "fmt" "math" "os" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { h := getNextInt(scanner) w := getNextInt(scanner) g := make(grid, h) for i := 0; i < h; i++ { g[i] = getNextString(scanner) } var ans float64 ans = math.MaxFloat64 for i := 0; i < w; i++ { a := g.paint(0, i+1) if ans > a { ans = a } } for i := 0; i < w; i++ { a := g.paint(h+1, i+1) if ans > a { ans = a } } for i := 0; i < h; i++ { a := g.paint(i+1, 0) if ans > a { ans = a } } for i := 0; i < h; i++ { a := g.paint(i+1, w+1) if ans > a { ans = a } } fmt.Fprintln(writer, ans) } type grid []string func (g grid) paint(y, x int) float64 { h := len(g) w := len(g[0]) var ans float64 for i := 0; i < h; i++ { for j := 0; j < w; j++ { if g[i][j] == '0' { continue } ans += math.Sqrt(math.Pow(float64(i+1-y), 2) + math.Pow(float64(j+1-x), 2)) } } return ans }