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

import java.util.Scanner
import scala.collection.immutable.Queue

// '.': true
case class Input(maze: List[List[Boolean]]) {
  val w = maze(0).length
  val h = maze.length
  def bfs(from: (Int, Int), to: (Int, Int)): Int = {
    val mary = maze.map(_.toArray).toArray
    def neighbors(from: (Int, Int)): List[(Int, Int)] = {
      val (x, y) = from
      List((0,1),(0,-1),(1,0),(-1,0)).flatMap(
        { case (dx, dy) =>
          val (nx, ny) = (x+dx, y+dy)
          if (nx >= 0 && nx < h && ny >= 0 && ny < w) Some((nx,ny))
          else None
        }).filter({ case (x, y) => mary(x)(y) })
    }
    def aux(q: Queue[((Int, Int), Int)]): Int = q match {
      case (from, cnt) +: rst => {
        if (from == to) cnt
        else {
          val nb = neighbors(from)
          nb foreach({ case (x, y) => mary(x)(y) = false })
          aux(rst.enqueue(nb.map((_, cnt+1))))
        }
      }
      case _ => -1
    }
    aux(Queue((from, 0)))
  }
  def solved: Output = {
    val withIndex =
      maze.map(_.zipWithIndex).zipWithIndex
        .flatMap({ case (l, i) => l.map({ case (b, j) => (((i, j), b)) }) })
    val dots = withIndex.filter({ case (coord, b) => b }).map(_._1)
    val combs = dots.combinations(2)
    val ans = combs.map({ case List(from, to) => bfs(from, to) }).max
    Output(ans)
  }
}

object Input {
  def fromString(s: String): Input = {
    Input(s.split('\n').map(_.toCharArray().map(_ == '.').toList).toList)
  }
  def fromStdin(): Input = {
    val sc = new Scanner(System.in)
    val h = sc.nextInt()
    val w = sc.nextInt()
    sc.nextLine()
    fromString(List.fill(h)(sc.nextLine()).foldLeft("")({ case (acc, s) => s"${acc}${s}\n" }))
  }
}

case class Output(ans: Int) {
  override def toString(): String = {
    ans.toString()
  }

  def print() = println(this.toString())
}

object Main extends App {
  Input.fromStdin().solved.print()
}