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 int N = 1e5 + 5, D = 26;
string s[N];
int dp[N][D];
void upd(int &a, int b) { a = max(a, b); }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (char &c : s[i]) c -= 'a';
  }
  for (int k = 0; k < n; k++) {
    int len = s[k].size();
    for (int i = 0, j = -1; i < len; i++) {
      if (i + 1 == len || s[k][i + 1] != s[k][i]) {
        upd(dp[k][s[k][i]], i - j);
        j = i;
      }
    }
    if (k > 0) {
      for (int i = 0; i < D; i++) {
        if (dp[k - 1][i] != 0) upd(dp[k][i], 1);
      }
      int x = 0, y = n - 1;
      while (x + 1 < len && s[k][x + 1] == s[k][x]) x++;
      while (y - 1 >= 0 && s[k][y - 1] == s[k][y]) y--;
      if (x == len - 1) {
        upd(dp[k][s[k][0]],
            len * (dp[k - 1][s[k][0]] + 1) + dp[k - 1][s[k][0]]);
      } else if (s[k].front() == s[k].back()) {
        if (dp[k - 1][s[k][0]] != 0) upd(dp[k][s[k][0]], x + 1 + len - y + 1);
      } else {
        if (dp[k - 1][s[k][0]] != 0) upd(dp[k][s[k][0]], x + 2);
        if (dp[k - 1][s[k][len - 1]] != 0)
          upd(dp[k][s[k][len - 1]], len - y + 1);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < D; i++) {
    ans = max(ans, dp[n - 1][i]);
  }
  cout << ans;
  return 0;
}
