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 = 1e9 + 9;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<string> s(n);
  for (long long i = (long long)(0); i < (long long)(n); ++i) cin >> s[i];
  vector<pair<long long, long long> > ch(n);
  vector<long long> best(n, 0);
  vector<bool> is(n, 0);
  for (long long i = (long long)(0); i < (long long)(n); ++i) {
    for (long long j = (long long)(0); j < (long long)(s[i].size() + 1); ++j) {
      if (j >= s[i].size() || s[i][j] != s[i][0]) {
        ch[i].first = j;
        break;
      }
    }
    for (long long j = s[i].size() - 1; j >= -1; --j) {
      if (j < 0 || s[i][j] != s[i].back()) {
        ch[i].second = s[i].size() - j - 1;
        break;
      }
    }
    long long last = 0;
    for (long long j = (long long)(0); j < (long long)(s[i].size() + 1); ++j) {
      if (j >= s[i].size() || s[i][last] != s[i][j]) {
        best[i] = max(best[i], j - last);
        last = j;
      }
    }
    is[i] = (best[i] == s[i].size());
  }
  vector<long long> dp(n, 0);
  vector<pair<long long, long long> > cdp(n), sym(n);
  dp[n - 1] = best[n - 1];
  cdp[n - 1] = ch[n - 1];
  sym[n - 1] = {s[n - 1][0], s[n - 1].back()};
  for (long long i = n - 2; i >= 0; --i) {
    dp[i] = dp[i + 1];
    long long cur = 0;
    cdp[i] = cdp[i + 1];
    bool going = 1;
    for (long long j = (long long)(0); j < (long long)(s[i].size()); ++j) {
      if (is[i + 1] && s[i][j] == sym[i + 1].second) {
        cur += dp[i + 1];
        if (cur > dp[i]) dp[i] = cur;
        if (going) cdp[i].first = cur;
      } else {
        going = 0;
        cur = 1;
        if (sym[i + 1].second == s[i][j]) cur += cdp[i + 1].second;
        if (cur > dp[i]) dp[i] = cur;
      }
    }
    if (going) cdp[i].first = s[i].size() + dp[i + 1];
    for (long long j = s[i].size() - 1; j >= 0; --j) {
      if (is[i + 1] && s[i][j] == sym[i + 1].second)
        cdp[i].second =
            (s[i].size() - 1 - j + 2) * dp[i + 1] + (s[i].size() - 1 - j + 1);
    }
    if (s[i].back() == sym[i + 1].first) {
      cur += cdp[i + 1].first;
      if (cur > dp[i]) dp[i] = cur;
    }
    is[i] = (dp[i] == s[i].size() + dp[i + 1]);
    sym[i] = sym[i + 1];
  }
  cout << dp[0];
  return 0;
}
