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"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 10000000
)

var (
	sc = bufio.NewScanner(os.Stdin)
	wt = bufio.NewWriter(os.Stdout)
)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(next())
	return i
}

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}

func min(nums ...int) int {
	ret := nums[0]
	for _, v := range nums {
		if v < ret {
			ret = v
		}
	}
	return ret
}

func max(nums ...int) int {
	ret := nums[0]
	for _, v := range nums {
		if v > ret {
			ret = v
		}
	}
	return ret
}

func bfs(s []string, cost [][]int, h, w, x, y int) {
	ways := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	for _, way := range ways {
		nextx, nexty := x+way[0], y+way[1]
		// 進める場合
		if (0 <= nextx && nextx < w && 0 <= nexty && nexty < h) && s[y][x] == '.' {
			if cost[y][x]+1 < cost[nexty][nextx] {
				cost[nexty][nextx] = cost[y][x] + 1
				bfs(s, cost, h, w, nextx, nexty)
			}
		}
	}
}

func fillSlice(s [][]int, n int) {
	for i := range s {
		for j := range s[i] {
			s[i][j] = n
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, initialBufSize), maxBufSize)
	defer wt.Flush()

	h, w := nextInt(), nextInt()
	s := make([]string, h)
	for i := range s {
		s[i] = next()
	}

	ans := 0
	cost := make([][]int, h)
	for i := range cost {
		cost[i] = make([]int, w)
	}

	for i := 0; i < h*w; i++ {
		fillSlice(cost, h*w)
		cost[i/w][i%w] = 0
		bfs(s, cost, h, w, i%w, i/w)
		for j := 0; j < h*w; j++ {
			if i == j || s[i/w][i%w] == '#' || s[j/w][j%w] == '#' {
				continue
			}
			ans = max(ans, cost[j/w][j%w])
		}
	}

	puts(ans)
}