#include <bits/stdc++.h>
const long long N = 2e5 + 7, mod = 1e9 + 7, mod2 = mod * mod;
typedef long long aN[N];
char s[N];
long long f[N][27];
void qmax(long long& a, long long b) {
  if (a < b) a = b;
}
long long readll() {
  long long x = 0, w = 1;
  int c = getchar();
  for (; c < '0' || c > '9'; c - '-' || (w = -w), c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = x * 10 + (c ^ 48), c = getchar())
    ;
  return x * w;
}
int readchar(int l = '0', int r = 'z') {
  int c = getchar();
  for (; c < l || c > r; c = getchar())
    ;
  return c;
}
int main() {
  long long n = readll();
  for (register long long i = 1; i <= 26; i++) f[0][i] = 0;
  for (register long long t = 1; t <= n; t++) {
    long long* now = f[t];
    const long long* lst = f[t - 1];
    scanf("%s", s + 1);
    long long m = strlen(s + 1), fn = 0, ln = 0, fc = s[1] - 96, lc = s[m] - 96;
    long long ls = 0;
    for (register long long i = 1; i <= m; i++)
      if (s[i + 1] != s[i]) {
        qmax(now[s[i] - 96], i - ls), fn = fn ?: i - ls, ln = i - ls, ls = i;
      }
    for (register long long i = 1; i <= 26; i++) qmax(now[i], !!lst[i]);
    if (lst[fc]) qmax(now[fc], 1 + fn);
    if (lst[lc]) qmax(now[lc], 1 + ln);
    if (fc == lc && lst[fc]) qmax(now[fc], fn + ln + 1);
    if (fn == m) qmax(now[fc], m * (lst[fc] + 1) + lst[fc]);
  }
  long long ans = 0;
  for (register long long i = 1; i <= 26; i++) ans = std::max(ans, f[n][i]);
  printf("%lld\n", ans);
  return 0;
}
