<?php

list($h, $w) = explode(' ', trim(fgets(STDIN)));

$s = [];
for ($i = 0; $i < $h; ++$i) {
    $s[] = str_split(trim(fgets(STDIN)));
}

$warshall_floyd = new WarshallFloyd($h, $w, $s);
echo $warshall_floyd->getLongestDistnace();

/**
 * ワーシャル–フロイド法での解法
 */
class WarshallFloyd
{
    private $height;
    private $width;
    private $vertex;
    private $distance;

    public function __construct($height, $width, $maze)
    {
        $this->height = $height;
        $this->width = $width;
        $this->vertex = $this->height * $this->width;

        $this->initializeDistance($maze);
    }

    public function getLongestDistnace()
    {
        $this->seekDistance();
        return max(array_map(function ($distance) {
            return max($distance);
        }, $this->distance));
    }

    protected function initializeDistance($maze)
    {
        for ($i_x = 0; $i_x < $this->width; ++$i_x) {
            for ($i_y = 0; $i_y < $this->height; ++$i_y) {
                $i_index = $this->convertXY2Index($i_x, $i_y);
                if ($maze[$i_y][$i_x] === '#') {
                    $this->distance[$i_index] = array_fill(0, $this->vertex, false);
                    continue;
                }

                $this->distance[$i_index] = [];

                for ($j_x = 0; $j_x < $this->width; ++$j_x) {
                    for ($j_y = 0; $j_y < $this->height; ++$j_y) {
                        $j_index = $this->convertXY2Index($j_x, $j_y);
                        if ($maze[$j_y][$j_x] === '#') {
                            $this->distance[$i_index][$j_index] = false;
                        } elseif ($j_x === $i_x && $j_y === $i_y) {
                            $this->distance[$i_index][$j_index] = 0;
                        } elseif (($j_x === $i_x && $j_y === $i_y + 1)
                            || ($j_x === $i_x && $j_y === $i_y - 1)
                            || ($j_x === $i_x + 1 && $j_y === $i_y)
                            || ($j_x === $i_x - 1 && $j_y === $i_y)
                        ) {
                            $this->distance[$i_index][$j_index] = 1;
                        } else {
                            $this->distance[$i_index][$j_index] = $this->vertex;
                        }
                    }
                }
            }
        }
    }

    protected function seekDistance()
    {
        for ($k = 0; $k < $this->vertex; ++$k) {
            for ($i = 0; $i < $this->vertex; ++$i) {
                for ($j = 0; $j < $this->vertex; ++$j) {
                    if ($this->distance[$i][$j] === false
                        || $this->distance[$i][$k] === false
                        || $this->distance[$k][$j] === false
                    ) {
                        continue;
                    }
                    if ($this->distance[$i][$j] > $this->distance[$i][$k] + $this->distance[$k][$j]) {
                        $this->distance[$i][$j] = $this->distance[$i][$k] + $this->distance[$k][$j];
                    }
                }
            }
        }
    }

    protected function convertXY2Index($x, $y)
    {
        return $this->width * $y + $x;
    }
}
