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: Time Limit Exceeded)

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int H = sc.nextInt();
    int W = sc.nextInt();

    char[][] mtx = new char[H][W];

    for (int i = 0; i < H; i++) {
      mtx[i]= sc.next().toCharArray();
    }

//    System.out.println(bfs(mtx,2,0,0,4));
//    if(mtx !=null) return;
    int ans = H-1+W-1;
    for (int i = 0; i < H*W ; i++) {//Start
      int r1 = i/W;int c1 = i%W;
      if(mtx[r1][c1]=='#') continue;
      for (int j = 0; j < H*W; j++) {//Goal
        int r2 = j/W;int c2 = j%W;
        if(mtx[r2][c2]=='#') continue;

        int steps = bfs(mtx,r1,c1,r2,c2);
        ans= Math.max(steps,ans);
      }
    }

    System.out.println(ans);
  }
  static
  int bfs(char[][] mtx, int r1,int c1, int r2,int c2){
    int[][] adj = {{-1,0},{1,0},{0,-1},{0,1}};
    Queue<String> q = new LinkedList<>();
    Map<String,Integer> m = new HashMap<>();
    Set<String> visited = new HashSet<>();
    String start = r1+" "+c1;
    String goal = r2+" "+c2;
    visited.add(start);
    q.offer(start);
    m.put(start,0);

    int steps=0;
    while(!q.isEmpty()){
      String s = q.remove();
      int step = m.get(s);

      if(s.equals(goal)) return step;
      String[]  rc = s.split(" ");
      int r = Integer.parseInt(rc[0]);
      int c = Integer.parseInt(rc[1]);


      for (int i = 0; i < adj.length; i++) {
        int rr = r + adj[i][0];
        int cc = c + adj[i][1];

        if(rr>=0 && rr<mtx.length && cc >=0 && cc <mtx[0].length && !visited.contains(rc) && mtx[rr][cc]=='.'){
          int[] newPath = new int[2];
          newPath[0]= rr;
          newPath[1]= cc;
          String rrcc =rr+" "+cc;
          q.offer(rrcc);
          visited.add(rrcc);
          m.put(rrcc,step+1);
        }


      }


    }

    return -1;
  }


}