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: Wrong Answer)

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.util.function.*;
import java.util.function.Predicate;
import java.math.BigInteger;
import java.rmi.MarshalException;
import java.time.Instant;
import java.time.Duration;
import java.util.concurrent.*;

public class Main {
    static int height;
    static int width;
    static char[][] grid;
    static int[] dh = new int[] {0, 0, 1, -1};
    static int[] dw = new int[] {1, -1, 0, 0};

    static boolean valid(int h, int w) {
        return h >= 0 && h < height && w >= 0 && w < width && grid[h][w] != '#';
    }
    // return furthest dist, height, width of futhest point
    static int[] BFS(int h, int w) {
        Queue<Pair> q = new LinkedList<>();
        q.add(new Pair(h, w));
        boolean[][] visited = new boolean[grid.length][grid[0].length];
        int[][] dist = new int[grid.length][grid[0].length];
        visited[h][w] = true;
        dist[h][w] = 0;
        
        int maxdist = -1;
        int maxh = -1;
        int maxw = -1;
        
        while (!q.isEmpty()) {
            Pair cur = q.poll();
            if (dist[cur.x][cur.y] > maxdist) {
                maxdist = dist[cur.x][cur.y];
                maxh = cur.x;
                maxw = cur.y;
            }
            for (int i = 0; i < dh.length; i++) {
                int nexth = cur.x + dh[i];
                int nextw = cur.y + dw[i];
                if (valid(nexth, nextw) && !visited[nexth][nextw]) {
                    q.add(new Pair(nexth, nextw));
                    visited[nexth][nextw] = true;
                    dist[nexth][nextw] = dist[cur.x][cur.y] + 1;
                }
            }
        }
        return new int[] {maxdist, maxh, maxw};
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // long startTime = System.nanoTime();
        // int  = sc.next();
        // int  = sc.nextInt();
        /////////////////////////////////////////////////////
        height = sc.nextInt();
        width = sc.nextInt();
        grid = new char[height][width];
        for (int i = 0; i < height; i++) {
            grid[i] = sc.next().toCharArray();
        }
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (grid[i][j] != '#') {
                    int[] firstres = BFS(i, j);
                    int[] sndres = BFS(firstres[1], firstres[2]);
                    System.out.println(sndres[0]);
                    return;
                }
            }
        }

        // for (int i = 0; i < h; i++) {
        //     System.out.println(new String(grid[i]));
        // }
        /////////////////////////////////////////////////////
        // long endTime = System.nanoTime();
        // System.out.printf("Executed in: %.2fms\n", ((double)endTime - startTime) / 1000000);
        // sc.close();
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }
    static boolean isOdd(int n) {
        return n % 2 == 1;
    }
     
    static boolean isPrime(int n) {
        if (n == 0 || n == 1) {
            return false;
        } else {
            for (int i = 2; i * i <= n; i++) {
                if (n % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

class Pair {
    public int x;
    public int y;

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

    @Override
    public boolean equals(Object o) {
        // System.out.println("inside equals");
        if (o == this) {
            return true;
        } else if (!(o instanceof Pair)) {
            return false;
        } else {
            Pair p = (Pair)o;
            return this.x == p.x && this.y == p.y;
        }
    }

    @Override
    public int hashCode() {
        // System.out.println("inside hashcode");
        return Arrays.hashCode(new int[]{this.x, this.y});
    }

    @Override
    public String toString() {
        return "(" + this.x + ", " + this.y + ")";
    }
}