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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;

public class Main
{

  static boolean[][] z = new boolean[22][22];
  static boolean[][] zz = new boolean[22][22];
  public static void main(String[] args) throws IOException
  {
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in), 1);
    String s;
    String sl[];

    s = r.readLine();
    sl = s.split(" ");
    int h = Integer.parseInt(sl[0]);
    int w = Integer.parseInt(sl[1]);

    for(int i = 0; i < h; i++)
    {
      s = r.readLine();
      for(int j = 0; j < w; j++)
      {
        z[i + 1][j + 1] = (s.charAt(j) == '.');
      }
    }

    int d = 0;
    for(int i = 0; i < 22; i++)
    {
      for(int j = 0; j < 22; j++)
      {
        for(int k = 0; k < 22; k++)
        {
          for(int l = 0; l < 22; l++)
          {
            if(z[i][j] && z[k][l] && i * 22 + j < k * 22 + l)
            {
              d = Math.max(d,  dist(i, j, k, l));
            }
          }
        }
      }
    }
    System.out.println(d);
  }

  public static int dist(int sx, int sy, int gx, int gy)
  {
    for(int i = 0; i < 22; i++)
    {
      for(int j = 0; j < 22; j++)
      {
        //System.out.print(z[i][j]);
        zz[i][j] = z[i][j];
      }
      //System.out.println();
    }
    return dist_w(zz, sx, sy, gx, gy);
  }

  public static int dist_w(boolean[][] b, int sx, int sy, int gx, int gy)
  {
    b[sx][sy] = false;
    int res = 9999;

    ArrayDeque<Integer> qsx = new ArrayDeque<>();
    ArrayDeque<Integer> qsy = new ArrayDeque<>();
    ArrayDeque<Integer> qc = new ArrayDeque<>();
    qsx.add(sx);
    qsy.add(sy);
    qc.add(0);
    int x;
    int y;
    int c;
    while(!qc.isEmpty())
    {
      x = qsx.poll();
      y = qsy.poll();
      c = qc.poll();

      if(x == gx && y == gy)
      {
        return c;
      }

      b[x][y] = false;
      if(b[x - 1][y])
      {
        qsx.add(x - 1);
        qsy.add(y);
        qc.add(c + 1);
      }

      if(b[x + 1][y])
      {
        qsx.add(x + 1);
        qsy.add(y);
        qc.add(c + 1);
      }

      if(b[x][y - 1])
      {
        qsx.add(x);
        qsy.add(y - 1);
        qc.add(c + 1);
      }

      if(b[x][y + 1])
      {
        qsx.add(x);
        qsy.add(y + 1);
        qc.add(c + 1);
      }
    }

    return 0;
  }

}

class RemRing
{
  long module;

  public RemRing(long module)
  {
    this.module = module;
  }

  public long sum(long a, long b)
  {
    return (a + b + module) % module;
  }

  public long sub(long a, long b)
  {
    return (a - b + module) % module;
  }

  public long prod(long a, long b)
  {
    return (a * b) % module;
  }

  public long div(long a, long b)
  {
    return (a * inv(b)) % module;
  }

  public long inv(long a)
  {
    long b = this.module;
    long u = 1;
    long v = 0;
    while (b > 0)
    {
      long t = a / b;
      a -= t * b;
      u -= t * v;
      long z = a;
      a = b;
      b = z;
      z = u;
      u = v;
      v = z;
    }
    u %= this.module;
    if (u < 0)
      u += this.module;
    return u;
  }
}