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 long long INF = 1e18;
const long long MAXN = 1e5 + 5;
const long long MOD = 1e9 + 7;
int n;
long long len[125], len2[125];
string s[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> s[i];
  fill(len + 'a', len + 'z' + 1, 0);
  long long k = 1;
  for (int i = 1; i < s[0].size(); ++i) {
    if (s[0][i] == s[0][i - 1])
      ++k;
    else {
      len[s[0][i - 1]] = max(len[s[0][i - 1]], k);
      k = 1;
    }
  }
  len[s[0][s[0].size() - 1]] = max(len[s[0][s[0].size() - 1]], k);
  for (int j = 1; j < n; ++j) {
    for (int i = 'a'; i <= 'z'; ++i) len2[i] = min((long long)1, len[i]);
    k = 1;
    for (int i = 1; i < s[j].size(); ++i) {
      if (s[j][i] == s[j][i - 1])
        ++k;
      else {
        len2[s[j][i - 1]] = max(len2[s[j][i - 1]], k);
        k = 1;
      }
    }
    len2[s[j][s[j].size() - 1]] = max(len2[s[j][s[j].size() - 1]], k);
    if (k == s[j].size() && len[s[j][0]] > 0) {
      len2[s[j][0]] = s[j].size() * (len[s[j][0]] + 1) + len[s[j][0]];
    } else {
      int i = 1;
      while (i < s[j].size() && s[j][i] == s[j][0]) ++i;
      k = i;
      i = s[j].size() - 2;
      while (i >= 0 && s[j][i] == s[j][s[j].size() - 1]) --i;
      long long k2 = s[j].size() - 1 - i;
      if (s[j][0] == s[j][s[j].size() - 1] && len[s[j][0]] > 0)
        len2[s[j][0]] = max(len2[s[j][0]], k + k2 + 1);
      else {
        if (len[s[j][0]] > 0) len2[s[j][0]] = max(len2[s[j][0]], k + 1);
        if (len[s[j][s[j].size() - 1]] > 0)
          len2[s[j][s[j].size() - 1]] =
              max(len2[s[j][s[j].size() - 1]], k2 + 1);
      }
    }
    copy(len2 + 'a', len2 + 'z' + 1, len + 'a');
  }
  k = 0;
  for (int i = 'a'; i <= 'z'; ++i) k = max(k, len[i]);
  cout << k;
  return 0;
}
