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: Wrong Answer)

package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

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

func nextStringLine(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = nextString()
	}
	return s
}

type Pair struct {
	i int
	j int
}

const INF = 100100100

func main() {
	sc.Split(bufio.ScanWords)
	H, W := nextInt(), nextInt()

	S := make([][]string, H)
	for i := 0; i < H; i++ {
		S[i] = make([]string, W)
		str := nextString()
		for j := 0; j < W; j++ {
			S[i][j] = string(str[j])
		}
	}

	DI := [4]int{-1, 0, 1, 0}
	DJ := [4]int{0, -1, 0, 1}

	distMax := 0

	for si := 0; si < H; si++ {
		for sj := 0; sj < W; sj++ {
			// init queue
			queue := []Pair{{si, sj}}

			// init dist
			dist := make([][]int, H)
			for i := 0; i < H; i++ {
				dist[i] = make([]int, W)
				for j := 0; j < W; j++ {
					dist[i][j] = INF
				}
			}
			dist[si][sj] = 0

			for len(queue) > 0 {
				p := queue[0]
				queue = queue[1:]

				for k := 0; k < 4; k++ {
					di := p.i + DI[k]
					dj := p.j + DJ[k]
					if di < 0 || dj < 0 || di > H-1 || dj > W-1 {
						continue
					}
					if S[di][dj] == "#" {
						continue
					}
					if dist[di][dj] == INF {
						queue = append(queue, Pair{di, dj})
						dist[di][dj] = dist[p.i][p.j] + 1
					}
				}
			}
			max := getMax(dist, H, W)
			if max > distMax {
				distMax = max
			}
		}
	}
	fmt.Println(distMax)
}

func getMax(list [][]int, h, w int) int {
	max := 0
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if max < list[i][j] && list[i][j] != INF {
				max = list[i][j]
			}
		}
	}
	return max
}