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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

class Program
{
    static void Main(string[] args)
    {
        var wrong_answer = new wrong_answer();
        wrong_answer.Solve();
    }
}

public class wrong_answer
{
    const int INF = 1 << 30;
    const long INFL = 1L << 60;
    const int MOD = 1000000007;
    const int MOD2 = 998244353;

    myinout IO = new myinout();
    Math2 mmath = new Math2();

    //functions






    public void Solve()
    {
        var o = IO.intm;
        var h = o[0];
        var w = o[1];

        var dist = new int[h, w];

        var dist2 = new int[h, w];
        var s = Enumerable.Range(0, h).Select(_ => IO.charm).ToArray();
        int[] dx = { 1, 0, -1, 0 };
        int[] dy = { 0, 1, 0, -1 };
        int sx = 0;
        int sy = 0;
        bool aaaa = false;
        for (int i = 0; i < h;++i){
            for (int j = 0; j < w;++j){
                if(s[i][j]=='.'){
                    sx = i;sy = j;
                    aaaa = true;
                }
                if(aaaa)break;
            }
            if(aaaa)break;
        }

            var q = new Queue<Tuple<int, int>>();
        for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) dist[i, j] = -1;
        dist[sx, sy] = 0;
        q.Enqueue(new Tuple<int, int>(sx, sy));
        while (q.Count() > 0)
        {
            var xx = q.Dequeue();
            var x = xx.Item1;
            var y = xx.Item2;
            for (int i = 0; i < 4; ++i)
            {
                if (x + dx[i] < 0 || y + dy[i] < 0 || x + dx[i] >= h || y + dy[i] >= w || s[x + dx[i]][y + dy[i]] == '#') continue;
                if (dist[x + dx[i], y + dy[i]] != -1) continue;
                dist[x + dx[i], y + dy[i]] = dist[x, y] + 1;
                q.Enqueue(new Tuple<int, int>(x + dx[i], y + dy[i]));
            }
        }
        var max = 0;
        int maxx = 0;
        int maxy = 0;
        for (int i = 0; i < h; ++i)
        {
            for (int j = 0; j < w; ++j)
            {
                if (max < dist[i, j])
                {
                    max = dist[i, j];
                    maxx = i;
                    maxy = j;
                }
            }
        }


        var q2 = new Queue<Tuple<int, int>>();
        for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) dist2[i, j] = -1;
        dist2[maxx, maxy] = 0;
        q2.Enqueue(new Tuple<int, int>(maxx, maxy));
        while (q2.Count() > 0)
        {
            var xx = q2.Dequeue();
            var x = xx.Item1;
            var y = xx.Item2;
            for (int i = 0; i < 4; ++i)
            {
                if (x + dx[i] < 0 || y + dy[i] < 0 || x + dx[i] >= h || y + dy[i] >= w || s[x + dx[i]][y + dy[i]] == '#') continue;
                if (dist2[x + dx[i], y + dy[i]] != -1) continue;
                dist2[x + dx[i], y + dy[i]] = dist2[x, y] + 1;
                q2.Enqueue(new Tuple<int, int>(x + dx[i], y + dy[i]));
            }
        }
        var max2 = 0;
        int maxx2= 0;
        int maxy2 = 0;
        for (int i = 0; i < h; ++i)
        {
            for (int j = 0; j < w; ++j)
            {
                if (max2 < dist2[i, j])
                {
                    max2 = dist2[i, j];
                    maxx2 = i;
                    maxy2 = j;
                }
            }
        }
        IO.wri(max2);




    }



}





//b-aで降順

public class Math2
{
    public long[] fact;
    public long[] invfact;
    public void factoricalmodset(long N, long MOD = 1000000007)
    {
        fact = new long[N + 1];
        invfact = new long[N + 1];
        fact[0] = 1;

        for (long i = 1; i <= N; ++i)
        {
            fact[i] = fact[i - 1] * i % MOD;
        }
        invfact[N] = pow(fact[N], MOD - 2);

        for (long i = N; i > 0; --i)
        {
            invfact[i - 1] = invfact[i] * i % MOD;
        }

    }

    public long nCr(long n, long r, long MOD = 1000000007)
    {
        return (fact[n] * invfact[r] % MOD * invfact[n - r] % MOD);
    }

    public long pow(long i, long n, long MOD = 1000000007)
    {
        long res = 1;
        while (n > 0)
        {
            if ((n & 1) != 0) res = res * i % MOD;
            i = i * i % MOD;
            n >>= 1;
        }
        return res;
    }

    public long gcd(long i, long y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public long lcm(long i, long y)
    {
        return i * y / gcd(i, y);
    }

    public Dictionary<long, int> primefactorization(long N)
    {
        var ret = new Dictionary<long, int>();

        for (long i = 2; i * i <= N; ++i)
        {
            int cnt = 0;
            while (N % i == 0)
            {
                cnt++;
                N /= i;
            }
            if (cnt != 0) ret[i] = cnt;

        }
        if (N >= 2) ret[N] = 1;

        return ret;

    }

    public List<long> divisorenumrate(long N)
    {
        var ret = new List<long>();

        for (long i = 1; i * i <= N; ++i)
        {

            if (N % i == 0)
            {
                ret.Add(i);
                ret.Add(N / i);
            }

        }

        return ret;

    }

    public void swap<T>(ref T a, ref T b)
    {
        var i = a;
        a = b;
        b = i;
    }

}

class myinout
{
    //in
    public string str => Console.ReadLine();

    public string[] strm => str.Split(' ');

    public long[] longm => strm.Select(long.Parse).ToArray();
    public int[] intm => strm.Select(int.Parse).ToArray();
    public char[] charm => str.ToArray();
    public double[] doublem => strm.Select(double.Parse).ToArray();
    public long long1 => longm[0];
    public int int1 => intm[0];
    public char char1 => charm[0];
    public double double1 => doublem[0];



    //out
    public void wri<T>(T i)
    {
        Console.WriteLine(i);
    }
    public void wri<T>(T[] i)
    {
        Console.WriteLine(string.Join(" ", i));
    }

    public void yn(bool i)
    {
        if (i) wri("Yes");
        else wri("No");
    }

}