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 java.util.*;
public class Beautify{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=Integer.parseInt(sc.nextLine());
        String s=sc.nextLine();
        int arr[]=new int[26];
        int i;int count=1;
        for(i=0;i<s.length();i++){
            char ch=s.charAt(i);
            if(i!=s.length()-1&&ch==s.charAt(i+1)){
                count++;
            }
            else{
                if(arr[ch-'a']<count)
                arr[ch-'a']=count;
                count=1;
            }
        }
        int max,fcount,lcount,icount=0;
        char fch='a',lch='a';
        for(int j=1;j<n;j++){
            s=sc.nextLine();
            count=1;max=0;fcount=0;lcount=0;icount=0;
            boolean fflag=false;
            fch=s.charAt(0);
            lch=s.charAt(s.length()-1);
            int fvalue=arr[fch-'a'];
            int lvalue=arr[lch-'a'];
            for(i=1;i<s.length();i++){
                if(s.charAt(i-1)==s.charAt(i))
                    count++;
                else{
                    if(fflag==false){
                        fcount=count;
                        fflag=true;
                    }
                    if(max<count){
                    max=count;}
                    if(arr[s.charAt(i-1)-'a']<count){
                        arr[s.charAt(i-1)-'a']=count;
                    }
                    count=1;
                }
            }
            lcount=count;
            icount=max;
            arr[fch-'a']=fvalue;
            arr[lch-'a']=lvalue;
            int sum=lcount+fcount;
            if(sum==s.length()&&lch==fch){
                arr[lch-'a']+=(arr[lch-'a']+1)*sum;
            }
            else{
                if(lch==fch){
                    if(arr[lch-'a']!=0)
                    { 
                        arr[lch-'a']=1+lcount+fcount;}
                    else
                    arr[lch-'a']=Math.max(lcount,fcount);
                }
                else{
                    arr[lch-'a']=(arr[lch-'a']>0?1:0)+lcount;
                    arr[fch-'a']=(arr[fch-'a']>0?1:0)+fcount;
                }
            }
        }
        System.out.println(Math.max(icount,Math.max(arr[fch-'a'],arr[lch-'a'])));
    }
}
        