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;
using System.Linq;
using System.Text.RegularExpressions;
using static kyoupuro.Input;
using static kyoupuro.MyMath;

namespace kyoupuro {

    class MainClass {

        public static void Main() {
            int H = NextInt, W = NextInt;
            var data = new List<string>();
            var d = new int[H * W, H * W];

            for (int i = 0; i < H * W; i++) {
                for (int j = 0; j < H * W; j++) {
                    d[i, j] = 100000000;
                }
            }

            for (int i = 0; i < H; i++) {
                data.Add(Line);
                for (int j = 0; j < W; j++) {
                    if (data[i][j] == '.') d[i * W + j, i * W + j] = 0;
                }
            }

            for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                    int pos = i * W + j;

                    if (data[i][j] == '#') continue;

                    if (j + 1 < W && data[i][j + 1] == '.') d[pos, pos + 1] = 1;
                    if (i + 1 < H && data[i + 1][j] == '.') d[pos, pos + W] = 1;
                    if (i > 0 && data[i - 1][j] == '.') d[pos, pos - W] = 1;
                    if (j > 0 && data[i][j - 1] == '.') d[pos, pos - 1] = 1;
                }
            }

            for (int k = 0; k < H * W; k++) {
                for (int i = 0; i < H * W; i++) {
                    for (int j = 0; j < H * W; j++) {
                        d[i, j] = Math.Min(d[i, j], d[i, k] + d[k, j]);
                    }
                }
            }

            var max = 0;
            for (int i = 0; i < H * W; i++) {
                for (int j = 0; j < H * W; j++) {
                    if (d[i, j] < 10000 && max < d[i, j]) {
                        max = d[i, j];
                    }
                }
            }

            /*
            for (int i = 0; i < H * W; i++) {
                for (int j = 0; j < W * H; j++) {
                    Console.Write(d[i, j] + " ");
                }
                Console.WriteLine();
            }*/

            Console.WriteLine(max);
        }
    }

    class MyMath {
        public const long mod = 1000000007;

        public static long Gcd(long a, long b) {
            return b == 0 ? a : Gcd(b, a % b);
        }

        public static long Sqrt(long n) {
            if (n < 0) return 0;
            long a = 0, tmp = 0, b = 0;
            for (int i = 62; i >= 0; i -= 2) {
                tmp = (b << 1) + 1 <= (n >> i) ? 1 : 0;
                a = a << 1 | tmp;
                n -= (((b << 1) + 1) * tmp) << i;
                b = (b << 1) + tmp + tmp;
            }
            return a;
        }

        public static long Factorial(int n) {
            if (n == 0)
                return 1L;
            return n * Factorial(n - 1);
        }
    }

    public class PriorityQueue<T> {

        readonly List<T> _heap = new List<T>();
        readonly Comparison<T> _comparison;

        public PriorityQueue(Comparison<T> comparison) {
            _comparison = comparison;
        }

        public PriorityQueue() : this(Comparer<T>.Default.Compare) { }

        public PriorityQueue(IComparer<T> comparer) : this(comparer.Compare) { }

        public void Enqueue(T item) {
            _heap.Add(item);

            int childID = _heap.Count - 1;
            int parentID = (childID - 1) / 2;

            while (childID > 0 && _comparison(_heap[parentID], _heap[childID]) > 0) {
                Swap(parentID, childID);
                childID = parentID;
                parentID = (parentID - 1) / 2;
            }
        }

        public T Dequeue() {
            var first = _heap.First();
            _heap[0] = _heap.Last();
            _heap.RemoveAt(_heap.Count - 1);

            int parentID = 0;
            int childID = parentID * 2 + 2;
            if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--;

            while (childID < _heap.Count && _comparison(_heap[parentID], _heap[childID]) > 0) {
                Swap(parentID, childID);
                parentID = childID;
                childID = parentID * 2 + 2;
                if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--;
            }

            return first;
        }

        void Swap(int parent, int child) {
            var tmp = _heap[parent];
            _heap[parent] = _heap[child];
            _heap[child] = tmp;
        }

        public T Peek() { return _heap[0]; }
        public int Count => _heap.Count;
        public bool Any() { return _heap.Any(); }
        public List<T> Data => _heap;
    }

    public class UnionFind {
        public int[] Parents { get; set; }
        public UnionFind(int size) {
            Parents = Enumerable.Repeat(-1, size).ToArray();
        }

        public int Find(int num) {
            if (Parents[num] < 0) return num;

            Parents[num] = Find(Parents[num]);
            return Parents[num];
        }

        public int Size(int num) {
            return -Parents[Find(num)];
        }

        public bool Same(int a, int b) {
            return Find(a) == Find(b);
        }

        public void Unite(int a, int b) {
            int x = Find(a), y = Find(b);

            if (Size(x) > Size(y)) {
                Parents[x] += Parents[y];
                Parents[y] = x;
            } else {
                Parents[y] += Parents[x];
                Parents[x] = y;
            }
        }
    }

    class Input {
        static IEnumerator<string> enumerator = new string[] { }.AsEnumerable().GetEnumerator();

        public static string Line => Console.ReadLine();

        public static string[] StrArr => Line.Split(' ');

        public static int NextInt => int.Parse(NextWord());

        public static long NextLong => long.Parse(NextWord());

        public static List<int> IntList => StrArr.Select(int.Parse).ToList();

        public static List<long> LongList => StrArr.Select(long.Parse).ToList();

        public static string NextWord() {
            while (!enumerator.MoveNext()) {
                enumerator = StrArr.AsEnumerable().GetEnumerator();
            }
            return enumerator.Current;
        }
    }
}