module queue
  implicit none
  type :: JagArray
    integer,allocatable :: array(:)
  end type JagArray
contains
  subroutine push(A,b)
  	integer,allocatable,dimension(:)::A
    integer::b
    A=[A,b]
  end subroutine
  subroutine pop(A)
  	integer,allocatable,dimension(:)::A
    A=A(2:)
  end subroutine
end module queue

module maze
integer,allocatable,dimension(:,:)::mazemap
contains
subroutine getmap(mazemap,R,C)
  integer,allocatable,dimension(:,:)::mazemap
  integer R,C
  character(50) linereader
  allocate(mazemap(0:R+1,0:C+1))
  mazemap(0,:)=-2;mazemap(R+1,:)=-2;mazemap(:,0)=-2;mazemap(:,C+1)=-2;
  do i=1,R
    read*,linereader
    do j=1,C
      if(linereader(j:j)==".")mazemap(i,j)=-1
      if(linereader(j:j)=="#")mazemap(i,j)=-2
    end do
  end do
end subroutine
end module maze

use queue
use maze
integer,allocatable,dimension(:)::queue_x,queue_y
integer H,W
integer numcnt
integer dirx(4),diry(4)
dirx=[1,0,-1,0]
diry=[0,1,0,-1]
read*,H,W
call getmap(mazemap,H,W)

mazemap(1,1)=1
queue_x=[1]
queue_y=[1]
do while(mazemap(H,W)==-1)
  do i=1,4
    if(mazemap(queue_x(1)+dirx(i),queue_y(1)+diry(i))==-1)then
      mazemap(queue_x(1)+dirx(i),queue_y(1)+diry(i))=mazemap(queue_x(1),queue_y(1))+1
      call push(queue_x,queue_x(1)+dirx(i))
      call push(queue_y,queue_y(1)+diry(i))
    endif
  end do
  call pop(queue_x)
  call pop(queue_y)
  if(size(queue_x)==0)exit
end do

numcnt=0
do i=1,H
  do j=1,W
    if(mazemap(i,j)==-2)numcnt=numcnt+1
  end do
end do
print"(i0)",merge(-1,H*W-mazemap(H,W)-numcnt,mazemap(H,W)==-1)
end