line_size = gets.chomp.to_i
lines = line_size.times.map { gets.chomp }

def count_em(string, substring)
  string.scan(/(?=#{substring})/).count
end

ab_count = lines.reduce(0) { |total, line| total += count_em(line, "AB") }
ends_with_a_count = lines.select { |line| line.end_with?("A") }.size
starts_with_b_count = lines.select { |line| line.start_with?("B") }.size
combination_count =  [ends_with_a_count, starts_with_b_count].min

puts ab_count + combination_count
