using AtCoderBeginnerContest151.Questions;
using AtCoderBeginnerContest151.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace AtCoderBeginnerContest151.Questions
{
    public class QuestionD : AtCoderQuestionBase
    {
        bool[,] canEnter;
        int h;
        int w;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var hw = inputStream.ReadIntArray();
            h = hw[0];
            w = hw[1];

            canEnter = new bool[h + 2, w + 2];

            for (int i = 0; i < h; i++)
            {
                var s = inputStream.ReadLine().Select(c => c == '.').ToArray();

                for (int j = 0; j < s.Length; j++)
                {
                    canEnter[i + 1, j + 1] = s[j];
                }
            }

            var max = int.MinValue;
            for (int i = 1; i <= h; i++)
            {
                for (int j = 1; j <= w; j++)
                {
                    if (canEnter[i, j])
                    {
                        max = Math.Max(max, GetMaxDistance(i, j));
                    }
                }
            }

            yield return max;
        }

        private int GetMaxDistance(int startH, int startW)
        {
            var seen = new bool[h + 2, w + 2];
            var distances = new int[h + 2, w + 2];
            var todo = new Queue<Coordinate>();

            todo.Enqueue(new Coordinate(startH, startW));
            seen[startH, startW] = true;

            while (todo.Any())
            {
                var current = todo.Dequeue();
                var currentDistance = distances[current.H, current.W];

                CheckCanGoAndQueue(current.H + 1, current.W, currentDistance, seen, todo, distances);
                CheckCanGoAndQueue(current.H - 1, current.W, currentDistance, seen, todo, distances);
                CheckCanGoAndQueue(current.H, current.W + 1, currentDistance, seen, todo, distances);
                CheckCanGoAndQueue(current.H, current.W - 1, currentDistance, seen, todo, distances);
            }

            var max = 0;

            for (int i = 1; i <= h; i++)
            {
                for (int j = 1; j <= w; j++)
                {
                    max = Math.Max(max, distances[i, j]);
                }
            }

            return max;
        }

        private void CheckCanGoAndQueue(int nextH, int nextW, int currentDistance, bool[,] seen, Queue<Coordinate> todo, int[,] distances)
        {
            if (canEnter[nextH, nextW] && !seen[nextH, nextW])
            {
                todo.Enqueue(new Coordinate(nextH, nextW));
                seen[nextH, nextW] = true;
                distances[nextH, nextW] = currentDistance + 1;
            }
        }

    }

    struct Coordinate
    {
        public int H { get; }
        public int W { get; }

        public Coordinate(int h, int w)
        {
            H = h;
            W = w;
        }
    }
}


namespace AtCoderBeginnerContest151
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionD();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
        }
    }
}

#region Base Classes

namespace AtCoderBeginnerContest151.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }

}

#endregion

#region Extensions

namespace AtCoderBeginnerContest151.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);
    }
}

#endregion

Measured memory_bytes: 13524