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;
int Read() {
  int p = 0, f = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-');
  for (; c >= '0' && c <= '9'; c = getchar()) p = p * 10 + c - '0';
  if (f) p = -p;
  return p;
}
string s[100005];
bool a[100005][26], b[100005];
long long dfs(int x, char c) {
  if (x == 0) return 0;
  int ans = a[x - 1][c - 'a'], sz = s[x].size();
  if (b[x] && s[x][0] == c) {
    ans = dfs(x - 1, c);
    return (ans + 1) * sz + ans;
  } else {
    int q = 0, h = 0, last = -1;
    for (int i = 0; i < sz; ++i) {
      if (s[x][i] == c && last == -1) last = i;
      if (s[x][i] == c)
        ans = max(ans, i - last + 1);
      else
        last = -1;
    }
    for (int i = 0; i < sz; ++i) {
      if (s[x][i] == c)
        q++;
      else
        break;
    }
    for (int i = sz - 1; ~i; --i) {
      if (s[x][i] == c)
        h++;
      else
        break;
    }
    if (s[x][0] == c && s[x - 1][sz - 1] == s[x][0] && a[x - 1][c - 'a'])
      ans = max(ans, q + h + 1);
    if (s[x][0] == c && a[x - 1][c - 'a']) ans = max(ans, q + 1);
    if (s[x][sz - 1] == c && a[x - 1][c - 'a']) ans = max(ans, h + 1);
    if (s[x][0] == c) ans = max(ans, q);
    if (s[x][sz - 1] == c) ans = max(ans, h);
    return ans;
  }
}
int main() {
  int n = Read();
  for (int i = 1; i <= n; i++) cin >> s[i], b[i] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < (int)s[i].size(); j++)
      if (s[i][j] != s[i][j - 1]) {
        b[i] = 0;
        break;
      }
    for (int j = 0; j < (int)s[i].size(); j++) a[i][s[i][j] - 'a'] = 1;
    for (int j = 0; j < 25; ++j) a[i][j] |= a[i - 1][j];
  }
  int ans = 1, q = 1, h = 1, last = 0, sz = s[n].size();
  for (int i = 1; i < sz; i++) {
    if (last == 0 && s[n][i] == s[n][0]) q++;
    if (s[n][i] == s[n][last])
      ans = max(ans, i - last + 1);
    else
      last = i;
  }
  for (int i = sz - 2; i >= 0; i--)
    if (s[n][i] == s[n][sz - 1])
      h++;
    else
      break;
  if (!b[n]) {
    if (s[n][0] == s[n][sz - 1] && a[n - 1][s[n][0] - 'a'])
      ans = max(ans, q + h + 1);
    if (a[n - 1][s[n][0] - 'a']) ans = max(ans, q + 1);
    if (a[n - 1][s[n][sz - 1] - 'a']) ans = max(ans, h + 1);
    ans = max(ans, max(q, h));
    cout << ans;
    return 0;
  }
  int t = dfs(n - 1, s[n][0]);
  cout << (t + 1) * sz + t;
}
