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>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 2e5;
int dp[N][27];
int b[27];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < 27; j++) {
      if (b[j] == 1) {
        dp[i][j] = 1;
      }
    }
    for (int j = 0; j < s.size(); j++) {
      int p1 = j;
      while (p1 < s.size() && s[p1] == s[j]) {
        p1++;
      }
      dp[i][s[j] - 'a'] = max(dp[i][s[j] - 'a'], p1 - j);
      j = p1 - 1;
    }
    int p2 = 0;
    while (p2 < s.size() && s[p2] == s[0]) {
      p2++;
    }
    int p3 = s.size() - 1;
    while (p3 > p2 && s[p3] == s[0]) {
      p3--;
    }
    int p4 = s.size() - 1;
    while (p4 > -1 && s[p4] == s[s.size() - 1]) {
      p4--;
    }
    int p5 = 0;
    while (p4 > p5 && s[p4] == s[s.size() - 1]) {
      p5++;
    }
    dp[i][s[s.size() - 1] - 'a'] =
        max(dp[i][s[s.size() - 1] - 'a'],
            p4 + (int)s.size() - 1 - p5 + b[s[s.size() - 1] - 'a']);
    dp[i][s[0] - 'a'] =
        max(dp[i][s[0] - 'a'], p2 + (int)s.size() - 1 - p3 + b[s[0] - 'a']);
    if (p2 == s.size()) {
      dp[i][s[0] - 'a'] =
          max(dp[i][s[0] - 'a'], (dp[i - 1][s[0] - 'a'] + 1) * (int)(s.size()) +
                                     dp[i - 1][s[0] - 'a']);
    }
    for (int j = 0; j < s.size(); j++) {
      b[s[j] - 'a'] = 1;
    }
    for (int j = 0; j < 27; j++) {
      dp[i][j] = min((int)1e9, dp[i][j]);
    }
  }
  int ms = 0;
  for (int i = 0; i < 27; i++) {
    ms = max(ms, dp[n][i]);
  }
  cout << ms;
}
