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.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = Integer.parseInt(sc.next());
        int w = Integer.parseInt(sc.next());
        List<Integer> xL = new ArrayList<>();
        List<Integer> yL = new ArrayList<>();
        char[][] map = new char[h][w];
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            map[i] = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if (map[i][j] == '.') {
                    xL.add(j);
                    yL.add(i);
                } else {
                    cnt++;
                }
            }
        }
        if (cnt <= 2) {
            System.out.println(h - 1 + w - 1);
            return;
        }
        int ans = 0;
        for (int i = 0; i < xL.size(); i++) {
            Point start = new Point(xL.get(i), yL.get(i));
            for (int j = 0; j < xL.size(); j++) {
                Point goal = new Point(xL.get(j), yL.get(j));
                ans = Math.max(ans, bfs(start, goal, map.clone(), h, w));
            }
        }
        System.out.println(ans);
    }

    private static int bfs(Point start, Point goal, char[][] baseMap, int h, int w) {
        int[] dx = {1, 0, -1, 0};
        int[] dy = {0, 1, 0, -1};
        Deque<Point> dq = new ArrayDeque<>();
        dq.add(start);
        char[][] map = deepCopy(baseMap);
        int[][] moves = new int[h][w];
        map[start.getY()][start.getX()] = '#';
        while (dq.size() > 0) {
            Point p = dq.removeFirst();
            for (int i = 0; i < 4; i++) {
                int x = p.getX() + dx[i];
                int y = p.getY() + dy[i];
                if (x < 0 || x > w - 1 || y < 0 || y > h - 1) {
                    continue;
                }
                if (map[y][x] == '.') {
                    dq.addLast(new Point(x, y));
                    map[y][x] = '#';
                    moves[y][x] = moves[p.getY()][p.getX()] + 1;
                }
            }
        }
        return moves[goal.getY()][goal.getX()];
    }

    private static char[][] deepCopy(char[][] org) {
        if (org == null) {
            return null;
        }

        final char[][] result = new char[org.length][];
        for (int i = 0; i < org.length; i++) {
            result[i] = Arrays.copyOf(org[i], org[i].length);
        }
        return result;
    }
}

class Point {

    private int x;
    private int y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}