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

var sc = bufio.NewScanner(os.Stdin)

var m [][]byte
var h, w int

func main() {
	sc.Split(bufio.ScanWords)
	log.SetFlags(log.Lshortfile)
	h = nextInt()
	w = nextInt()
	m = make([][]byte, h)
	for i := 0; i < h; i++ {
		s := nextString()
		m[i] = []byte(s)
	}
	max_distance := -1
	for x1 := 0; x1 < w; x1++ {
		for y1 := 0; y1 < h; y1++ {
			for x2 := 0; x2 < w; x2++ {
				for y2 := 0; y2 < h; y2++ {
					max_distance = max(max_distance, distance(x1, y1, x2, y2))
				}
			}
		}
	}
	fmt.Println(max_distance)
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}

func distance(x1, y1, x2, y2 int) int {
	var dist [21][21]int
	q := make([]pair, 0)
	q = append(q, pair{x1, y1})
	dist[y1][x1] = 1
	for len(q) > 0 {
		now := q[0]
		q = q[1:]
		for d := 0; d < 4; d++ {
			nx := now.a + dx[d]
			ny := now.b + dy[d]
			if nx >= 0 && nx < w && ny >= 0 && ny < h {
				if m[ny][nx] == '.' {
					if dist[ny][nx] == 0 || dist[ny][nx] > dist[now.b][now.a]+1 {
						q = append(q, pair{nx, ny})
						dist[ny][nx] = dist[now.b][now.a] + 1
					}
				}
			}
		}
	}
	return dist[y2][x2] - 1
}

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

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

var MAX = math.MaxInt32

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type pair struct {
	a, b int
}

type pairs []pair

func (p pairs) Len() int {
	return len(p)
}
func (p pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}