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 Scala (2.11.7) (judge verdict: Wrong Answer)

import scala.collection.immutable.Queue

object Main {

  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)

    val H, W = sc.nextInt(); sc.nextLine()
    val S = Array.fill(H)(sc.nextLine().trim.toCharArray)
    // スタートの決定にO(H*W)
    // ゴールの決定にO(H*W)
    // 最短路の探索にO(H*W)
    // 全探索するとO(H^3*W^3)
    // 64 * 10^6で間に合わない
    // dist(y)(x)(y')(x') := マスy,xからマスy',x'への最短路としておく
    def solve(H: Int, W: Int, S: Array[Array[Char]]): Int = {
      val MAX = Int.MaxValue
      val dist = Array.fill(H, W, H, W)(MAX)
      val dx = Array(0, 0, -1, 1)
      val dy = Array(-1, 1, 0, 0)

      def bfs(sx: Int, sy: Int, ex: Int, ey: Int): Int = {
        val UNREACHED = 401
        val arr = Array.fill(H, W)(MAX)
        arr(sy)(sx) = 0
        def loop(queue: Queue[(Int, Int)], min: Int): Int = {
          if (queue.nonEmpty) {
            val (x, y) = queue.head
            if (x == ex && y == ey) {
              min min arr(ey)(ex)
            } else if (dist(y)(x)(ey)(ex) != MAX) {
              loop(queue.tail, min min (arr(y)(x) + dist(y)(x)(ey)(ex)))
            } else {
              dist(sy)(sx)(y)(x) = dist(sy)(sx)(y)(x) min arr(y)(x)
              dist(y)(x)(sy)(sx) = dist(y)(x)(sy)(sx) min arr(y)(x)
              loop((0 to 3).foldLeft(queue.tail) { (queue, i) =>
                val nx = x + dx(i)
                val ny = y + dy(i)
                if (nx >= 0 && nx < W &&
                  ny >= 0 && ny < H &&
                  S(ny)(nx) == '.' &&
                  arr(ny)(nx) == MAX) {
                  arr(ny)(nx) = arr(y)(x) + 1
                  queue.enqueue((nx, ny))
                } else queue
              }, min)
            }
          } else min
        }
        loop(Queue((sx, sy)), UNREACHED)
      }

      (for {
        sy <- 0 until H
        sx <- 0 until W
        if (S(sy)(sx) == '.')
        ey <- 0 until H
        ex <- 0 until W
        if (S(ey)(ex) == '.')
      } yield bfs(sx, sy, ex, ey)).max
    }

    println(solve(H, W, S))
  }

}