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;
class Program{
  static int Height;
  static int Width;
  static void Main(string[] args){
	string[] input1 = Console.ReadLine().Split(' ');
	Height = int.Parse(input1[0]);
	Width = int.Parse(input1[1]);
    int[,] board = new int[Height,Width];
    int[,] dest3 = new int[Height, Width];
    int[,] dest4 = new int[Height, Width];
    for (int h = 0; h < Height; h++) {
      char[] input2 = Console.ReadLine().ToCharArray();
      for (int w = 0; w < Width; w++) {
        if (input2[w] == '.') {
          board[h,w] = -1;
        } else {
          board[h,w] = -2;
        }
      }
    }
    Array.Copy(board, dest3, board.Length);
    Array.Copy(board, dest4, board.Length);
    bool startfound = false;
    int starth = 0;
    int startw = 0;
    while (!startfound) {
      if (board[starth,startw] == -1) {
        startfound = true;
        board[starth,startw] = 0;
      } else {
        if (startw == Width - 1) {
          starth++;
          startw = 0;
        } else {
          startw++;
        }
      }
    }
    
    board = CalcAll(board, starth, startw);
    int max = 0;
    int x = 0;
    int y = 0;
    for (int h = 0; h < Height; h++) {
      for (int w = 0; w < Width; w++) {
          if (board[h,w] > max) {
            max = board[h,w];
            x = h;
            y = w;
          }
        }
      }
    
    dest3[x,y] = 0;
    dest3 = CalcAll(dest3, x, y);
    x = 0;
    y = 0;
    for (int h = 0; h < Height; h++) {
      for (int w = 0; w < Width; w++) {
          if (dest3[h,w] > max) {
            max = dest3[h,w];
            x = h;
            y = w;
          }
        }
      }
    
    dest4[x,y] = 0;
    dest4 = CalcAll(dest4, x, y);
    x = 0;
    y = 0;
    for (int h = 0; h < Height; h++) {
      for (int w = 0; w < Width; w++) {
          if (dest4[h,w] > max) {
            max = dest4[h,w];
            x = h;
            y = w;
          }
        }
      }
    
    Console.WriteLine(max);
  }
  
  static int[,] CalcAll(int[,] board, int starth, int startw) {
    bool res = false;
    board = CalcAround(board, starth, startw, 1);
    int distNow = 1;
    while (!res) {
      res = true;
      
      for (int h = 0; h < Height; h++) {
        for (int w = 0; w < Width; w++) {
          if (board[h,w] == distNow) {
            board = CalcAround(board, h, w, distNow + 1);
          }
        }
      }
      distNow++;
      
      int zeroNum = 0;
      for (int h = 0; h < Height; h++) {
        for (int w = 0; w < Width; w++) {
          if (board[h,w] == -1) {
            zeroNum++;
          }
        }
      }
      if (zeroNum > 0) {
        res = false;
      }
    }
    return board;
  }
  
  static int[,] CalcAround(int[,] board, int h, int w, int dist) {
    if (h > 0) {
        if (board[h - 1, w] == -1 || board[h - 1, w] > dist) {
          board[h - 1, w] = dist;
        }
      }
      if (h < Height - 1) {
        if (board[h + 1, w] == -1 || board[h + 1, w] > dist) {
          board[h + 1, w] = dist;
        }
      }
      if (w > 0) {
        if (board[h, w - 1] == -1 || board[h, w - 1] > dist) {
          board[h, w - 1] = dist;
        }
      }
      if (w < Width - 1) {
        if (board[h, w + 1] == -1 || board[h, w + 1] > dist) {
          board[h, w + 1] = dist;
        }
      }
    return board;
  }
}