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 LuaJIT (2.0.4) (judge verdict: Accepted)

--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("*n","*n","*l")
local maze={}
for i=1,h do
    local s=io.read()
    maze[i]={}
    for j=1,w do
        maze[i][j]=s:sub(j,j)
    end
end

local dx={1,0,-1,0}
local dy={0,1,0,-1}

local function bfs(x,y)
    local dist={}
    for i=1,h do
        dist[i]={}
        for j=1,w do
            dist[i][j]=-1
        end
    end
    local que=List.new()
    
    local result=0
    List.pushright(que,{x,y})
    dist[x][y]=0
    while true do
        local q=List.popleft(que)
        if not q then
            break
        end
        x,y=q[1],q[2]
        result=math.max(result,dist[x][y])
        for i=1,4 do
            local nx,ny=x+dx[i],y+dy[i]
            local checker=(nx<1 or nx>h or ny<1 or ny>w)
            if not checker and maze[nx][ny]~="#" and dist[nx][ny]==-1 then
                dist[nx][ny]=dist[x][y]+1
                List.pushright(que,{nx,ny})
            end
        end
    end

    return result
end

local result=0
for i=1,h do
    for j=1,w do
        if maze[i][j]~="#" then
            result=math.max(result,bfs(i,j))
        end
    end
end
print(result)