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

_=gets
s=$<.map(&:chomp)
ab = s.map {|ss| c_s(ss)}.inject(:+)
a = s.select {|ss| ss.end_with?('A')}.select do |e|
  s.find {|ss| ss != e && ss.start_with?('B')}.sub!('B','X')
end
puts ab + a.size
