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 main() {
  long long n;
  cin >> n;
  vector<string> p(n + 1);
  vector<long long> dp[n + 1];
  vector<long long> freq(256, 0);
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    cin >> p[i];
    for (char c : p[i]) freq[c]++;
  }
  long long ans = 0;
  for (long long c = (long long)('a'); c <= (long long)('z'); c++) {
    string last = p[n];
    long long cnt = 0, pre = 0, suf = 0;
    for (char x : last) {
      if (x == c) cnt++;
      dp[n].push_back(0);
    }
    if (cnt == 0) {
      long long val = (freq[c] > 0);
      ans = max(ans, val);
      continue;
    }
    for (long long i = (long long)(0); i <= (long long)(last.size() - 1); i++) {
      if (last[i] != c) break;
      pre++;
    }
    for (long long i = (long long)(last.size() - 1); i >= (long long)(0); i--) {
      if (last[i] != c) break;
      suf++;
    }
    if (last[0] != c && last.back() != c) {
      long long maxval = 0, val = 0;
      for (char x : last) {
        if (x != c)
          val = 0;
        else {
          val++;
          maxval = max(maxval, val);
        }
      }
      ans = max(ans, maxval);
      continue;
    }
    if (last.back() == c) {
      long long left = freq[c] - cnt;
      ans = max(ans, suf + (left > 0));
    }
    if (last[0] == c) {
      long long left = freq[c] - cnt;
      ans = max(ans, pre + (left > 0));
    }
    if (last[0] == c && last.back() == c) {
      long long left = freq[c] - cnt;
      if (left > 0) ans = max(ans, suf + pre + 1);
    }
    if (cnt != last.size()) continue;
    if (n > 1) {
      string cur = p[n - 1];
      long long val = 0;
      for (long long i = (long long)(0); i <= (long long)(cur.size() - 1);
           i++) {
        if (cur[i] != c) {
          val = 0;
        } else {
          val += cnt + 1;
          ans = max(ans, val + cnt);
        }
      }
    }
    long long maxVal = 0;
    for (long long lvl = (long long)(n - 1); lvl >= (long long)(1); lvl--) {
      dp[lvl].clear();
      for (long long i = (long long)(0); i <= (long long)(p[lvl].size() - 1);
           i++) {
        long long val = 0;
        if (p[lvl][i] == c) {
          val = 1 + dp[lvl + 1][p[lvl + 1].size() - 1];
        }
        dp[lvl].push_back(val);
        if (val) maxVal = max(maxVal, 1 + dp[lvl + 1][p[lvl + 1].size() - 1]);
      }
    }
    ans = max(ans, (maxVal + 1) * cnt + maxVal);
  }
  cout << ans;
}
