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 math
  
  n = int(input())
  s = [input() for i in range(n)]
  
  ans = 0
  b_pre = []
  a_suf = []
  ab = []
  for st in s:
      if st.startswith("B") and st.endswith("A"):
          ans += st.count("AB")
          ab.append(st)
      elif st.startswith("B"):
          ans += st.count("AB")
          b_pre.append(st)
      elif st.endswith("A"):
          ans += st.count("AB")
          a_suf.append(st)
      else:
          ans += st.count("AB")
  
  if len(b_pre) - len(a_suf) > 1:
      while len(b_pre) - len(a_suf) > 1:
          for b in b_pre:
              if b.endswith("A"):
                  a_suf.append(b)
                  b_pre.remove(b)
              if len(b_pre) - len(a_suf) <= 1:
                  break
  elif len(a_suf) - len(b_pre) > 1:
      while len(a_suf) - len(b_pre) > 1:
          for a in a_suf:
              if b.startswith("B"):
                  b_pre.append(b)
                  a_suf.remove(b)
              if len(a_suf) - len(b_pre) > 1:
                  break
  
  ans += math.ceil(len(ab) / 2)
  if len(b_pre) < len(a_suf):
      ans += len(b_pre)
      ans += len(a_suf) - len(b_pre)
  else:
      ans += len(a_suf)
      ans += len(b_pre) - len(a_suf)
  
  
  print(ans)
  
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"}]