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
import os
from io import BytesIO
#input = BytesIO(os.read(0, os.fstat(0).st_size)).readline

from collections import namedtuple
Parsed = namedtuple("Parsed", "type p pl s sl")
D, U = 0, 1

def parse(s):
    pc, sc = 0, 0

    for c in s:
        if c != s[0]:
            break
        pc += 1

    for c in reversed(s):
        if c != s[-1]:
            break
        sc += 1


    if s[0] == s[-1] and pc == sc == len(s):
        tp = U
    else:
        tp = D

    return Parsed(tp, s[0], pc, s[-1], sc)

def max_conti_len(s, target):
    mx = 0
    cur = 0
    for c in s:
        if c == target:
            cur += 1
            mx = max(mx, cur)
    return mx

def len_mul(nl, ol):
    return ol*nl + ol + nl

def solve(n, ss):
    s = ss.pop()

    op = parse(s)

    mc = 0

    while ss:
        s = ss.pop()
        np = parse(s)

        if np.type == U and op.type == U:
            if np.p == op.p:
                nl = len_mul(np.pl, op.pl)
                op = Parsed(U, op.p, nl, op.s, nl)
            else:
                op = Parsed(D, op.p, op.pl, op.s, op.sl)
            mc = max(mc, op.pl)

        elif np.type == D and op.type == U:
            npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl
            nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl

            op = Parsed(D, op.p, npl, op.s, nsl)

            mx = max_conti_len(s, op.s)
            mc = max(mc, len_mul(mx, op.pl))

        elif op.type == D:
            if op.p == op.s:
                mp = op.pl+op.sl+1 if op.p in s else op.pl
                ms = 0
            else:
                mp = op.pl+1 if op.p in s else op.pl
                ms = op.sl+1 if op.s in s else op.sl
            mc = max(mc, mp, ms)

    print(mc)


def solve_from_stdin():
    n = int(input())
    ss = []
    for _ in range(n):
        ss.append(input())
    solve(n, ss)

solve_from_stdin()