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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
class Program {

    static Graph G;
    static int H;
    static int W;
    static int N;

    public struct Graph {
        public HashSet<int>[] points;

        public Graph (int n) {
            points = new HashSet<int>[n];
            for (int i = 0; i < n; i++) {
                points[i] = new HashSet<int> ();
            }
        }

        public void AddLine (int a, int b) {
            points[a].Add (b);
            points[b].Add (a);
        }

        public void AddDirectionalLine (int a, int b) {
            points[a].Add (b);
        }
    }

    static void Main (string[] args) {
        string[] input = Console.ReadLine ().Split (' ');
        H = int.Parse (input[0]);
        W = int.Parse (input[1]);
        N = H * W;
        G = new Graph (N);
        bool[, ] board = new bool[H, W];

        for (int h = 0; h < H; h++) {
            char[] input2 = Console.ReadLine ().ToCharArray ();
            for (int w = 0; w < W; w++) {
                if (input2[w] == '.') {
                    board[h, w] = true;
                } else {
                    board[h, w] = false;
                }
            }
        }

        G = BoardToGraph (board, G);

        int max = 0;

        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (board[h, w]) {
                    int t = BFS (G, h * W + w);
                    if (t > max) {
                        max = t;
                    }
                }
            }
        }

        Console.WriteLine (max);

    }

    static int BFS (Graph graph, int startNode) {
        int[] dist = new int[N];
        Queue<int> que = new Queue<int> ();
        for (int i = 0; i < N; i++) {
            dist[i] = -1;
        }
        dist[startNode] = 0;
        que.Enqueue (startNode);

        while (que.Count > 0) {
            int nowPoint = que.Dequeue ();
            foreach (int nextPoint in graph.points[nowPoint]) {
                if (dist[nextPoint] != -1) continue;

                dist[nextPoint] = dist[nowPoint] + 1;
                que.Enqueue (nextPoint);
            }
        }

        int max = 0;

        for (int i = 0; i < N; i++) {
            if (dist[i] > max) {
                max = dist[i];
            }
        }
        return max;
    }

    static Graph BoardToGraph (bool[, ] board, Graph graph) {
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W - 1; w++) {
                if (board[h, w] && board[h, w + 1]) {
                    graph.AddLine (h * W + w, h * W + w + 1);
                }
            }
        }
        for (int h = 0; h < H - 1; h++) {
            for (int w = 0; w < W; w++) {
                if (board[h, w] && board[h + 1, w]) {
                    graph.AddLine (h * W + w, h * W + w + W);
                }
            }
        }
        return graph;
    }
}