n = gets.to_i
words = Array.new(n).map {gets.upcase.chomp}

$ab_in_word = {}
words.each {|w| $ab_in_word[w] = w.scan('AB').size}

def ab_substrings(last_char, rest_words, i = 0, d = 0)
  return i if rest_words.size.zero?

  ret = 0

  (0...rest_words.size).each {|idx|
    j = if last_char + rest_words[idx][0] == 'AB'
          1 + $ab_in_word[rest_words[idx]]
        else
          $ab_in_word[rest_words[idx]]
        end
    rw = rest_words.clone
    rw.delete_at(idx)
    k = i + j
    tmp = ab_substrings(rest_words[idx][-1], rw, k, d + 1)
    if ret < tmp
      ret = tmp
    end
  }

  ret
end

puts ab_substrings('', words)