require 'set'

r = true
while r do
h, w = readline.split(" ").map(&:to_i)
m = []
h.times do |i|
  m << readline.split("")
end

if h == 0 then
  r = false
  next
end

d = (0...h).map{|y| (0...w).map{|x| lambda {return {x: x, y: y} }}}
s = Set.new

h.times do |y|
  w.times do |x|
    if x-1>=0 && m[y][x]==m[y][x-1] then
      to = d[y][x-1].call
      if !s.delete?(to).nil?
        d[to[:y]][to[:x]] = lambda{ d[y][x].call }
      end
    end
    if y-1>=0 && m[y][x]==m[y-1][x] then
      to = d[y-1][x].call
      if !s.delete?(to).nil?
        d[to[:y]][to[:x]] = lambda{ d[y][x].call }
      end
    end
    s.add({x: x, y: y})
  end
end
p s.count
end
