program main
  character(401) :: Sc
  integer,allocatable,dimension(:, :) :: S, C, D
  integer :: i, j, H, W
  integer(16) :: su1, su0, n
  read*, H, W
  allocate(S(W, H), C(W, H), D(W, H))
  do i = 1, H
    read*, Sc
    do j = 1, W
      if (Sc(j:j) == "#") then
        S(j, i) = 1
      else
        S(j, i) = 0
      endif
    enddo
  enddo

  i = 1
  j = 1
  D = 0
  n = 0
  do while(.true.)
    C = 0
    call search_zero(D, i, j)
    call search(S, i, j, C)
    su1 = sum(C * S)
    su0 = sum(C) - su1
    n = n + su1 * su0
    D = D + C
    if (minval(D) > 0) then
      print*, n
      return
    endif
  end do


contains

  subroutine search_zero(S, i, j)
    integer,dimension(:, :),intent(inout) :: S
    integer :: i, j
    integer :: a, b
    do a = 1, size(S, 1)
      do b = 1, size(S, 2)
        if (S(a, b) == 0) then
          i = a
          j = b
          return
        endif
      enddo
    enddo
  end subroutine

  recursive subroutine search(S, i, j, C)
    integer,dimension(:, :),intent(inout) :: S, C
    integer,intent(in) :: i, j
    integer :: di, dj
    C(i, j) = 1
    do di = -1, 1, 2
      if (i + di >= 1 .and. i + di <= W) then
        if(C(i + di, j) == 0) then
          if (S(i + di, j) /= S(i, j)) then
            call search(S, i + di, j, C)
          endif
        endif
      endif
    enddo
    do dj = -1, 1, 2
      if (j + dj >= 1 .and. j + dj <= H) then
        if(C(i, j + dj) == 0) then
          if (S(i, j + dj) /= S(i, j)) then
            call search(S, i, j + dj, C)
          endif
        endif
      endif
    enddo
  end subroutine

  subroutine print_matrix(A)
    integer,intent(in) :: A(:,:)
    integer :: s(2), i
    s = shape(A)
    do i = 1, s(1)
      print *, A(i, :)
    enddo
  end subroutine

end program
