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.*;

public class Main implements Runnable {

    private static int MOD = 1_000_000_007;

    public static void main(String[] args) {
        // Run with 32MB stack
        Thread thread = new Thread(null, new Main(), "", 32 * 1024 * 1024);
        thread.start();
    }

    @Override
    public void run() {
        final Scanner scanner = new Scanner(System.in);
        solve(scanner);
    }

    static int H, W;
    static char[][] m;
    static void solve(Scanner scanner) {
        H = Integer.parseInt(scanner.next());
        W = Integer.parseInt(scanner.next());
        m = new char[H][W];
        for (int i = 0; i < H; i++) {
            m[i] = scanner.next().toCharArray();
        }

        Set<Integer> visited = new HashSet<>();
        int max = 0;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                for (int i2 = 0; i2 < H; i2++) {
                    for (int j2 = 0; j2 < W; j2++) {
                        if (visited.add(i * 1000000 + j * 10000 + i2 * 100 + j2) ||
                                visited.add(i2 * 1000000 + j2 * 10000 + i * 100 + j)) {
                            int res = bfs(i, j, i2, j2);
                            if (res >= 0) {
                                max = Math.max(max, res);
                            }
                        }
                    }
                }
            }
        }

        System.out.println(max);
    }

    static int[][][][] dist = new int[20][20][20][20];
    static int bfs(int sy, int sx, int ey, int ex) {
        Queue<Integer> q = new LinkedList<>();
        Set<Integer> visited = new HashSet<>();
        q.add(sy * 100 + sx);
        q.add(null);
        int d = 0;

        while (!q.isEmpty()) {
            Integer top = q.poll();
            if (top == null) {
                d++;
                if (q.peek() == null) {
                    break;
                }
                q.add(null);
                continue;
            }
            int y = top / 100;
            int x = top % 100;
            if (x < 0 || x >= W || y < 0 || y >= H || m[y][x] == '#') {
                continue;
            }

            if (y == ey && x == ex) {
                if (dist[sy][sx][ey][ex] > 0) {
                    dist[sy][sx][ey][ex] = Math.min(dist[sy][sx][ey][ex], d);
                } else {
                    dist[sy][sx][ey][ex] = d;
                }
                return dist[sy][sx][ey][ex];
            }
            if (dist[sy][sx][y][x] > 0) {
                dist[sy][sx][y][x] = Math.min(dist[sy][sx][y][x], d);
            } else {
                dist[sy][sx][y][x] = d;
            }
            if (dist[y][x][ey][ex] > 0) {
                dist[sy][sx][ey][ex] = Math.min(dist[sy][sx][ey][ex], d + dist[y][x][ey][ex]);
                return dist[sy][sx][ey][ex];
            }
            if (visited.add(top)) {
                q.add((y + 1) * 100 + x);
                q.add((y - 1) * 100 + x);
                q.add(y * 100 + x + 1);
                q.add(y * 100 + x - 1);
            }
        }

        return -1;
    }

}