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: Accepted)

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {
  static class Pair {
    int left;
    int right;
    int depth;

    Pair(int l, int r, int depth) {
      left = l;
      right = r;
      this.depth = depth;
    }
  }

  public static void main(String[] args) {
    Scanner std = new Scanner(System.in);
    int h = std.nextInt();
    int w = std.nextInt();
    char[][] table = new char[h][w];
    for (int i = 0; i < h; i++) {
      String line = std.next();
      for (int j = 0; j < w; j++) {
        char c = line.charAt(j);
        table[i][j] = c;
      }
    }

    int max = 0;

    Deque<Pair> deque = new ArrayDeque<>();
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (table[i][j] == '#') {
          continue;
        }

        Pair start = new Pair(i, j, 0);
        boolean[][] visited = new boolean[h][w];

        deque.add(start);
        while (!deque.isEmpty()) {
          Pair p = deque.poll();
          max = Math.max(p.depth, max);
          if (visited[p.left][p.right]) {
            continue;
          }

          visited[p.left][p.right] = true;
          int left = p.right - 1;
          int right = p.right + 1;
          int top = p.left - 1;
          int bottom = p.left + 1;

          if (left >= 0) {
            char c = table[p.left][left];
            if (c != '#' && !visited[p.left][left]) {
              deque.add(new Pair(p.left, left, p.depth + 1));
            }
          }

          if (right < table[0].length) {
            char c = table[p.left][right];
            if (c != '#' && !visited[p.left][right]) {
              deque.add(new Pair(p.left, right, p.depth + 1));
            }
          }

          if (top >= 0) {
            char c = table[top][p.right];
            if (c != '#' && !visited[top][p.right]) {
              deque.add(new Pair(top, p.right, p.depth + 1));
            }
          }

          if (bottom < table.length) {
            char c = table[bottom][p.right];
            if (c != '#' && !visited[bottom][p.right]) {
              deque.add(new Pair(bottom, p.right, p.depth + 1));
            }
          }
        }
      }
    }

    System.out.println(max);
  }
}