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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.*;
import java.util.*;
import static java.lang.Math.min;
import static java.lang.Math.max;
public class Main {
    static int max; static  int arr[][]; static boolean visited[][];
    static int depth[][]; static int n,m;
    private static void bfs(int x1, int y1){
        Queue<Pair> q = new LinkedList<>();
        q.add(new Pair(x1,y1));
        while(q.size() > 0){
            Pair p = q.poll();
            int x = p.x; int y = p.y;
            visited[x][y] = true;
            if(x+1 < n && !visited[x+1][y] && arr[x+1][y]==0) {
                q.add(new Pair(x + 1, y));
                depth[x+1][y] = 1 + depth[x][y];
                if(depth[x+1][y] > max) max = depth[x+1][y];
                visited[x+1][y]  =true;
            }
            if(x-1 >=0 && !visited[x-1][y] && arr[x-1][y]==0) {
                q.add(new Pair(x - 1, y));
                depth[x-1][y] = 1 + depth[x][y];
                if(depth[x-1][y] > max) max = depth[x-1][y];
                visited[x-1][y] = true;
            }
            if(y+1 < m && !visited[x][y+1] && arr[x][y+1]==0) {
                q.add(new Pair(x, y + 1));
                depth[x][y+1] = 1 + depth[x][y];
                if(depth[x][y+1] > max) max = depth[x][y+1];
                visited[x][y+1] = true;
            }
            if(y-1 >=0 && !visited[x][y-1] && arr[x][y-1]==0) {
                q.add(new Pair(x, y - 1));
                depth[x][y-1] = 1 + depth[x][y];
                if(depth[x][y-1] > max) max = depth[x][y-1];
                visited[x][y-1] = true;
            }
        }
    }
    private static void solver(InputReader sc, PrintWriter out) throws Exception {
             n = sc.nextInt();
             m = sc.nextInt();
           arr = new int[n][m];
           visited = new boolean[n][m];
           depth  = new int[n][m];
            for(int i=0; i<n; i++){
                    String s = sc.next();
                for(int j=0; j<s.length(); j++) {
                    if (s.charAt(j)=='#') arr[i][j] = 1;
                }
            }
            for(int i=0; i<n; i++){
                for(int j=0; j<m; j++){
                    if(arr[i][j]==0) {
                       for(int k=0; k<n; k++){
                           for(int l=0; l<m; l++){
                               visited[k][l] = false;
                               depth[k][l] = 0;
                           }
                       }
                        bfs(i, j);
                    }
                }
            }
            out.println(max);
    }
    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solver(in,out);
        out.close();
    }
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}
class Pair implements Comparable<Pair>{
    int x,y;
    Pair(int x, int y){
        this.x = x;
        this.y = y;
    }
    public int compareTo(Pair p){
        return (int)(this.x - p.x);
    }
}
class Tuple{
    String s;
    int x, y;

    public Tuple(String s, int x, int y) {
        this.s = s;
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Tuple tuple = (Tuple) o;
        return x == tuple.x &&
                y == tuple.y &&
                s.equals(tuple.s);
    }

    @Override
    public int hashCode() {
        return Objects.hash(s, x, y);
    }
}