output_description: Print the answer.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      s_1
      \vdots
      s_N
attempt: import re
  
  
  pattern = "AB"
  num_input = int(input())
  
  total_ab = 0
  
  num_ab = 0
  num_end_with_a = 0
  num_start_with_b = 0
  num_both = 0
  
  prints = []
  
  for i in range(0, num_input):
      input_string = input()
      num_ab += len(re.findall(pattern, input_string))
      start_with_b = input_string[0] == "B"
      end_with_a = input_string[-1] == "A"
  
      if start_with_b and end_with_a:
          num_both += 1
      elif start_with_b:
          num_start_with_b += 1
      elif end_with_a:
          num_end_with_a += 1
  
  # print(num_ab, num_both, num_start_with_b, num_end_with_a)
  
  # add in-string 'AB'
  total_ab += num_ab
  
  # consume both first
  
  # do setup, for case of '--A's or 'B--'s not exist
  if num_end_with_a == 0 and num_start_with_b == 0:
      if num_both >= 2:
          num_both -= 2
          num_end_with_a += 1
          num_start_with_b += 1
  
  elif min(num_end_with_a, num_start_with_b) == 0:
      if num_both >= 1:
          if num_end_with_a == 0:
              num_both -= 1
              num_end_with_a += 1
  
          if num_start_with_b == 0:
              num_both -= 1
              num_start_with_b += 1
  
  # then consume
  if num_both > 0:
      total_ab += num_both + 1
      num_end_with_a -= 1
      num_start_with_b -= 1
  
  # then consume other '--A's  and 'B--'s
  total_ab += min(num_start_with_b, num_end_with_a)
  
  print(total_ab)
  exit(0)
  
problem_description: Statement
  
  Snuke has N strings. The i-th string is s_i.
  
  Let us concatenate these strings into one string after arranging them in some
  order. Find the maximum possible number of occurrences of `AB` in the
  resulting string.
samples: [{"input": "3\n    ABCA\n    XBAZ\n    BAD", "output": "2\n    \n\nFor example, if we concatenate `ABCA`, `BAD` and `XBAZ` in this order, the\nresulting string `ABCABADXBAZ` has two occurrences of `AB`.\n\n* * *"}, {"input": "9\n    BEWPVCRWH\n    ZZNQYIJX\n    BAVREA\n    PA\n    HJMYITEOX\n    BCJHMRMNK\n    BP\n    QVFABZ\n    PRGKSPUNA", "output": "4\n    \n\n* * *"}, {"input": "7\n    RABYBBE\n    JOZ\n    BMHQUVA\n    BPA\n    ISU\n    MCMABAOBHZ\n    SZMEHMA", "output": "4"}]