h, w = gets.split.map(&:to_i)
a = (1..h).map { gets.chomp.split("") }
pos = {}
a.each.with_index do |row, i|
  row.each.with_index do |c, j|
    if c =~ /\w/
      (pos[c] ||= []) << i
      pos[c] << j
    end
  end
end

def paths(y1, x1, y2, x2)
  return [Range.new(*[y1, y2].sort).map {|y| [y, x1] }] if x1 == x2
  return [Range.new(*[x1, x2].sort).map {|x| [y1, x] }] if y1 == y2
  return [
    (Range.new(*[x1, x2].sort).map {|x| [y1, x] } + Range.new(*[y1, y2].sort).map {|y| [y, x2] }),
    (Range.new(*[y1, y2].sort).map {|y| [y, x1] } + Range.new(*[x1, x2].sort).map {|x| [y2, x] })
  ]
end

answer = pos.size * 2
loop do
  erase = false

  pos.each do |c, coor|
    y1, x1, y2, x2 = coor
    paths = paths(y1, x1, y2, x2)
    if paths.any? {|path| (path - [[y1, x1], [y2, x2]]).size > 0 && path.all? {|y, x| [y, x] == [y1, x1] || [y, x] == [y2, x2] || a[y][x] == ?. } }
      erase = true
      pos.delete(c)
      a[y1][x1] = ?.
      a[y2][x2] = ?.
      break
    end
  end
  break if !erase
end
answer -= pos.size * 2
p answer

