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;
void maxi(int& x1, int x2) { x1 = max(x1, x2); }
int cnt[256];
int main() {
  int n;
  cin >> n;
  for (int it = 0; it < n; it++) {
    string s;
    cin >> s;
    int cnt2[256];
    fill(cnt2, cnt2 + 256, 0);
    for (int i = 0; i < 256; i++) {
      if (cnt[i] != 0) {
        cnt2[i] = 1;
      }
    }
    int pos = 0;
    int len1 = 0;
    char c1 = s[0];
    int len2 = 0;
    char c2 = 0;
    for (int i = 1; i < s.size(); i++) {
      if (s[i] != s[pos]) {
        cnt2[s[pos]] = max(cnt2[s[pos]], i - pos);
        if (pos == 0) {
          len1 = i;
          c1 = s[0];
        }
        pos = i;
      }
    }
    if (len1 == 0) len1 = s.size();
    len2 = s.size() - pos;
    c2 = s.back();
    cnt2[s[pos]] = max(cnt2[s[pos]], cnt[s[pos]] + (int)s.size() - pos);
    if (len1 == s.size()) {
      maxi(cnt2[c1], len1 * (cnt[c1] + 1) + cnt[c1]);
    } else if (c1 == c2) {
      if (cnt[c1] != 0) {
        maxi(cnt2[c1], 1 + len1 + len2);
      } else {
        maxi(cnt2[c1], len1 + len2);
      }
    } else {
      if (cnt[c1] != 0) {
        maxi(cnt2[c1], 1 + len1);
      } else {
        maxi(cnt2[c1], len1);
      }
      if (cnt[c2] != 0) {
        maxi(cnt2[c2], 1 + len1);
      } else {
        maxi(cnt2[c2], len2);
      }
    }
    for (int i = 0; i < 256; i++) {
      cnt[i] = cnt2[i];
    }
  }
  int res = 0;
  for (int i = 0; i < 256; i++) {
    res = max(cnt[i], res);
  }
  cout << res;
  return 0;
}
