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 Ruby (2.3.3) (judge verdict: Runtime Error)

H,W=gets.split.map(&:to_i)
$array = Array.new(H).map{Array.new(W).map{Array.new(2,0)}}
H.times do |i|
  str = gets.chomp!
  $cnt = 0
  str.chars {|ch|
    if ch =="#"
      $array[i][$cnt] = [0,0]
    elsif ch =="."
      $array[i][$cnt] = [1,0]
    end
    $cnt += 1
  }
end
$cnt = 0
def explode(color,column,row)
  if row+1<W && $array[column][row+1][0] == (color+1)%2 && $array[column][row+1][1] == 0
    if color == 0
      $cnt += 1
    end
    $array[column][row+1][1] = 1
    explode((color+1)%2,column,row+1)
  end
  if row-1>-1 && $array[column][row-1][0] == (color+1)%2 && $array[column][row-1][1] == 0
    if color == 0
      $cnt += 1
    end
    $array[column][row-1][1] = 1
    explode((color+1)%2,column,row-1)
  end
  if column+1<H && $array[column+1][row][0] == (color+1)%2 && $array[column+1][row][1] == 0
    if color == 0
      $cnt += 1
    end
    $array[column+1][row][1] = 1
    explode((color+1)%2,column+1,row)
  end
  if column-1>-1 && $array[column-1][row][0] == (color+1)%2 && $array[column-1][row][1] == 0
    if color == 0
      $cnt += 1
    end
    $array[column-1][row][1] = 1
    explode((color+1)%2,column-1,row)
  end
end
#puts $array[0][2][0]
$array.each_with_index { |row,i|
  row.each_with_index { |fac,j|
    if fac[0] == 0
      fac[1] = 1
#      puts "#{i} is i. #{j} is j"
      explode(0,i,j)
      $array.each {|a|
        a.each {|b|
          b[1] = 0
        }
      }
    end
  }
}
puts $cnt