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

import java.io.*
import java.util.*

fun solve(H: Int, W: Int, S: Array<String>) {
    val graph = AdjacencyMatrixGraph<Int, Long>(Array(H*W) { Array<Long?>(H*W) { null } })
    fun idx(h: Int, w: Int) = h * W + w
    for (h in 0 until H) {
        for (w in 0 until W) {
            if (S[h][w] == '#') continue
            val nexts = setOf(h + 1 to w, h to w + 1)
                .filter { it.first < H && it.second < W }
            for (next in nexts) {
                if (S[next.first][next.second] == '.') {
                    graph.addEdge(idx(h, w), idx(next.first, next.second), 1L)
                    graph.addEdge(idx(next.first, next.second), idx(h, w), 1L)
                }
            }
        }
    }
    val start = (0 until H * W).find{ S[it / H][it % H] == '.' }?: -1
    if(start == -1){
        println(0)
        return
    }
    val bfs = BreadthFirstSearch(graph, start)
    val ret = bfs.distances.withIndex().maxBy { it.value }!!.index
    val diameter = BreadthFirstSearch(graph, ret).distances.max()!!
    println(diameter)
    return
}

fun main(args: Array<String>) {
    val (H, W) = readLine()!!.split(" ").map { it.toInt() }
    val S = Array(H) { readLine()!! }
    solve(H, W, S)
}

interface Graph<T : Comparable<T>, E : Comparable<E>> {
    val size: Int
    fun getWeight(from: Int, to: Int): E?
    fun edgesFrom(from: Int): List<Edge<E>>
    fun addEdge(from: Int, to: Int, weight: E): Boolean
    fun removeEdge(from: Int, to: Int): Boolean
    fun isAdjacent(from: Int, to: Int): Boolean
}

data class Edge<T : Comparable<T>>(val to: Int, val weight: T)
data class Vertex<T : Comparable<T>, E : Comparable<E>>(val data: T) {
    val edges = mutableListOf<Edge<E>>()

    fun addEdge(to: Int, weight: E) = when (edges.find { it.to == to }) {
        null -> {
            edges.add(Edge(to, weight))
            true
        }
        else -> false
    }

    fun removeEdge(to: Int): Boolean {
        for ((i, edge) in edges.withIndex()) {
            if (edge.to == to) {
                edges.removeAt(i)
                return true
            }
        }
        return false
    }
}

class AdjacentListGraph<T : Comparable<T>, E : Comparable<E>>(
    private val vertices: MutableList<Vertex<T, E>>
) : Graph<T, E> {
    override val size = vertices.size
    override fun getWeight(from: Int, to: Int): E? =
        vertices[from].edges.find { it.to == to }?.weight
    override fun edgesFrom(from: Int): List<Edge<E>> = vertices[from].edges
    override fun addEdge(from: Int, to: Int, weight: E) = vertices[from].addEdge(to, weight)
    override fun removeEdge(from: Int, to: Int) = vertices[from].removeEdge(to)
    override fun isAdjacent(from: Int, to: Int) =
        vertices[from].edges.find { it.to == to } != null
}

class AdjacencyMatrixGraph<T : Comparable<T>, E : Comparable<E>>(
    private val matrix: Array<Array<E?>>
) : Graph<T, E> {
    override val size = matrix.size
    override fun getWeight(from: Int, to: Int): E? = matrix[from][to]
    override fun edgesFrom(from: Int): List<Edge<E>> =
        matrix[from].withIndex()
            .filter { it.value != null }
            .map { Edge(it.index, it.value!!) }

    override fun addEdge(from: Int, to: Int, weight: E): Boolean {
        if (matrix[from][to] == null) {
            matrix[from][to] = weight
            return true
        }
        return false
    }

    override fun removeEdge(from: Int, to: Int): Boolean {
        if (from >= size || to >= size || matrix[from][to] == null) {
            return false
        }
        matrix[from][to] = null
        return true
    }

    override fun isAdjacent(from: Int, to: Int): Boolean = matrix[from][to] != null
}

class BreadthFirstSearch<T : Comparable<T>>(val graph: Graph<T, Long>, val from: Int) {
    /**
     * Constraints:
     *   - weights must be same
     *
     * Complexity:
     *   - time: O(V)
     *   - space: O(V)
     */

    val distances = Array(graph.size) { -1L }
    val beforeNodes = IntArray(graph.size) { -1 }
    init {
        search()
    }

    fun search() {
        val deque = ArrayDeque<Int>()
        distances[from] = 0L
        deque.add(from)
        while (deque.isNotEmpty()) {
            val node = deque.removeFirst()
            graph.edgesFrom(node).filter { distances[it.to] == -1L }.forEach {
                distances[it.to] = distances[node] + it.weight
                beforeNodes[it.to] = node
                deque.add(it.to)
            }
        }
    }

    fun shortestPath(to: Int): List<Int> {
        val path = ArrayDeque<Int>()
        var current = to
        path.add(to)
        while (current != from) {
            current = beforeNodes[current]
            path.addFirst(current)
        }
        return path.toList()
    }
}