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;
const int N = 1e5 + 5;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x;
}
char s[N];
int n, dp[N][26];
int main() {
  n = read();
  for (register int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    int res = 1;
    for (register int j = 1; j <= len; j++) {
      if (s[j] == s[j + 1])
        res++;
      else
        dp[i][s[j] - 'a'] = max(dp[i][s[j] - 'a'], res), res = 1;
    }
    int l, r, cur;
    cur = 1;
    while (cur <= len && s[cur] == s[1]) cur++;
    l = cur - 1;
    cur = len;
    while (cur && s[cur] == s[len]) cur--;
    r = len - cur;
    if (i) {
      if (l == len && r == len)
        dp[i][s[1] - 'a'] =
            max(dp[i][s[1] - 'a'],
                len * (dp[i - 1][s[1] - 'a'] + 1) + dp[i - 1][s[1] - 'a']);
      else if (s[1] == s[len]) {
        if (dp[i - 1][s[1] - 'a'])
          dp[i][s[1] - 'a'] = max(dp[i][s[1] - 'a'], l + r + 1);
      } else {
        dp[i][s[1] - 'a'] =
            max(dp[i][s[1] - 'a'], l + (dp[i - 1][s[1] - 'a'] != 0));
        dp[i][s[len] - 'a'] =
            max(dp[i][s[len] - 'a'], l + (dp[i - 1][s[len] - 'a'] != 0));
      }
    }
    for (register int j = 0; j <= 25; j++)
      dp[i][j] = max(dp[i][j], (int)(dp[i - 1][j] != 0));
  }
  int ans = 0;
  for (register int i = 0; i <= 25; i++) ans = max(ans, dp[n][i]);
  printf("%d", ans);
}
