#include <bits/stdc++.h>
using namespace std;
int tong[30], dd[30], ans, n;
char s[150009];
inline long long rd() {
  long long x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f ? -x : x;
}
int main() {
  n = rd();
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    int len = strlen(s);
    int l = 1, r = 1, now = 1, nowans = 1;
    memset(dd, 0, sizeof(dd));
    for (int j = 1; j < len; ++j)
      if (s[j] == s[j - 1])
        l++;
      else
        break;
    for (int j = len - 2; j >= 0; --j)
      if (s[j] == s[j + 1])
        r++;
      else
        break;
    for (int j = 1; j < len; ++j) {
      if (s[j] == s[j - 1])
        now++;
      else {
        nowans = max(nowans, now);
        dd[s[j - 1] - 'a'] = max(dd[s[j - 1] - 'a'], now);
        now = 1;
      }
    }
    dd[s[len - 1] - 'a'] = max(dd[s[len - 1] - 'a'], now);
    nowans = max(nowans, now);
    if (nowans == len) {
      int x = s[0] - 'a';
      dd[x] = max(dd[x], (tong[x] + 1) * len + tong[x]);
    } else if (s[0] == s[len - 1]) {
      if (tong[s[0] - 'a']) dd[s[0] - 'a'] = max(dd[s[0] - 'a'], l + r + 1);
    } else {
      if (tong[s[0] - 'a']) dd[s[0] - 'a'] = max(dd[s[0] - 'a'], l + 1);
      if (tong[s[len - 1] - 'a'])
        dd[s[len - 1] - 'a'] = max(dd[s[len - 1] - 'a'], r + 1);
    }
    for (int j = 0; j < 26; ++j) tong[j] = (tong[j] != 0);
    for (int j = 0; j < 26; ++j) tong[j] = max(tong[j], dd[j]);
    for (int j = 0; j < len; ++j) s[j] = 0;
  }
  for (int i = 0; i < 26; ++i) ans = max(ans, tong[i]);
  cout << ans;
  return 0;
}
