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 Kotlin (1.0.0) (judge verdict: Time Limit Exceeded)

import java.lang.Math
import kotlin.comparisons.*

val MOD: Int = 1000000000 + 7
val MODL: Long = MOD.toLong()

var maze: Array<Array<Boolean>> = arrayOf<Array<Boolean>>()

fun main(args: Array<String>)
{
    val (h, w) = read().split().toInt()
    // val minStep = IntArray(h*w) {-1}
    var outside = arrayOf<Boolean>()
    for (i in 1..w+2) {
        outside += false
    }
    maze += outside // 迷路の外(上側)
    for (i in 0 until h) {
        val s = read().toCharArray()
        var row = arrayOf<Boolean>()
        row += false // 迷路の外(左側)
        for (c in s) {
            row += c == '.'
        }
        row += false // 迷路の外(右側)
        maze += row
    }
    maze += outside // 迷路の外(下側)

    var ans = -1
    for (sy in 1..h) {
        for (sx in 1..w) {
            if (!maze[sy][sx]) continue
            for (gy in 1..h) {
                for (gx in 1..w) {
                    if (!maze[gy][gx]) continue
                    ans = Math.max(ans, move(sy, sx, gy, gx, mutableListOf<Pair<Int, Int>>()))
                }
            }
            // minStep[(i-1) * w + (k-1)] = calc(i, k)
        }
    }
    println(ans)
}

fun move(y: Int, x: Int, gy: Int, gx: Int, path: MutableList<Pair<Int, Int>>): Int {
    path.add(Pair<Int, Int>(y, x))
    if (y == gy && x == gx) {
        val len = path.lastIndex
        path.removeAt(len)
        return len
    }

    var minStep = Int.MAX_VALUE
    if (maze[y-1][x] && !path.contains(Pair<Int, Int>(y-1, x))) {
        minStep = Math.min(minStep, move(y-1, x, gy, gx, path))
    }
    if (maze[y+1][x] && !path.contains(Pair<Int, Int>(y+1, x))) {
        minStep = Math.min(minStep, move(y+1, x, gy, gx, path))
    }
    if (maze[y][x-1] && !path.contains(Pair<Int, Int>(y, x-1))) {
        minStep = Math.min(minStep, move(y, x-1, gy, gx, path))
    }
    if (maze[y][x+1] && !path.contains(Pair<Int, Int>(y, x+1))) {
        minStep = Math.min(minStep, move(y, x+1, gy, gx, path))
    }
    path.removeAt(path.lastIndex)
    return minStep
}

fun read(): String = readLine()!!
fun CharSequence.split(): List<String> = this.split(" ")
fun Iterable<String>.toInt(): List<Int> = this.map{it.toInt()}
fun Iterable<String>.toLong(): List<Long> = this.map{it.toLong()}