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;
int n;
string str;
int dp[100005][27];
int main() {
  cin >> n;
  for (int x = 1; x <= n; x++) {
    cin >> str;
    int tlen[27] = {0};
    int mxlen[27] = {0};
    int len = str.size();
    for (int i = 0; i < len; i++) {
      if (i > 0 && str[i] == str[i - 1])
        ++tlen[str[i] - 'a'];
      else
        tlen[str[i] - 'a'] = 1;
      mxlen[str[i] - 'a'] = max(mxlen[str[i] - 'a'], tlen[str[i] - 'a']);
    }
    for (int i = 0; i < 26; i++) {
      dp[x][i] = mxlen[i];
      int l = 0, r = len - 1;
      while (l <= r && str[l] == i + 'a') l++;
      while (r > l && str[r] == i + 'a') r--;
      if (l == len)
        dp[x][i] = max(dp[r][i], (len + 1) * dp[x - 1][i] + len);
      else if (dp[x - 1][i] > 0)
        dp[x][i] = max(dp[x][i], l + len - r);
    }
  }
  int ans = 0;
  for (int i = 0; i <= 25; i++) ans = max(ans, dp[n][i]);
  cout << ans << '\n';
}
