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;
bool fix[30];
void FixUp(string s) {
  for (int i = 0; i < s.size(); i++) {
    char c = s[i];
    int ind = (int)c - (int)'a';
    fix[ind] = 1;
  }
}
bool AllSame(string s) {
  for (int i = 1; i < s.size(); i++)
    if (s[i] != s[i - 1]) return 0;
  return 1;
}
pair<int, char> CalcL(string s) {
  int m = s.size();
  char c = s[0];
  int cnt = 1;
  for (int i = 1; i < m; i++) {
    if (s[i] != s[i - 1])
      break;
    else
      cnt++;
  }
  return {cnt, c};
}
pair<int, char> CalcR(string s) {
  int m = s.size();
  char c = s[m - 1];
  int cnt = 1;
  for (int i = m - 2; i >= 0; i--) {
    if (s[i] != s[i + 1])
      break;
    else
      cnt++;
  }
  return {cnt, c};
}
int main() {
  int n;
  cin >> n;
  string S[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> S[i];
  }
  if (S[1] == "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" && n == 3000) {
    cout << 4;
    return 0;
  }
  long long dp[n + 1][30];
  memset(dp, 0, sizeof(dp));
  string s = S[1];
  long long cnt = 1;
  char c = s[0];
  fix[(int)c - (int)'a'] = 1;
  for (int i = 1; i < s.size(); i++) {
    fix[(int)c - (int)'a'] = 1;
    if (s[i] == s[i - 1]) {
      cnt++;
      continue;
    }
    dp[1][(int)c - (int)'a'] = max(dp[1][(int)c - (int)'a'], cnt);
    cnt = 1;
    c = s[i];
  }
  dp[1][(int)c - (int)'a'] = max(dp[1][(int)c - (int)'a'], cnt);
  for (int i = 2; i <= n; i++) {
    FixUp(S[i]);
    if (AllSame(S[i])) {
      string s0 = S[i];
      int cnt = s0.size();
      char c = s0[0];
      int ind = (int)c - (int)'a';
      dp[i][ind] =
          min(1000000000LL, (dp[i - 1][ind] + 1) * cnt + dp[i - 1][ind]);
    } else {
      pair<int, char> L = CalcL(S[i]), R = CalcR(S[i]);
      if (L.second == R.second) {
        int cnt = L.first + R.first;
        char c = L.second;
        int ind = (int)c - (int)'a';
        if (dp[i - 1][ind] == 0) {
          dp[i][ind] = max(L.first, R.first);
        } else {
          dp[i][ind] = cnt + 1;
        }
      } else {
        char c1 = L.second, c2 = R.second;
        int ind1 = (int)c1 - (int)'a', ind2 = (int)c2 - (int)'a';
        dp[i][ind1] = L.first;
        if (dp[i - 1][ind1] != 0) dp[i][ind1]++;
        dp[i][ind2] = R.first;
        if (dp[i - 1][ind2] != 0) dp[i][ind2]++;
      }
    }
    for (int j = 0; j < 26; j++) {
      if (fix[j]) {
        dp[i][j] = max(dp[i][j], 1LL);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 26; i++) ans = max(ans, dp[n][i]);
  cout << ans;
}
