9
lfpgbnlzyn
c
s
w
r
m
q
y
yinfblfcdhidphyfvgkxyuwomahiibbhnigdslsguhjkplibnhhrshtekwgefxeugyyyyy
 6
u
p
m
z
v
vv
 4
qe
nd
iqryhukieskfvaeettersinksrmazelxtgvartuz
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 6
nf
ysvchtjkfgsbyfzxjaykey
advqnf
wl
pi
ppppfvvmaebvhkoagppppp
 3
j
k
kkailgwqnxtdsttkkkkkkk
 4
o
yhnx
e
ennn
 10
j
dyfu
udzj
wwow
xtnw
f
c
q
o
gggg
 6
w
g
c
j
y
f
 4
r
o
w
wwwwwwwwww
 2
zxwp
ppppctfisvflhtjggg
 5
m
g
u
t
tttt
 2
n
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 3
lt
en
eeeeeeeeee
 4
vr
hp
l
llbn

7 5 541 10 10 4 4 1 21 5 9 93 21 3
#include <bits/stdc++.h>
using namespace std;
bool same;
long long N, pre, suf, ans;
char cp, cs;
string s[200005];
long long findPre(long long t) {
  long long i = 0;
  while (i + 1 < s[t].size() && s[t][i + 1] == s[t][i]) i++;
  return i + 1;
}
long long findSuf(long long t) {
  long long i = s[t].size() - 1;
  while (i - 1 >= 0 && s[t][i - 1] == s[t][i]) i--;
  return s[t].size() - i;
}
long long longest(long long t, long long c) {
  char ant = 'A';
  long long tmp = 0;
  long long act = 0;
  for (auto v : s[t]) {
    if (v == ant)
      act++;
    else
      act = 1;
    ant = v;
    if (ant == c) tmp = max(tmp, act);
  }
  return tmp;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (long long i = 1; i <= N; i++) cin >> s[i];
  pre = findPre(N);
  cp = s[N][0];
  suf = findSuf(N);
  cs = s[N][s[N].size() - 1];
  ans = max(pre, suf);
  if (pre == suf && pre == s[N].size()) same = 1;
  for (long long i = N - 1; i > 0; i--) {
    long long nwpre = findPre(i);
    long long nwsuf = findSuf(i);
    if (same) {
      if (nwpre == nwsuf && nwpre == s[i].size() && s[i][0] == cp) {
        long long tam = s[i].size() + pre * (s[i].size() + 1);
        pre = suf = tam;
        ans = max(ans, tam);
      } else {
        if (s[i][0] == cp) {
          long long nw = nwpre + (nwpre + 1) * pre;
          ans = max(ans, nw);
          pre = nw;
        }
        if (s[i][s[i].size() - 1] == cp) {
          long long nw = nwsuf + (nwsuf + 1) * pre;
          ans = max(ans, nw);
          suf = nw;
        }
        int sz = longest(i, cp);
        ans = max(ans, pre * (sz + 1) + sz);
        same = 0;
      }
    } else {
      int lp = longest(i, cp);
      int ls = longest(i, cs);
      if (lp) ans = max(ans, pre + 1);
      if (ls) ans = max(ans, suf + 1);
      if (cp == cs && lp) ans = max(ans, 1 + suf + pre);
    }
  }
  cout << ans;
  return 0;
}
