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 s[100001];
int zf[100001][50], ans[50], out;
struct po {
  long long he, ti, helen, tilen, tl, len;
} a[100001];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    int l = s[i].length();
    a[i].he = s[i][0] - 'a' + 1;
    a[i].ti = s[i][l - 1] - 'a' + 1;
    a[i].len = l;
    zf[i][s[i][0] - 'a' + 1] = 1;
    if (a[i].len == 1) {
      a[i].tl = 1;
    }
    int now = 1, flag = 0;
    for (int j = 1; j < l; j++) {
      if (s[i][j] == s[i][j - 1]) {
        now++;
        if (j == l - 1) a[i].tilen = now;
        if (now == l) {
          a[i].tl = 1;
          continue;
        }
      } else {
        zf[i][s[i][j - 1] - 'a' + 1] = max(zf[i][s[i][l - 1] - 'a' + 1], now);
        if (flag == 0) {
          flag = 1;
          a[i].helen = now;
        }
        now = 1;
        if (j == l - 1) a[i].tilen = now;
      }
    }
    zf[i][s[i][l - 1] - 'a' + 1] = max(zf[i][s[i][l - 1] - 'a' + 1], now);
  }
  for (int i = 1; i <= 26; i++) ans[i] = zf[1][i];
  for (int i = 2; i <= n; i++) {
    if (a[i].tl) {
      int id = s[i][0] - 'a' + 1;
      for (int j = 1; j <= 26; j++)
        if (j != id && ans[j]) ans[j] = 1;
      ans[id] = (ans[id] + 1) * a[i].len + ans[id];
      continue;
    }
    for (int j = 1; j <= 26; j++)
      if (ans[j] != 0) ans[j] = 1;
    int heid = s[i][0] - 'a' + 1, tiid = s[i][a[i].len - 1] - 'a' + 1;
    ans[heid] += a[i].helen;
    ans[tiid] += a[i].tilen;
    for (int j = 1; j <= 26; j++) {
      ans[j] = max(ans[j], zf[i][j]);
    }
  }
  for (int i = 1; i <= 26; i++) {
    out = max(out, ans[i]);
  }
  cout << out << endl;
}
