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 = 5e5 + 1000;
long long dp[N][30];
char s[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    int len = strlen(s + 1), cnt = 1, qz = 1, hz = 1;
    for (int j = 1; j <= len; j++) {
      if (s[j] == s[j + 1])
        cnt++;
      else
        dp[i][s[j] - 'a'] = max(dp[i][s[j] - 'a'], 1ll * cnt), cnt = 1;
    }
    for (int j = 2; j <= len; j++)
      if (s[j] == s[1])
        qz++;
      else
        break;
    for (int j = len - 1; j; j--)
      if (s[j] == s[len])
        hz++;
      else
        break;
    if (i != 1) {
      if (qz == 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'], 1ll * qz + hz + 1);
        }
      } else {
        dp[i][s[1] - 'a'] =
            max(dp[i][s[1] - 'a'], 1ll * !!dp[i - 1][s[1] - 'a'] + qz);
        dp[i][s[len] - 'a'] =
            max(dp[i][s[len] - 'a'], 1ll * !!dp[i - 1][s[len] - 'a'] + hz);
      }
    }
    for (int j = 0; j < 26; j++) dp[i][j] = max(dp[i][j], 1ll * !!dp[i - 1][j]);
  }
  long long ans = 0;
  for (int i = 0; i < 26; i++) ans = max(ans, dp[n][i]);
  cout << ans;
}
