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 Lua (5.3.2) (judge verdict: Wrong Answer)

local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
List = {}
function List.new()
    return setmetatable({first = 0, last = -1}, {__index = List})
end

function List.clone(origin)
    local child = List.new()
    for k,v in pairs(origin) do
        child[k] = v
    end
    return child
end

function List:count()
    return self.last - (self.first - 1)
end

function List:pushleft(value)
    local first = self.first - 1
    self.first = first
    self[first] = value
end

function List:popright()
    local last = self.last
    if self.first > last then error("list is empty") end
    local value = self[last]
    self[last] = nil -- to allow garbage collection
    self.last = last - 1
    return value
end
----
local H, W = read.nnl()
local S, L = {}
for i=1,H do
    S[i] = read.l():totable()
end
local function bfs(i1, j1)
    local seen = {}
    for i=1,H do
        seen[i] = {}
    end
    local q = List.new()
    q:pushleft({i1, j1, 0})
    seen[i1][j1] = true
    local dmax = 0
    while q:count() > 0 do
        local i, j, d = table.unpack(q:popright())
        local function checkdo(newi, newj)
            if newi >= 1 and newi <= H then
                if not seen[newi][newj] and S[newi][newj] == '.' then
                    q:pushleft({newi, newj, d+1})
                    dmax = math.max(dmax, d+1)
                    seen[newi][newj] = true
                end
            end
        end
        checkdo(i+1,j)
        checkdo(i-1,j)
        checkdo(i,j+1)
        checkdo(i,j-1)
    end
    return dmax
end
local ans = 0
for i=1,H do
    for j=1,W do
        ans = math.max(ans, bfs(i, j))
    end
end
print(ans)