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: Runtime Error)

package main

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

func StrStdin() string {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	return strings.TrimSpace(scanner.Text())
}

func Int64Stdin() int64 {
	stringInput := StrStdin()
	num, _ := strconv.ParseInt(strings.TrimSpace(stringInput), 10, 64)
	return num
}

func SplitWithoutEmpty(stringTargeted string, delim string) []string {
	stringSplited := strings.Split(stringTargeted, delim)
	strs := []string{}

	for _, str := range stringSplited {
		if str != "" {
			strs = append(strs, str)
		}
	}
	return strs
}

func SliceStrStdin(delim string) []string {
	stringInput := StrStdin()
	return SplitWithoutEmpty(stringInput, delim)
}

func SliceInt64Stdin() []int64 {
	stringSplited := SliceStrStdin(" ")

	uint64Slice := []int64{}

	for ni := range stringSplited {
		num, _ := strconv.ParseInt(stringSplited[ni], 10, 64)
		uint64Slice = append(uint64Slice, num)
	}

	return uint64Slice
}

type Maze struct {
	maze []string
}

var (
	H, W int64
	m    Maze
	dist [30][30]int64
	dy   = [4]int64{0, 1, 0, -1}
	dx   = [4]int64{1, 0, -1, 0}
)

type Point struct {
	y int64
	x int64
}

func BFS(y, x int64) int64 {

	que := []Point{}
	que = append(que, Point{y: y, x: x})
	var ans int64
	ans = 0

	for hy := 0; int64(hy) < H; hy++ {
		for wx := 0; int64(wx) < W; wx++ {
			dist[hy][wx] = 0
		}
	}

	for len(que) > 0 {

		q := que[0]
		que = que[1:]
		for dir := 0; dir < len(dy); dir++ {
			ny := q.y + dy[dir]
			nx := q.x + dx[dir]

			if (0 <= ny && int64(ny) < H) && (0 <= nx && int64(nx) < W) {
				if m.maze[ny][nx] != '.' {
					continue
				}
				if dist[ny][nx] > 0 {
					continue
				}
				que = append(que, Point{y: ny, x: nx})
				dist[ny][nx] = dist[q.y][q.x] + 1
				ans = int64(math.Max(float64(ans), float64(dist[ny][nx])))
			}
		}
	}

	return ans
}

func Solve() {
	HW := SliceInt64Stdin()
	H, W = HW[0], HW[1]

	for hy := 0; int64(hy) < H; hy++ {
		m.maze = append(m.maze, StrStdin())
	}

	var res int64
	res = 0
	for hy := 0; int64(hy) < H; hy++ {
		for wx := 0; int64(wx) < W; wx++ {
			if m.maze[hy][wx] == '.' {
				res = int64(math.Max(float64(res), float64(BFS(int64(hy), int64(wx)))))
			}
		}
	}

	fmt.Printf("%d", res)
}

func main() {
	Solve()
	return
}