Problem:
Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6

Solution in Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

module mod_union_find
  implicit none
  integer :: n
  integer, allocatable :: par(:), rnk(:), siz(:)
  private
  public :: initialize, finalize, find, same, unite, size_of
contains
  subroutine initialize(m)
    implicit none
    integer, intent(in) :: m
    integer :: i
    n = m
    if (allocated(par)) deallocate(par)
    if (allocated(rnk)) deallocate(rnk)
    if (allocated(siz)) deallocate(siz)
    allocate(par(n),rnk(n),siz(n))
    rnk = 0
    siz = 1
    do i = 1, n
      par(i) = i
    end do
    return
  end subroutine initialize
  subroutine finalize()
    implicit none
    if (allocated(par)) deallocate(par)
    if (allocated(rnk)) deallocate(rnk)
    if (allocated(siz)) deallocate(siz)
    return
  end subroutine finalize
  recursive function find(i) result(j)
    implicit none
    integer, intent(in) :: i
    integer :: j
    if (par(i).ne.i) then
      par(i) = find(par(i))
    end if
    j = par(i)
    return
  end function find
  function same(i,j) result(y)
    implicit none
    integer, intent(in) :: i, j
    logical :: y
    y = find(i).eq.find(j)
  end function same
  subroutine unite(i,j)
    implicit none
    integer, intent(in) :: i, j
    integer :: x, y
    x = find(i)
    y = find(j)
    if (x.eq.y) return
    if (rnk(x).lt.rnk(y)) then
      par(x) = y
      siz(y) = siz(y)+siz(x)
    else
      par(y) = x
      siz(x) = siz(x)+siz(y)
      if (rnk(x).eq.rnk(y)) rnk(x) = rnk(x)+1
    end if
    return
  end subroutine unite
  function size_of(i) result(s)
    implicit none
    integer, intent(in) :: i
    integer :: s
    s = siz(find(i))
  end function size_of
end module mod_union_find
program alternating_path
  use mod_union_find
  implicit none
  integer :: h, w, a(400,400), i, j, m, num(160000,0:1)
  character(400) :: s
  a = 0
  num = 0
  read(*,*) h, w
  call initialize(h*w)
  do i = 1, h
    read(*,*) s
    s = trim(adjustl(s))
    do j = 1, w
      if (s(j:j).eq."#") a(i,j) = 1
      if (i.ne.1.and.a(i,j).ne.a(i-1,j)) call unite((i-1)*w+j,(i-2)*w+j)
      if (j.ne.1.and.a(i,j).ne.a(i,j-1)) call unite((i-1)*w+j,(i-1)*w+j-1)
    end do
  end do
  do i = 1, h
    do j = 1, w
      num(find((i-1)*w+j),a(i,j)) = num(find((i-1)*w+j),a(i,j))+1
    end do
  end do
  m = 0
  do i = 1, h*w
    m = m+num(i,0)*num(i,1)
  end do
  write(*,'(i0)') m
  call finalize()
  stop
end program alternating_path