def c_s(str)
    kk = 0
    while 1
        i = str.sub!('AB','X')
        break if i == nil
        kk += 1
    end
    kk
end

aabb = 0
n=gets.split.map(&:to_i)
s=$<.map(&:chomp)
ab = s.map {|ss| c_s(ss)}.inject(:+)
a = s.select {|ss| ss.end_with?('A')}.select do |e|
  s.reject {|r|r==e}.find {|ss|ss.start_with?('B')}.sub!('B','X')
end

puts ab + a
