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;
struct Table {
  int left[26];
  int right[26];
  int inner[26];
  int length;
};
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  Table last;
  memset(&last, 0, sizeof(last));
  string lastS = "A";
  for (int i = 0; i < n; i++) {
    Table cur;
    memset(&cur, 0, sizeof(cur));
    string s;
    cin >> s;
    {
      char b = s.front() - 'a';
      char e = s.back() - 'a';
      for (char ch : s) {
        int code = ch - 'a';
        if (code != b) {
          break;
        }
        cur.left[b]++;
      }
      for (int i = (int)s.length() - 1; i >= 0; i--) {
        int code = s[i] - 'a';
        if (code != e) {
          break;
        }
        cur.right[e]++;
      }
      int cch = s.front() - 'a';
      int k = 1;
      for (int i = 1; i < (int)s.length(); i++) {
        int code = s[i] - 'a';
        if (code != cch) {
          cur.inner[cch] = max(cur.inner[cch], k);
          k = 0;
          cch = code;
        }
        k++;
      }
      cur.inner[s.back() - 'a'] = max(cur.inner[s.back() - 'a'], k);
      cur.length = s.length();
    }
    {
      Table next;
      memset(&next, 0, sizeof(next));
      for (int i = 0; i < 26; i++) {
        if (cur.length == cur.inner[i]) {
          next.left[i] = last.left[i] * (cur.length + 1) + cur.length;
        } else {
          next.left[i] = cur.left[i];
        }
      }
      for (int i = 0; i < 26; i++) {
        if (cur.length == cur.inner[i]) {
          next.right[i] = last.right[i] * (cur.length + 1) + cur.length;
        } else {
          next.right[i] = cur.right[i];
        }
      }
      for (int i = 0; i < 26; i++) {
        if (cur.length == cur.inner[i]) {
          next.inner[i] = last.inner[i] * (cur.length + 1) + cur.length;
        } else {
          next.inner[i] = max(cur.inner[i], (int)(bool)last.inner[i]);
          next.inner[i] = max(next.inner[i], next.left[i]);
          next.inner[i] = max(next.inner[i], next.right[i]);
          if (last.inner[i] > 0) {
            next.inner[i] = max(next.inner[i], cur.right[i] + cur.left[i] + 1);
          }
        }
      }
      next.length = last.length * (cur.length + 1) + cur.length;
      lastS = s;
      last = next;
    }
  }
  int res = 0;
  for (int i = 0; i < 26; i++) {
    res = max(res, last.inner[i]);
  }
  cout << res << "\n";
  return 0;
}
