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

object Main {
    val mod: Long = (1e9 + 7).toLong
    val fct: Array[Int] = new Array[Int]( (1e5 + 5).toInt )
    val ifct: Array[Int] = new Array[Int]( (1e5 + 5).toInt )

    def power(x: Long, y: Long):Long = {
        if(y <= 0)  return 1
        var ans: Long = power( (x * x) % mod, y >> 1 )
        if( (y & 1) > 0 ) ans = (ans * x) % mod
        return ans
    }

    def factorials(n: Int) = {
        fct(0) = 1
        for(i <- 1 to n) fct(i) = ((1L * i * fct(i-1)) % mod).toInt
        ifct(n) = power(fct(n), mod - 2).toInt
        for(i <- (n-1) to 0 by -1) ifct(i) = ( (1L * (i+1) * ifct(i+1)) % mod ).toInt
    }

    def cmb(n: Int, k: Int): Int = {
        if(n < 0 || k < 0 || n < k) return 0
        var ans: Long = (1L * fct(n) * ifct(k)) % mod
        ans = (ans * ifct(n-k)) % mod
        return ans.toInt
    }

    def main(args: Array[String]) = {
        import scala.io.StdIn.readLine
        var Array(n, k) = readLine.split(" ").map(_.toInt)
        var a: Array[Int] = readLine.split(" ").map(_.toInt)
        factorials(n)

        a = a.sorted
        var ans: Int = 0
        for(i <- 0 until n) {
            val maxes = cmb(i, k - 1)
            val mins = cmb(n - i - 1, k - 1)
            val add = ((maxes - mins + mod) % mod).toInt
            ans = ans + ((1L * add * a(i)) % mod).toInt
            if(ans >= mod)  ans = ans - mod.toInt
        }
        printf("%d\n", ans)
    }
}