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() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<string> str(n);
  for (auto& s : str) cin >> s;
  vector<vector<long long>> dp(n, vector<long long>(26, 0));
  vector<vector<int>> best(n, vector<int>(26, 0));
  vector<int> pref(n, 0);
  vector<int> suff(n, 0);
  for (int i = 0; i < n; ++i) {
    while (pref[i] < str[i].size() && str[i][pref[i]] == str[i][0]) ++pref[i];
    while (suff[i] < str[i].size() &&
           str[i][str[i].size() - suff[i] - 1] == str[i][str[i].size() - 1])
      ++suff[i];
    int j = 0;
    while (j < str[i].size()) {
      int k = j;
      while (k < str[i].size() && str[i][j] == str[i][k]) ++k;
      best[i][str[i][j] - 'a'] = max(best[i][str[i][j] - 'a'], k - j);
      j = k;
    }
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 26; ++j) {
      dp[i][j] = best[i][j];
      if (i == 0) continue;
      if (best[i][j] != str[i].size()) {
        if (dp[i - 1][j] > 0) {
          long long cur = 1;
          if (str[i][0] - 'a' == j) cur += pref[i];
          if (str[i][str[i].size() - 1] - 'a' == j) cur += suff[i];
          dp[i][j] = max(dp[i][j], cur);
        }
      } else {
        dp[i][j] = max(dp[i][j], dp[i - 1][j] + (long long)str[i].size() *
                                                    (dp[i - 1][j] + 1));
        dp[i][j] = min(dp[i][j], (long long)1e9);
      }
    }
  long long res = -1;
  for (int i = 0; i < 26; ++i) res = max(res, dp[n - 1][i]);
  cout << res << endl;
  return 0;
}
