Problem:
Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.

Solution in Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const (
	initialBufSize = 1024
	maxBufSize     = 1e6
)

var (
	scanner = bufio.NewScanner(os.Stdin)
	// writer  = bufio.NewWriterSize(os.Stdout, 1000000)
)

const (
	wall = "#"
	road = "."
)

var (
	di = []int{-1, 0, 1, 0}
	dj = []int{0, -1, 0, 1}
)

type point struct {
	x, y int
}

// 解説見た
func main() {
	scanner.Split(bufio.ScanWords)
	// scanner.Buffer(make([]byte, initialBufSize), maxBufSize)
	// defer writer.Flush()
	h, w := scanInt(), scanInt()
	maze := make([][]string, h)
	for i := 0; i < h; i++ {
		maze[i] = make([]string, w)
		s := scanString()
		for j := 0; j < w; j++ {
			maze[i][j] = string(s[j])
		}
	}

	createDist := func() [][]int {
		d := make([][]int, h)
		for i := 0; i < h; i++ {
			d[i] = make([]int, w)
			for j := 0; j < w; j++ {
				d[i][j] = math.MaxInt64
			}
		}
		return d
	}

	q := CreateQueue(1000000)

	ans := 0
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if maze[i][j] == wall {
				continue
			}
			d := createDist()

			upd := update(d, q)
			upd(i, j, 0)
			for !q.Empty() {
				qt, _ := q.Deq()
				qq := qt.(*point)
				x, y := qq.x, qq.y

				for dir := 0; dir < 4; dir++ {
					ni, nj := y+di[dir], x+dj[dir]
					if ni < 0 || ni >= h || nj < 0 || nj >= w {
						continue
					}
					if maze[ni][nj] == wall {
						continue
					}
					upd(ni, nj, d[y][x]+1)
				}
			}

			for ii := 0; ii < h; ii++ {
				for jj := 0; jj < w; jj++ {
					if d[ii][jj] == math.MaxInt64 {
						continue
					}
					ans = max(ans, d[ii][jj])
				}
			}
		}
	}

	fmt.Println(ans)
}

func update(d [][]int, q *Queue) func(i, j, x int) {

	return func(i, j, x int) {
		if d[i][j] != math.MaxInt64 {
			return
		}
		d[i][j] = x
		q.Enq(&point{x: j, y: i})
	}
}

func scanInt() int {
	i, _ := strconv.Atoi(scanString())
	return i
}

func scanInts(size int) []int {
	ints := make([]int, size)
	for i := 0; i < size; i++ {
		ints[i] = scanInt()
	}
	return ints
}

func scanFloat64() float64 {
	f, _ := strconv.ParseFloat(scanString(), 64)
	return f
}

func scanFloat64s(size int) []float64 {
	f := make([]float64, size)
	for i := 0; i < size; i++ {
		f[i] = scanFloat64()
	}
	return f
}

func scanString() string {
	scanner.Scan()
	return scanner.Text()
}

func scanStrings(size int) []string {
	s := make([]string, size)
	for i := 0; i < size; i++ {
		s[i] = scanString()
	}
	return s
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

type Queue struct {
	ele []interface{}
}

func CreateQueue(cap int) *Queue {
	return &Queue{
		ele: make([]interface{}, 0, cap),
	}
}

func (q *Queue) Empty() bool {
	return len(q.ele) == 0
}

func (q *Queue) Enq(e interface{}) {
	q.ele = append(q.ele, e)
}

func (q *Queue) Deq() (interface{}, bool) {
	if q.Empty() {
		return nil, false
	}
	r := q.ele[0]
	q.ele = q.ele[1:]
	return r, true
}