program maze_master
    implicit none
    integer(4):: h,w
    integer(4):: i,j, cnt
    character(20)::s
    logical, allocatable:: is_pass(:,:), is_road(:,:)

    read*, h,w
    allocate(is_pass(h,w), is_road(0:h+1,0:w+1))

    is_road(:,:) = .false.
    do i=1,h
        read*, s
        do j=1,w
            if (s(j:j) == "." ) is_road(i,j) = .true.
        end do
    end do

    ! do i=0, h+1
    !     print*, is_road(i,:)
    ! end do
    
    cnt = 0
    do j=1, w
       do i=1, h
        cnt = max(solve_maze(i,j, is_pass, is_road), cnt)
       end do
    end do

    print*, cnt
    
contains

function solve_maze(ih, iw, is_pass, is_road) result(cnt)
    integer(4):: ih, iw, cnt, i, j, dh, dw
    logical:: is_pass(:,:), is_road(0:,0:), is_update
    logical, allocatable:: new_pass(:,:)
    allocate(new_pass(h,w))

    cnt = 0
    is_update = .true.
    is_pass(:,:) = .false.

    if(is_road(ih,iw)) is_pass(ih,iw)=.true.
    ! print*, '------'

    do while(is_update)
        is_update = .false.
        new_pass(:,:) = is_pass(:,:)
        do i = 1,h
            do j= 1,w
                if (.not. is_pass(i,j)) cycle
                do dh = -1,1
                    do dw = -1,1
                        if ((dw/=0 .and. dh/=0) .or. (dw==0 .and. dh==0)) cycle
                        if (is_road(i+dh,j+dw) .and. (.not. is_pass(i+dh,j+dw)))then
                            new_pass(i+dh,j+dw) = .true.
                            is_update = .true.
                        end if
                    end do
                end do
            end do
        end do
        is_pass(:,:) = new_pass(:,:)
        
        if (is_update) cnt = cnt + 1
        ! print*, cnt
        ! do i=1,h
        !     print*, is_pass(i,:)
        ! end do
    end do


    
end function
    
end program maze_master