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: Time Limit Exceeded)

package main

import "fmt"

type point struct {
	name int
	pos_h int
	pos_w int
	right int
	left int
	up int
	down int
	deep int
	checked bool
}

func pop(p []point) (point, []point) {
	//fmt.Println("pop! name is ", p[0].name)
	p[0].checked = true
	pp := p[0]
	p = p[1:]
	//fmt.Println("Remains : ", p)
	if len(p) == 0 {
		//fmt.Println("Empty!")
		//var k = []point
		//return pp, k
	}
	return pp, p
}

func push(data point, p []point) []point {
	//fmt.Println("push! name is ", data.name)
	p = append(p, data)
	return p
}

func reset(p []point) []point {
	for i := 0 ; i < len(p) ; i++ {
		p[i].deep = 0
		p[i].checked = false
	}
	return p
}

/*func printQueue(p []point){
	for i := 0; i < len(p); i++ {
		fmt.Println("Name : ",p[i].name)
		fmt.Println("Height : ",p[i].pos_h)
		fmt.Println("Width : ",p[i].pos_w)
		fmt.Println("Right : ",p[i].right)
		fmt.Println("Left : ",p[i].left)
		fmt.Println("Up : ",p[i].up)
		fmt.Println("Down : ",p[i].down)
		fmt.Println("Deep : ",p[i].deep)
		fmt.Println("")
	}
}*/

func address(p []point, h int, w int) point {
	var re point
	for i := 0 ; i < len(p) ; i++ {
		if p[i].pos_h == h && p[i].pos_w == w {
			re = p[i]
		}
	}
	return re
}

func searchName(n int, p []point) point {
	var c point
	for i := 0 ; i < len(p) ; i++ {
		if p[i].name == n {
			c = p[i]
			break
		}
	}
	return c
}


func main(){
	//var queue []point
	var h,w int
	fmt.Scan(&h,&w)
	route := make([][]bool, h)
	for i := 0 ; i < h ; i++ {
		route[i] = make([]bool, w)
	}
	var d string
	//fmt.Println(route)
	count := 0
	for i := 0 ; i < h ; i++ {
		fmt.Scan(&d)
		for j := 0 ; j < w ; j++ {
			if d[j:j+1] == "." {
				route[i][j] = true
				count++
			}
		}
	}
	//fmt.Println(route)
	data := make([]point,count)
	count = 0
	for i := 0 ; i < h ; i++ {
		for j := 0 ; j < w ; j++ {
			if route[i][j] {
				data[count].name = count+1
				data[count].pos_h = i
				data[count].pos_w = j
				count++
			}
		}
	}
	for i := 0 ; i < count ; i++{
		if data[i].pos_h != 0 && route[data[i].pos_h-1][data[i].pos_w] {
			temp := address(data,data[i].pos_h-1,data[i].pos_w)
			data[i].up = temp.name
		}
		if data[i].pos_h != h-1 && route[data[i].pos_h+1][data[i].pos_w] {
			temp := address(data,data[i].pos_h+1,data[i].pos_w)
			data[i].down = temp.name
		}
		if data[i].pos_w != 0 && route[data[i].pos_h][data[i].pos_w-1] {
			temp := address(data,data[i].pos_h,data[i].pos_w-1)
			data[i].left = temp.name
		}
		if data[i].pos_w != w-1 && route[data[i].pos_h][data[i].pos_w+1] {
			temp := address(data,data[i].pos_h,data[i].pos_w+1)
			data[i].right = temp.name
		}
	}
	//printQueue(data)
	max := 0
	var root point
	for i := 0 ; i < count-1 ; i++ {
		for j := i+1 ; j < count ; j++ {
			//fmt.Println("Search now : ",i,j)
			reset(data)
			var queue []point
			//data[i] -> data[j] distance
			//BFS
			queue = push(data[i] , queue)
			for ;; {
				//printQueue(queue)
				//fmt.Println("Search now : ",i,j)
				root,queue = pop(queue)
				//fmt.Println("root name is ", root.name)
				if root.name == data[j].name {
					//fmt.Println("Distance is ",root.deep)
					if max < root.deep {
						max = root.deep
					}
					break
				}
				if root.up != 0 {
					//checked?
					leaf := searchName(root.up,data)
					if !leaf.checked {
						leaf.deep = root.deep + 1
						queue = push(leaf , queue)
					}
				}
				if root.left != 0 {
					leaf := searchName(root.left,data)
					if !leaf.checked {
						leaf.deep = root.deep + 1
						queue = push(leaf , queue)
					}
				}
				if root.down != 0 {
					leaf := searchName(root.down,data)
					if !leaf.checked {
						leaf.deep = root.deep + 1
						queue = push(leaf , queue)
					}
				}
				if root.right != 0{
					leaf := searchName(root.right,data)
					if !leaf.checked {
						leaf.deep = root.deep + 1
						queue = push(leaf , queue)
					}
				}
			}
		}
	}
	fmt.Println(max)
}