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.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

class Main {

  static class Solver {
    final FastScanner sc;
    final PrintWriter writer;

    Solver(final FastScanner sc, final PrintWriter writer) {
      this.sc = sc;
      this.writer = writer;
    }

    static class Cell {
      int h, w, step;

      public Cell(int h, int w, int step) {
        this.h = h;
        this.w = w;
        this.step = step;
      }
    }

    void run() {
      int h = sc.nextInt();
      int w = sc.nextInt();
      char[][] s = new char[h][w];
      for (int i = 0; i < h; i++) {
        s[i] = sc.next().toCharArray();
      }
      int maxStep = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          // search start position
          if (s[i][j] == '#') {
            continue;
          }
          Queue<Cell> queue = new LinkedList<>();
          queue.add(new Cell(i, j, 0));
          int step = bfs(s, new boolean[h][w], queue);
          maxStep = Math.max(maxStep, step);
        }
      }
      writer.println(maxStep);
    }

    private int bfs(char[][] map, boolean[][] visited, Queue<Cell> queue) {
      int maxStep = 0;
      while (!queue.isEmpty()) {
        Cell element = queue.poll();
        visited[element.h][element.w] = true;
        maxStep = Math.max(maxStep, element.step);
        if (element.h > 0 && !visited[element.h - 1][element.w] && map[element.h - 1][element.w] != '#') {
          queue.add(new Cell(element.h - 1, element.w, element.step + 1));
        }
        if (element.w > 0 && !visited[element.h][element.w - 1] && map[element.h][element.w - 1] != '#') {
          queue.add(new Cell(element.h, element.w - 1, element.step + 1));
        }
        if (element.h < map.length - 1 && !visited[element.h + 1][element.w] && map[element.h + 1][element.w] != '#') {
          queue.add(new Cell(element.h + 1, element.w, element.step + 1));
        }
        if (element.w < map[element.h].length - 1 && !visited[element.h][element.w + 1]
            && map[element.h][element.w + 1] != '#') {
          queue.add(new Cell(element.h, element.w + 1, element.step + 1));
        }
      }
      return maxStep;
    }
  }

  public static void main(final String[] args) {
    final FastScanner sc = new FastScanner();
    final PrintWriter w = new PrintWriter(System.out);
    new Solver(sc, w).run();
    w.flush();
    w.close();
  }

  // CRT stuff
  static class MOD {
    private static final int MOD = (int) 1e9 + 7;

    static long plus(long x, long y) {
      x %= MOD;
      y %= MOD;
      return (x + y) % MOD;
    }

    static long sub(long x, long y) {
      x %= MOD;
      y %= MOD;
      return (x - y + MOD) % MOD;
    }

    static long multi(long x, long y) {
      x %= MOD;
      y %= MOD;
      return x * y % MOD;
    }

    static long div(long x, long y) {
      x %= MOD;
      y %= MOD;
      return x * pow(y, MOD - 2) % MOD;
    }

    static long pow(final long a, final long p) {
      if (p == 0)
        return 1;
      if (p % 2 == 0) {
        final long halfP = p / 2;
        final long root = pow(a, halfP);
        return root * root % MOD;
      } else
        return a * pow(a, p - 1) % MOD;
    }
  }

  // FastScanner
  static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    public FastScanner() {
    }

    private boolean hasNextByte() {
      if (ptr < buflen) {
        return true;
      } else {
        ptr = 0;
        try {
          buflen = in.read(buffer);
        } catch (final IOException e) {
          e.printStackTrace();
        }
        if (buflen <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte())
        return buffer[ptr++];
      else
        return -1;
    }

    private boolean isPrintableChar(final int c) {
      return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr]))
        ptr++;
      return hasNextByte();
    }

    public String next() {
      if (!hasNext())
        throw new NoSuchElementException();
      final StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    public long nextLong() {
      if (!hasNext())
        throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    public int nextInt() {
      final long nl = nextLong();
      if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
        throw new NumberFormatException();
      return (int) nl;
    }

    public double nextDouble() {
      return Double.parseDouble(next());
    }

    public int[] nextIntArray(final int n) {
      final int[] a = new int[n];
      for (int i = 0; i < n; i++)
        a[i] = nextInt();
      return a;
    }

    public long[] nextLongArray(final int n) {
      final long[] a = new long[n];
      for (int i = 0; i < n; i++)
        a[i] = nextLong();
      return a;
    }
  }
}