def c_s(str)
    i = -2
    kk = 0
    while 1
        i = str.index('AB', i+2)
        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')}.size
b = s.select {|ss| ss.start_with?('B')}.size
a_b = s.select {|ss| ss.start_with?('B')}.select {|ss| ss.end_with?('A')}.size
if a_b == a && a_b == b
    if s.select {|ss| c_s(ss)}.select {|ss| ss.end_with?('A')}.size == 0 && s.select {|ss| c_s(ss)}.select {|ss| ss.start_with?('B')}  .size == 0
        aabb = 1 
    end

end
puts ab + ([a,b].min) - aabb
