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.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Stack;


public class Main{
	public static class FastReader {
		BufferedReader br;
		StringTokenizer root;
		
 
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next() {
			while (root == null || !root.hasMoreTokens()) {
				try {
					root = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return root.nextToken();
		}
 
		int nextInt() {
			return Integer.parseInt(next());
		}
 
		double nextDouble() {
			return Double.parseDouble(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}
	
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	static int mod = (int) (1e9+7);
	static long cf = 998244353;
    static final long MAX = (long) 1e15;
    public static List<Integer>[] edges;
    public static int[][] parent;
    public static int col = 32;
    public static int[] Bit;
    public static long[] dp;
    
	public static void main(String[] args) {
	
	
	FastReader sc = new FastReader();
	
	int n = sc.nextInt();
	int m = sc.nextInt();
	char[][] tab = new char[n][m];
	for(int i=0;i<n;++i) 
		tab[i] = sc.next().toCharArray();
	int ans = 0;
	for(int i=0;i<n;++i) {
		for(int j=0;j<m;++j) {
			if(tab[i][j] != '#')
			ans = Math.max(ans, bfs(i,j,n,m,tab));
		}
	}
	out.print(ans);
	
	out.close();
	}

	static int[] dx = {0,-1,0,1};
	static int[] dy = {-1,0,1,0};
	
	private static int bfs(int i, int j, int n, int m, char[][] tab)  {
		boolean[][] vis = new boolean[n][m];
		Queue<Node> q = new LinkedList<>();
		int ans = 0;
		q.add(new Node(i,j,0));
		vis[i][j] = true;
		while(q.size() > 0) {
			Node node = q.poll();
			for(int k=0;k<4;++k) {
				int x = node.x+dx[k];
				int y = node.y+dy[k];
				if(x<0 || y<0 || x>=n || y>=m || tab[x][y] == '#' || vis[x][y]) continue;
				vis[x][y] = true;
				q.add(new Node(x,y,node.d+1));
				
			}
			//out.println(node.x+" "+node.y+" , "+node.d);
			ans = Math.max(ans,node.d);
		}
		return ans;
	}
	static class Node{
		int x;
		int y;
		int d;
		Node(int x,int y,int d){
			this.x =x;
			this.y = y;
			this.d = d;
		}
	}
	
}