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 MOD = 998244353;
const double INF = 1e18;
const int N = 100010;
string s[N];
int n, dp[N][3], m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = (1); i <= (n); ++i) cin >> s[i];
  bool allsame = true, allsameLast;
  m = s[n].size();
  char beg = s[n][0], end = s[n][m - 1];
  dp[n][0] = dp[n][1] = dp[n][2] = 1;
  for (int j = (1); j <= (m - 1); ++j)
    if (s[n][j] != s[n][j - 1])
      break;
    else
      ++dp[n][0];
  for (int j = m - 2; j >= 0; --j)
    if (s[n][j] != s[n][j + 1])
      break;
    else
      ++dp[n][0];
  int cnt = 1;
  for (int j = (1); j <= (m - 1); ++j)
    if (s[n][j] == s[n][j - 1]) {
      ++cnt;
      dp[n][1] = max(dp[n][1], cnt);
    } else
      cnt = 1;
  if (dp[n][0] < m) allsame = false;
  for (int i = n - 1; i; --i) {
    m = s[i].size();
    allsameLast = allsame;
    for (int j = (1); j <= (m - 1); ++j)
      if (s[i][j] != s[i][0]) allsame = false;
    if (s[i][0] != beg) allsame = false;
    int x = dp[i + 1][0], y = dp[i + 1][1], z = dp[i + 1][2];
    if (allsame) {
      dp[i][0] = dp[i][1] = dp[i][2] = y * (m + 1) + m;
      continue;
    }
    if (allsameLast) {
      int numbeg = 0, numend = 0, nummid = 0, cnt = 0;
      for (int j = (0); j <= (m - 1); ++j)
        if (s[i][j] == beg)
          numbeg++;
        else
          break;
      for (int j = m - 1; j >= 0; --j)
        if (s[i][j] == beg)
          numend++;
        else
          break;
      for (int j = (0); j <= (m - 1); ++j)
        if (s[i][j] == beg) {
          ++cnt;
          nummid = max(nummid, cnt);
        } else
          cnt = 0;
      dp[i][0] = x * (numbeg + 1) + numbeg;
      dp[i][1] = y * (nummid + 1) + nummid;
      dp[i][2] = z * (numend + 1) + numend;
    } else {
      dp[i][0] = x;
      dp[i][2] = z;
      dp[i][1] = y;
      for (int j = (0); j <= (m - 1); ++j)
        dp[i][1] = max(dp[i][1],
                       (end == s[i][j] ? z : 0) + 1 + (beg == s[i][j] ? x : 0));
    }
  }
  int ans = 0;
  for (int i = (1); i <= (n); ++i)
    for (int j = (0); j <= (2); ++j) ans = max(ans, dp[i][j]);
  cout << ans << endl;
  return 0;
}
