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

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt() int {
  n, _ := strconv.Atoi(NextLine())
  return n
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func Min(x, y int) int {
  if x < y { return x }
  return y
}
func Max(x, y int) int {
  if x < y { return y }
  return x
}

func main() {
  HW := NextIntVec()
  H, W := HW[0], HW[1]
  M := make([][]int, H)
  for i := range M {
    M[i] = make([]int, W)
    S := NextLine()
    for j, s := range S {
      if s == '.' {
        M[i][j] = 1
      }
    }
  }
  L := H * W + 1
  G := make([][]int, H * W)
  for i := range G {
    G[i] = make([]int, H * W)
    for j := range G[i] {
      G[i][j] = L
    }
    h, w := i / W, i % W
    if M[h][w] < 1 { continue }
    G[i][i] = 0
    if 0 < h && 0 < M[h - 1][w] { G[i][i - W] = 1 }
    if h < H - 1 && 0 < M[h + 1][w] { G[i][i + W] = 1 }
    if 0 < w && 0 < M[h][w - 1] { G[i][i - 1] = 1 }
    if w < W - 1 && 0 < M[h][w + 1] { G[i][i + 1] = 1 }
  }
  N := H * W
  for k := 0; k < N; k++ {
    for i := 0; i < N; i++ {
      for j := 0; j < N; j++ {
        G[i][j] = Min(G[i][k] + G[k][j], G[i][j])
      }
    }
  }
  D := make([]int, N)
  for i, g := range G {
    for _, d := range g {
      if d < L { D[i] = Max(d, D[i]) }
    }
  }
  ans := 0
  for _, d := range D {
    if ans < d { ans = d }
  }
  Write(ans)
  Output()
}