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 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int H = Integer.parseInt(sc.next());
        int W = Integer.parseInt(sc.next());

        String[] F = new String[H];
        for (int h = 0; h < H; h++) {
            F[h] = sc.next();
        }

        Graph g = new Graph(H, W, F);
        int res = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (F[h].charAt(w) != '.') {
                    continue;
                }
                
                int r = g.wfs(h, w);
                if (r > res) {
                    res = r;
                }
            }
        }
        
        System.out.println(res);
    }
}

class Graph {
    private int H;
    private int W;
    private String[] F;
    
    public Graph(int H, int W, String[] F) {
        this.H = H;
        this.W = W;
        this.F = F;
    }
    
    class Node {
        public int h;
        public int w;
        public int c;

        public Node(int h, int w, int c) {
            this.h = h;
            this.w = w;
            this.c = c;
        }
    }
    
    public int wfs(int h0, int w0) {
        int[][] cost = new int[H][W];
        for (int h = 0; h < H; h++) {
            Arrays.fill(cost[h], -1);
        }
        
        Queue<Node> q = new ArrayDeque<>();
        q.add(new Node(h0, w0, 0));
        while (!q.isEmpty()) {
            Node n = q.poll();
            int h = n.h;
            int w = n.w;

            if (cost[h][w] != -1) {
                continue;
            }
            
            cost[h][w] = n.c;
            
            if (h != 0 && F[h - 1].charAt(w) == '.' && cost[h - 1][w] == -1) {
                q.add(new Node(h - 1, w, n.c + 1));
            }

            if (h != H - 1 && F[h + 1].charAt(w) == '.' && cost[h + 1][w] == -1) {
                q.add(new Node(h + 1, w, n.c + 1));
            }            

            if (w != 0 && F[h].charAt(w - 1) == '.' && cost[h][w - 1] == -1) {
                q.add(new Node(h, w - 1, n.c + 1));
            }           
            
            if (w != W - 1 && F[h].charAt(w + 1) == '.' && cost[h][w + 1] == -1) {
                q.add(new Node(h, w + 1, n.c + 1));
            } 
        }
        
        int res = 0;
        for (int h = 0; h < H; h++) {
            int r = Arrays.stream(cost[h]).max().getAsInt();
            if (r > res) {
                res = r;
            }
        }
        
        return res;
    }
}