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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define ll long long int 

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    
    ll n;
    cin>>n;
    
    vector<string> p(n+1);
    vector<ll> cnt(26,0);
    for(ll i=1;i<=n;i++){
        cin>>p[i];
        for(auto it : p[i]){
            cnt[it-'a']++;
        }
    }
    
    ll st=0;
    ll en=p[n].length()-1;
    
    while(st<=p[n].length()-1 && p[n][st]==p[n][0]){
        st++;
    }
    
    while(en>=0 && p[n][en]==p[n][p[n].length()-1]){
        en--;
    }
    
    ll Max=0;
    for(ll i=0;i<p[n].length();i++){
        ll j=i;
        while(j<p[n].length() && p[n][j]==p[n][i]){
            cnt[p[n][j]-'a']--;
            j++;
        }
        
        Max=max(Max,j-i);
        i=j-1;
    }
    
    ll ans=1;
    // middle substring
    ans=max(Max,ans);
    
    // prefix substring
    if(cnt[p[n][0]-'a']){
        ans=max(ans,st+1);
    }
    else{
        ans=max(ans,st);
    }
    
    // suffix substring
    if(cnt[p[n][p[n].length()-1]-'a'] && p[n].length()-1-en+1>ans){
        ans=p[n].length()-1-en+1;
    }
    else if(p[n].length()-1-en>ans){
        ans=p[n].length()-1-en;
    }
    
    // prefix+suffix possibility
    if(p[n][0]==p[n][p[n].length()-1] && en!=-1){
        if(cnt[p[n][0]-'a'] && st+1+p[n].length()-1-en>ans){
            ans=st+1+p[n].length()-1-en;
        }
        else if(st+p[n].length()-1-en>ans){
            ans=st+p[n].length()-1-en;
        }
    }
    
    // all char same
    if(st==p[n].length()){
        ll i=n-1;
        char ch=p[n][0];
        
        ll len=p[n].length();
        while(i>=1){
            ll j=0;
            while(j<p[i].length() && p[i][j]==ch){
                j++;
            }
            
            if(j!=p[i].length()){
                break;
            }
            
            len=len*(p[i].length()+1);
            len+=p[i].length();
            
            cnt[p[n][0]-'a']-=p[i].length();
            i--;
        }
        
        if(i==0){
            cout<<len<<"\n";
            return 0;
        }
        else{
            // find max consecutive chars equal to ch
            ll Max=0;
            for(ll i1=0;i1<p[i].length();i1++){
                ll j=i1;
                while(j<p[i].length() && p[i][j]==ch){
                    cnt[p[i][j]-'a']--;
                    j++;
                }
                
                Max=max(Max,j-i1);
                cnt[p[i][j]-'a']--;
                i1=j;
            }
            
            ans=max(ans,((Max+1)*len)+Max);
            
            // find max prefix chars equal to ch
            
            ll st=0;
            ll en=p[i].length()-1;
            
            while(st<=p[i].length()-1 && p[i][st]==ch){
                st++;
            }
            
            if(cnt[ch-'a'] && (st+1)*len+st+1>ans){
                ans=(st+1)*len+st+1;
            }
            else if((st+1)*len+st>ans){
                ans=(st+1)*len+st;
            }
            
            // find max suffix of chars equal to ch
            while(en>=0 && p[i][en]==ch){
                en--;
            }
            
            if(cnt[ch-'a'] && (p[i].length()-1-en+1)*len+(p[i].length()-1-en)+1>ans){
                ans=(p[i].length()-1-en+1)*len+(p[i].length()-1-en)+1;
            }
            else if((p[i].length()-1-en+1)*len+(p[i].length()-1-en)>ans){
                ans=(p[i].length()-1-en+1)*len+(p[i].length()-1-en);
            }
        }
    }
    
    cout<<ans<<"\n";
}






