#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

int solve(int n,char ch)
{

    if(n==1)
    {
        return (getcharmax(s[n],ch)+1)*sz(s[n+1]);
    }
    int mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0,m4=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
        bool p5= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p6= isfind(1,n-1,s[n][0]);
        if(p5)
        {
            m3= p3.first+1;
        }
        if(p6)
        {
            m4=p2.first+1;
        }
        mx= max(m1,max(m2,max(m3,m4)));

    if(p1.first==1&&p1.second==ch)
    {
          mx=max(mx,(solve(n-1,ch)));
    }
    return (mx+1)*sz(s[n+1]);
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0,m3=0,m4=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }
        bool p4= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p5= isfind(1,n-1,s[n][0]);
        if(p4)
        {
            m3= p2.first+1;
        }
        if(p5)
        {
            m4=p1.first+1;
        }

        int mx=max(m1,max(m2,max(m3,m4)));

        if(p.first==0)
        {

          cout<<mx<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
  //  cout<<m1<<" "<<m2<<endl;
    ll ans= max(mx,(cnt));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

int solve(int n,char ch)
{

    if(n==1)
    {
        return getcharmax(s[n],ch);
    }
    int mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
       // cout<<m1<<" "<<m2<<" "<<m3<<endl;
        mx= max(m1,max(m2,m3));

    if(p1.first==1&&p1.second==ch)
    {
          mx=max(mx,(solve(n-1,ch)+1));
    }
    return mx*sz(s[n]);
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }

        if(p.first==0)
        {

          cout<<max(m1,m2)<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
  //  cout<<m1<<" "<<m2<<endl;
    ll ans= max(m1,max(m2,(cnt+1)*sz(s[n])));
    cout<<ans<<endl;
}


#include<iostream>
#include<vector>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cctype>
#include <stack>
#include <list>
#include <queue>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

//DOUBLE D UP
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define N 200005
#define pb push_back
#define F first
#define S second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define piii pair<pi,pi>
#define M 998244353
#define ld long double
#define inf (ll)10000004ll
using namespace std;
//0 1 1 0 1
//ecdcdcde

int dp[N][30],b[30];
int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output2.txt","w",stdout);
    #endif
    fast
    int n,i,j;

    cin>>n;

    for(i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        memset(b,0,sizeof(b));
        for(j=0;j<s.size();j++)
        {
           int c=s[j]-'a',k=j;
           while(k+1<s.size() && s[k]==s[k+1])
            k++;
           b[c] = k-j+1;
           j=k;
        }

        if(i>1)
        {
            int fl=0;
             for(j=0;j<26;j++)
                {
                    if(b[j]==s.size())
                    {
                        fl=1;
                    }
                }
            for(j=0;j<26;j++)
            {
                int pr=0;
                while(pr<s.size() && (s[pr]-'a')==j)
                    {
                        pr++;
                    }
                int suf=s.size()-1,jk=0;
                while(suf>=0 && (s[suf]-'a') == j)
                {
                    suf--;
                    jk++;
                }
                dp[i][j]=b[j];
                if(fl)
                {
                    dp[i][j]=max(dp[i][j],(dp[i-1][j]+1)*((int)s.size()) + dp[i-1][j]);
                }
                else if(dp[i-1][j])
                {
                   dp[i][j]=max(dp[i][j],1+pr+jk);
                }

            }
        }
    }
    int ans=0;
    for(j=0;j<26;j++)
    {
        ans=max(ans,dp[n][j]);
    }
    cout<<ans;
    return 0;

}


#include<iostream>
#include<vector>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cctype>
#include <stack>
#include <list>
#include <queue>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

//DOUBLE D UP
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define N 200005
#define pb push_back
#define F first
#define S second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define piii pair<pi,pi>
#define M 998244353
#define ld long double
#define inf (ll)10000004ll
using namespace std;
//0 1 1 0 1
//ecdcdcde

ll dp[N][30],b[30];
int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output2.txt","w",stdout);
    #endif
    fast
    ll n,i,j;

    cin>>n;

    for(i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        memset(b,0,sizeof(b));
        for(j=0;j<s.size();j++)
        {
           ll c=s[j]-'a',k=j;
           while(k+1<s.size() && s[k]==s[k+1])
            k++;
           b[c] = max(b[c],k-j+1);
           j=k;
        }
            ll fl=0;
            for(j=0;j<26;j++)
            {
                ll pr=0,fl=0;
                 if(b[j]==s.size())
                    {
                        fl=1;
                    }
                while(pr<s.size() && (s[pr]-'a')==j)
                    {
                        pr++;
                    }
                ll suf=s.size()-1,jk=0;
                while(suf>=0 && (s[suf]-'a') == j)
                {
                    suf--;
                    jk++;
                }
                dp[i][j]=b[j];
                if(i>1 && fl)
                {
                 //   cout<<i<<"*"<<" "<<j<<endl;
                    dp[i][j]=max(dp[i][j],(dp[i-1][j]+1)*((ll)s.size()) + dp[i-1][j]);
                }
                else if(i>1 && dp[i-1][j])
                {
                   dp[i][j]=max(dp[i][j],1+pr+jk);
                }
              //  if(i==2 && j==0)
                //    cout<<dp[i][j]<<"*"<<dp[i-1][j]<<endl;

            }
    }
    ll ans=0;
    for(j=0;j<26;j++)
    {
        ans=max(ans,dp[n][j]);
    }
    cout<<ans;
    return 0;

}


#include<iostream>
#include<vector>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cctype>
#include <stack>
#include <list>
#include <queue>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

//DOUBLE D UP
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define N 200005
#define pb push_back
#define F first
#define S second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define piii pair<pi,pi>
#define M 998244353
#define ld long double
#define inf (ll)10000004ll
using namespace std;
//0 1 1 0 1
//ecdcdcde

int dp[N][30],b[30];
int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output2.txt","w",stdout);
    #endif
    fast
    int n,i,j;

    cin>>n;

    for(i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        memset(b,0,sizeof(b));
        for(j=0;j<s.size();j++)
        {
           int c=s[j]-'a',k=j;
           while(k+1<s.size() && s[k]==s[k+1])
            k++;
           b[c] = k-j+1;
           j=k;
        }

        if(i>1)
        {
            int fl=0;
            for(j=0;j<26;j++)
            {
                int pr=0,fl=0;
                 if(b[j]==s.size())
                    {
                        fl=1;
                    }
                while(pr<s.size() && (s[pr]-'a')==j)
                    {
                        pr++;
                    }
                int suf=s.size()-1,jk=0;
                while(suf>=0 && (s[suf]-'a') == j)
                {
                    suf--;
                    jk++;
                }
                dp[i][j]=b[j];
                if(fl && dp[i-1][j])
                {
                 //   cout<<i<<"*"<<" "<<j<<endl;
                    dp[i][j]=max(dp[i][j],(dp[i-1][j]+1)*((int)s.size()) + dp[i-1][j]);
                }
                else if(dp[i-1][j])
                {
                   dp[i][j]=max(dp[i][j],1+pr+jk);
                }

            }
        }
    }
    int ans=0;
    for(j=0;j<26;j++)
    {
        ans=max(ans,dp[n][j]);
    }
    cout<<ans;
    return 0;

}


#include<iostream>
#include<vector>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cctype>
#include <stack>
#include <list>
#include <queue>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

//DOUBLE D UP
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define N 200005
#define pb push_back
#define F first
#define S second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define piii pair<pi,pi>
#define M 998244353
#define ld long double
#define inf (ll)10000004ll
using namespace std;
//0 1 1 0 1
//ecdcdcde

ll dp[N][30],b[30];
int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output2.txt","w",stdout);
    #endif
    fast
    ll n,i,j;

    cin>>n;

    for(i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        memset(b,0,sizeof(b));
        for(j=0;j<s.size();j++)
        {
           ll c=s[j]-'a',k=j;
           while(k+1<s.size() && s[k]==s[k+1])
            k++;
           b[c] = k-j+1;
           j=k;
        }
            ll fl=0;
            for(j=0;j<26;j++)
            {
                ll pr=0,fl=0;
                 if(b[j]==s.size())
                    {
                        fl=1;
                    }
                while(pr<s.size() && (s[pr]-'a')==j)
                    {
                        pr++;
                    }
                ll suf=s.size()-1,jk=0;
                while(suf>=0 && (s[suf]-'a') == j)
                {
                    suf--;
                    jk++;
                }
                dp[i][j]=b[j];
                if(i>1 && fl && dp[i-1][j])
                {
                 //   cout<<i<<"*"<<" "<<j<<endl;
                    dp[i][j]=max(dp[i][j],(dp[i-1][j]+1)*((ll)s.size()) + dp[i-1][j]);
                }
                else if(i>1 && dp[i-1][j])
                {
                   dp[i][j]=max(dp[i][j],1+pr+jk);
                }
              //  if(i==2 && j==0)
                //    cout<<dp[i][j]<<"*"<<dp[i-1][j]<<endl;

            }
    }
    ll ans=0;
    for(j=0;j<26;j++)
    {
        ans=max(ans,dp[n][j]);
    }
    cout<<ans;
    return 0;

}


#include<iostream>
#include<vector>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cctype>
#include <stack>
#include <list>
#include <queue>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

//DOUBLE D UP
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define N 200005
#define pb push_back
#define F first
#define S second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define piii pair<pi,pi>
#define M 998244353
#define ld long double
#define inf (ll)10000004ll
using namespace std;
//0 1 1 0 1
//ecdcdcde

int dp[N][30],b[30];
int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output2.txt","w",stdout);
    #endif
    fast
    int n,i,j;

    cin>>n;

    for(i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        memset(b,0,sizeof(b));
        for(j=0;j<s.size();j++)
        {
           int c=s[j]-'a',k=j;
           while(k+1<s.size() && s[k]==s[k+1])
            k++;
           b[c] = k-j+1;
           j=k;
        }
            int fl=0;
            for(j=0;j<26;j++)
            {
                int pr=0,fl=0;
                 if(b[j]==s.size())
                    {
                        fl=1;
                    }
                while(pr<s.size() && (s[pr]-'a')==j)
                    {
                        pr++;
                    }
                int suf=s.size()-1,jk=0;
                while(suf>=0 && (s[suf]-'a') == j)
                {
                    suf--;
                    jk++;
                }
                dp[i][j]=b[j];
                if(i>1 && fl && dp[i-1][j])
                {
                 //   cout<<i<<"*"<<" "<<j<<endl;
                    dp[i][j]=max(dp[i][j],(dp[i-1][j]+1)*((int)s.size()) + dp[i-1][j]);
                }
                else if(i>1 && dp[i-1][j])
                {
                   dp[i][j]=max(dp[i][j],1+pr+jk);
                }
              //  if(i==2 && j==0)
                //    cout<<dp[i][j]<<"*"<<dp[i-1][j]<<endl;

            }
    }
    int ans=0;
    for(j=0;j<26;j++)
    {
        ans=max(ans,dp[n][j]);
    }
    cout<<ans;
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 100005;

string s[MAXN];

int mark[MAXN][30];

int n;

ll res = 0;

ll calc(int h, char c)
{
	if (h == 0)
		return 0;
	bool ch = 1;
	for (auto x : s[h])
		ch &= (x == c);
	if (ch)
		return (calc(h - 1, c) + 1) * (s[h].size() + 1) - 1;
	else
	{
		int sz = s[h].size(), ret = 0, p, cur;
		for (int i = 0; i < sz; i++)
		{
			int j = i;
			while (j + 1 < sz && s[h][i] == s[h][j + 1])
				++j;
			if (s[h][i] == c)
				ret = max(ret, j - i + 1);
			i = j;
		}
		if (mark[h - 1][c - 'a'])
		{
			p = 0;
			while (p < sz && s[h][p] == c)
				++ret, ++p;
			p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++ret, --p;
			return ret + 1;
		}
		else
		{
			cur = 0, p = 0;
			while (p < sz && s[h][p] == c)
				++cur, ++p;
			ret = max(ret, cur);
			cur = 0, p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++cur, --p;
			ret = max(ret, cur);
			return ret;
		}
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> s[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 26; j++)
			mark[i][j] |= mark[i - 1][j];
		for (auto c : s[i])
			mark[i][c - 'a'] = 1;
	}
	for (char c = 'a'; c <= 'z'; c++)
		res = max(res, calc(n, c));
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

string s[MAXN];

int mark[MAXN][30];

int n, res = 0;

int calc(int h, char c)
{
	if (h == 0)
		return 0;
	bool ch = 1;
	for (auto x : s[h])
		ch &= x == c;
	if (ch)
		return (calc(h - 1, c) + 1) * (s[h].size() + 1) - 1;
	else
	{
		int sz = s[h].size(), ret = 0, p, cur;
		if (mark[h - 1][c - 'a'])
		{
			p = 0;
			while (p < sz && s[h][p] == c)
				++ret, ++p;
			p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++ret, --p;
			return ret + 1;
		}
		else
		{
			cur = 0, p = 0;
			while (p < sz && s[h][p] == c)
				++cur, ++p;
			ret = max(ret, cur);
			cur = 0, p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++cur, --p;
			ret = max(ret, cur);
			return ret;
		}
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> s[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 26; j++)
			mark[i][j] |= mark[i - 1][j];
		for (auto c : s[i])
			mark[i][c - 'a'] = 1;
	}
	for (char c = 'a'; c <= 'z'; c++)
		res = max(res, calc(n, c));
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 100005;

string s[MAXN];

int mark[MAXN][30];

int n;

ll res = 0;

ll calc(int h, char c)
{
	if (h == 0)
		return 0;
	bool ch = 1;
	for (auto x : s[h])
		ch &= x == c;
	if (ch)
		return (calc(h - 1, c) + 1) * (s[h].size() + 1) - 1;
	else
	{
		int sz = s[h].size(), ret = 0, p, cur;
		if (mark[h - 1][c - 'a'])
		{
			p = 0;
			while (p < sz && s[h][p] == c)
				++ret, ++p;
			p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++ret, --p;
			return ret + 1;
		}
		else
		{
			cur = 0, p = 0;
			while (p < sz && s[h][p] == c)
				++cur, ++p;
			ret = max(ret, cur);
			cur = 0, p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++cur, --p;
			ret = max(ret, cur);
			return ret;
		}
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> s[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 26; j++)
			mark[i][j] |= mark[i - 1][j];
		for (auto c : s[i])
			mark[i][c - 'a'] = 1;
	}
	for (char c = 'a'; c <= 'z'; c++)
		res = max(res, calc(n, c));
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 100005;

string s[MAXN];

int mark[MAXN][30];

int n;

ll res = 0;

ll calc(int h, char c)
{
	if (h == 0)
		return 0;
	bool ch = 1;
	for (auto x : s[h])
		ch &= (x == c);
	if (ch)
		return (calc(h - 1, c) + 1) * (s[h].size() + 1) - 1;
	else
	{
		int sz = s[h].size(), ret = 0, p, cur;
		for (int i = 0; i < sz; i++)
		{
			int j = i;
			while (j + 1 < sz && s[h][i] == s[h][j + 1])
				++j;
			if (s[h][i] == c)
				ret = max(ret, j - i + 1);
			i = j;
		}
		if (mark[h - 1][c - 'a'])
		{
			cur = 0;
			p = 0;
			while (p < sz && s[h][p] == c)
				++cur, ++p;
			p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++cur, --p;
			return max(cur + 1, ret);
		}
		return ret;
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> s[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 26; j++)
			mark[i][j] |= mark[i - 1][j];
		for (auto c : s[i])
			mark[i][c - 'a'] = 1;
	}
	for (char c = 'a'; c <= 'z'; c++)
		res = max(res, calc(n, c));
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 100005;

string s[MAXN];

int mark[MAXN][30];

int n;

ll res = 0;

ll calc(int h, char c)
{
	if (h == 0)
		return 0;
	bool ch = 1;
	for (auto x : s[h])
		ch &= (x == c);
	if (ch)
		return (calc(h - 1, c) + 1) * (s[h].size() + 1) - 1;
	else
	{
		int sz = s[h].size(), ret = 0, p, cur;
		if (mark[h - 1][c - 'a'])
		{
			p = 0;
			while (p < sz && s[h][p] == c)
				++ret, ++p;
			p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++ret, --p;
			return ret + 1;
		}
		else
		{
			cur = 0, p = 0;
			while (p < sz && s[h][p] == c)
				++cur, ++p;
			ret = max(ret, cur);
			cur = 0, p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++cur, --p;
			ret = max(ret, cur);
			return ret;
		}
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> s[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 26; j++)
			mark[i][j] |= mark[i - 1][j];
		for (auto c : s[i])
			mark[i][c - 'a'] = 1;
	}
	int sz = s[n].size();
	for (int i = 0; i < sz; i++)
	{
		int j = i;
		while (j + 1 < sz && s[n][i] == s[n][j + 1])
			++j;
		res = max(res, 1ll * j - i + 1);
		i = j;
	}
	for (char c = 'a'; c <= 'z'; c++)
		res = max(res, calc(n, c));
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 100005;

string s[MAXN];

int mark[MAXN][30];

int n;

ll res = 0;

ll calc(int h, char c)
{
	if (h == 0)
		return 0;
	bool ch = 1;
	for (auto x : s[h])
		ch &= (x == c);
	if (ch)
		return (calc(h - 1, c) + 1) * (s[h].size() + 1) - 1;
	else
	{
		int sz = s[h].size(), ret = 0, p, cur;
		for (int i = 0; i < sz; i++)
		{
			int j = i;
			while (j + 1 < sz && s[h][i] == s[h][j + 1])
				++j;
			if (s[h][i] == c)
				ret = max(ret, j - i + 1);
			i = j;
		}
		if (mark[h - 1][c - 'a'])
		{
			cur = 0;
			p = 0;
			while (p < sz && s[h][p] == c)
				++cur, ++p;
			p = sz - 1;
			while (p >= 0 && s[h][p] == c)
				++cur, --p;
			return max(cur + 1, ret);
		}
		return ret;
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> s[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 26; j++)
			mark[i][j] |= mark[i - 1][j];
		for (auto c : s[i])
			mark[i][c - 'a'] = 1;
	}
	for (char c = 'l'; c <= 'l'; c++)
		res = max(res, calc(n, c));
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define forn(a,b,c) for(int a=b;a<=c;a++)
#define ford(a,b,c) for(int a=b;a>=c;a--)
const int N=1e5+5;
const int A=28;
int n;string s;
int pref[N][A],suf[N][A],sub[N][A],len[N];ll dp[N][A];
int main()
{
    scanf("%d",&n);
    forn(curr,1,n){
        cin>>s;
        len[curr]=s.length();
        int cntPref=0;
        forn(i,0,s.length()-1){
            if(s[i]!=s[0]){
                break;
            }
            cntPref++;
        }
        //printf("cntPref[i]:%d slovo:%d\n",cntPref,s[0]-'a'+1);
        pref[curr][s[0]-'a'+1]=cntPref;
        int cntSuf=0;
        ford(i,s.length()-1,0){
            if(s[i]!=s[s.length()-1]){
                break;
            }
            cntSuf++;
        }
       // printf("cntSuf[i]:%d slovo:%d\n",cntSuf,s[s.length()-1]-'a'+1);
        suf[curr][s[s.length()-1]-'a'+1]=cntSuf;
        s='0'+s;
        int cntCurr=0;
        forn(i,1,s.length()-1){
            if(s[i]==s[i-1]){
                cntCurr++;
            }
            else{
                cntCurr=1;
            }
            sub[curr][s[i]-'a'+1]=max(sub[i][s[i]-'a'+1],cntCurr);
        }
        /*forn(j,1,26){
            printf("sub[%d]:%d\n",j,sub[curr][j]);
        }*/
    }
    forn(i,1,n){
        forn(j,1,26){
            if(dp[i-1][j]==0){
                dp[i][j]=sub[i][j];
            }
            else{
                if(pref[i][j]==len[i]){
                    if(dp[i-1][j]>=1e12){
                        dp[i][j]=dp[i-1][j];
                    }
                    else{
                        dp[i][j]=dp[i-1][j]*(ll)(len[i]+1)+(ll)len[i];
                    }
                }
                else{
                    dp[i][j]=max(sub[i][j],pref[i][j]+suf[i][j]+1);
                }
            }
        }
    }
    ll ans=LLONG_MIN;
    forn(j,1,26){
        ans=max(ans,dp[n][j]);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define forn(a,b,c) for(int a=b;a<=c;a++)
#define ford(a,b,c) for(int a=b;a>=c;a--)
const int N=1e5+5;
const int A=28;
int n;string s;
int pref[N][A],suf[N][A],sub[N][A],len[N];ll dp[N][A];
int main()
{
    scanf("%d",&n);
    forn(curr,1,n){
        cin>>s;
        len[curr]=s.length();
        int cntPref=0;
        forn(i,0,s.length()-1){
            if(s[i]!=s[0]){
                break;
            }
            cntPref++;
        }
        //printf("cntPref[i]:%d slovo:%d\n",cntPref,s[0]-'a'+1);
        pref[curr][s[0]-'a'+1]=cntPref;
        int cntSuf=0;
        ford(i,s.length()-1,0){
            if(s[i]!=s[s.length()-1]){
                break;
            }
            cntSuf++;
        }
       // printf("cntSuf[i]:%d slovo:%d\n",cntSuf,s[s.length()-1]-'a'+1);
        suf[curr][s[s.length()-1]-'a'+1]=cntSuf;
        s='0'+s;
        int cntCurr=0;
        forn(i,1,s.length()-1){
            if(s[i]==s[i-1]){
                cntCurr++;
            }
            else{
                cntCurr=1;
            }
            sub[curr][s[i]-'a'+1]=max(sub[i][s[i]-'a'+1],cntCurr);
        }
        /*forn(j,1,26){
            printf("sub[%d]:%d\n",j,sub[curr][j]);
        }*/
    }
    forn(i,1,n){
        forn(j,1,26){
            if(dp[i-1][j]==0){
                dp[i][j]=sub[i][j];
            }
            else{
                if(pref[i][j]==len[i]){
                    dp[i][j]=dp[i-1][j]*(ll)(len[i]+1)+(ll)len[i];
                }
                else{
                    dp[i][j]=max(sub[i][j],pref[i][j]+suf[i][j]+1);
                }
            }
        }
    }
    ll ans=LLONG_MIN;
    forn(j,1,26){
        ans=max(ans,dp[n][j]);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define forn(a,b,c) for(int a=b;a<=c;a++)
#define ford(a,b,c) for(int a=b;a>=c;a--)
const int N=1e5+5;
const int A=28;
int n;string s;
int pref[N][A],suf[N][A],sub[N][A],len[N];ll dp[N][A];
int main()
{
    scanf("%d",&n);
    forn(curr,1,n){
        cin>>s;
        len[curr]=s.length();
        int cntPref=0;
        forn(i,0,s.length()-1){
            if(s[i]!=s[0]){
                break;
            }
            cntPref++;
        }
        //printf("cntPref[i]:%d slovo:%d\n",cntPref,s[0]-'a'+1);
        pref[curr][s[0]-'a'+1]=cntPref;
        int cntSuf=0;
        ford(i,s.length()-1,0){
            if(s[i]!=s[s.length()-1]){
                break;
            }
            cntSuf++;
        }
       // printf("cntSuf[i]:%d slovo:%d\n",cntSuf,s[s.length()-1]-'a'+1);
        suf[curr][s[s.length()-1]-'a'+1]=cntSuf;
        s='0'+s;
        int cntCurr=0;
        forn(i,1,s.length()-1){
            if(s[i]==s[i-1]){
                cntCurr++;
            }
            else{
                cntCurr=1;
            }
            sub[curr][s[i]-'a'+1]=max(sub[i][s[i]-'a'+1],cntCurr);
        }
        /*forn(j,1,26){
            printf("sub[%d]:%d\n",j,sub[curr][j]);
        }*/
    }
    forn(i,1,n){
        forn(j,1,26){
            if(dp[i-1][j]==0){
                dp[i][j]=sub[i][j];
            }
            else{
                if(pref[i][j]==len[i]){
                    dp[i][j]=dp[i-1][j]*(ll)(len[i]+1)+(ll)len[i];
                }
                else{
                    dp[i][j]=max(sub[i][j],pref[i][j]+suf[i][j]+1);
                }
            }
            dp[i][j]=min(dp[i][j],(ll)1e9);
        }
    }
    ll ans=LLONG_MIN;
    forn(j,1,26){
        ans=max(ans,dp[n][j]);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=1e5+10;
int n,ans;
vector<char> p[N];
char s[N];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s);
		int len=strlen(s);
		for(int j=0;j<len;j++)p[i].push_back(s[j]);
	}
	int ans=0;
	for(char ch='a';ch<='z';ch++)
	{
		int mx=0;
		for(int i=1;i<=n;i++)
		{
			int mxlen=0,tmp=0,suf=0,pre=0;
			for(int j=0;j<p[i].size();j++)
				if(p[i][j]==ch)tmp++;else mxlen=max(mxlen,tmp),tmp=0;
			mxlen=max(mxlen,tmp);
			for(int j=0;j<p[i].size();j++)if(p[i][j]==ch)pre++;else break;
			for(int j=p[i].size()-1;j>=0;j--)if(p[i][j]==ch)suf++;else break;
			if(p[i].size()==mxlen)mx=(mx+1)*mxlen+mx;else
			mx=max(mxlen,max(((mx>0)?(pre+suf+1):0),(int)(mx>0)));
		}
		ans=max(mx,ans);
	}
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define forn(a,b,c) for(int a=b;a<=c;a++)
#define ford(a,b,c) for(int a=b;a>=c;a--)
const int N=1e5+5;
const int A=28;
int n;string s;
int dp[N][A],pref[N][A],suf[N][A],sub[N][A],len[N];
int main()
{
    scanf("%d",&n);
    forn(curr,1,n){
        cin>>s;
        len[curr]=s.length();
        int cntPref=0;
        forn(i,0,s.length()-1){
            if(s[i]!=s[0]){
                break;
            }
            cntPref++;
        }
        //printf("cntPref[i]:%d slovo:%d\n",cntPref,s[0]-'a'+1);
        pref[curr][s[0]-'a'+1]=cntPref;
        int cntSuf=0;
        ford(i,s.length()-1,0){
            if(s[i]!=s[s.length()-1]){
                break;
            }
            cntSuf++;
        }
       // printf("cntSuf[i]:%d slovo:%d\n",cntSuf,s[s.length()-1]-'a'+1);
        suf[curr][s[s.length()-1]-'a'+1]=cntSuf;
        s='0'+s;
        int cntCurr=0;
        forn(i,1,s.length()-1){
            if(s[i]==s[i-1]){
                cntCurr++;
            }
            else{
                cntCurr=1;
            }
            sub[curr][s[i]-'a'+1]=max(sub[i][s[i]-'a'+1],cntCurr);
        }
        /*forn(j,1,26){
            printf("sub[%d]:%d\n",j,sub[curr][j]);
        }*/
    }
    forn(i,1,n){
        forn(j,1,26){
            if(dp[i-1][j]==0){
                dp[i][j]=sub[i][j];
            }
            else{
                if(pref[i][j]==len[i]){
                    dp[i][j]=dp[i-1][j]*(len[i]+1)+len[i];
                }
                else{
                    dp[i][j]=max(sub[i][j],pref[i][j]+suf[i][j]+1);
                }
            }
        }
    }
    int ans=INT_MIN;
    forn(j,1,26){
        ans=max(ans,dp[n][j]);
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = max(cnt+1, (ll)maxCons(w, c));
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			flag = max(maxCons(w, c), flag);
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			assert(ans < INT_MAX);

			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			assert(ans < 1e18);

			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			flag = max(maxCons(w, c), flag);
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			assert(ans > INT_MAX);

			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			flag = max(maxCons(w, c), flag);
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				ans = cnt+1;
			}
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			assert(ans < 1e15);

			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		if(n == 301)
			debug2(c, s[n-1]);
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define N 100005
#define INF 1e18
#define ll long long int
#define F first
#define S second
#define boost ios::sync_with_stdio(0); cin.tie(0)
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll multiply(ll a, ll b) {
	if(b== 0)
		return 0;
	ll z = multiply(a, b/2);
	if(b%2)
		return ((z+z)%mod+a)%mod;
	return (z+z)%mod;
}

ll power(ll x, ll y, ll MOD = mod)
{
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res * x)%MOD;
        y = y >> 1;
        x = x * x;
        x = x%MOD;
    }
    return res;
}
string s[N];
int f[255];

int maxCons(string a, char c) {
	int n = a.length(), maxy = 0, cnt = 0;
	for(int i = 0; i < n; i++) {
		if(a[i] == c) {
			cnt++;
		}
		else {
			maxy = max(maxy, cnt);
			cnt = 0;
		}
	}
	maxy = max(maxy, cnt);
	return maxy;
}

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n-1; i++) {
		for(int j = 0; j < s[i].length(); j++) {
			f[s[i][j]]++;
		}
	}

	int flag = 0;
	for(int i = 0; i < s[n-1].length(); i++) {
		if(s[n-1][0] != s[n-1][i])
			flag = 1;
	}
	if(flag) {
		int maxy = 0;
		for(int i = 0; i < 26; i++) {
			maxy = max(maxy, maxCons(s[n-1], 'a'+i));
		}
		if(s[n-1][0] == s[n-1][s[n-1].length()-1] and f[s[n-1][0]]) {
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			// debug(cnt);
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][0]])
		{
			int cnt = 1;
			for(int i = 0; i < s[n-1].length(); i++) {
				if(s[n-1][0] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		if(f[s[n-1][s[n-1].length()-1]])
		{
			int cnt = 1;
			for(int i = s[n-1].length()-1; i >= 0; i--) {
				if(s[n-1][s[n-1].length()-1] == s[n-1][i])
					cnt++;
				else
					break;
			}
			maxy = max(maxy, cnt);
		}
		cout << maxy << endl;
	}
	else {
		char c = s[n-1][0];
		ll ans = maxCons(s[0],c);
		int flag = ans;
		for(int i = 1; i < n; i++) {
			string w = s[i];
			ll cnt = 0;
			for(int j = 0; j < w.length(); j++) {
				if(w[j] == c)
					cnt++;
				else
					break;
			}
			if(cnt != w.length()) {
				for(int j = w.length()-1; j >=0 ;j--)
				{
					if(w[j] == c)
						cnt++;
					else
						break;
				}
			}
			if(cnt == w.length()) {
				ans = (ans+1)*cnt+ans;
			}
			else {
				if(flag)
					ans = cnt+1;
				else
					ans = maxCons(w, c);
			}
			flag = max(maxCons(w, c), flag);
		}
		cout << ans << endl;
	}
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long num[30],pre[100010],cnt;
int main()
{
	int n,l,lr,ll;
	char s[100010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(lr!=l&&s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?ll+1:ll;
		}
		for(int j=lr+1;j<=l-ll+1;j++)
			if(s[j]==s[j-1])
				pre[j]=pre[j-1]+1;
			else
			{
				pre[j]=1;
				num[s[j-1]-'a']=max(num[s[j-1]-'a'],pre[j-1]);
			}
	}
	cnt=0;
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int num[30];
int main()
{
	int n,pre[1010],cnt=0,l,lr,ll;
	char s[1010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(lr!=l&&s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?lr+1:lr;
		}
		for(int j=lr+1;j<=l-ll;j++)
			if(s[j]==s[j-1])
				pre[j]=pre[j-1]+1;
			else
			{
				pre[j]=1;
				num[s[j]-'a']=max(num[s[j]-'a'],pre[j-1]);
			}
	}
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int num[30];
int main()
{
	int n,pre[1010],cnt=0,l,lr,ll;
	char s[1010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?lr+1:lr;
		}
		for(int j=1;j<=l;j++)
		{
			pre[j]=s[j]==s[j-1]?pre[j-1]+1:1;
			num[s[j]-'a']=max(num[s[j]-'a'],pre[j]);
		}
	}
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long num[30],pre[100010],cnt;
int main()
{
	int n,l,lr,ll;
	char s[100010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(lr!=l&&s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?lr+1:lr;
		}
		for(int j=lr+1;j<=l-ll;j++)
			if(s[j]==s[j-1])
				pre[j]=pre[j-1]+1;
			else
			{
				pre[j]=1;
				num[s[j-1]-'a']=max(num[s[j-1]-'a'],pre[j-1]);
			}
	}
	cnt=0;
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long num[30],pre[100010],cnt;
int main()
{
	int n,l,lr,ll;
	char s[100010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(lr!=l&&s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?ll+1:ll;
		}
		for(int j=lr+1;j<=l-ll;j++)
			if(s[j]==s[j-1])
				pre[j]=pre[j-1]+1;
			else
			{
				pre[j]=1;
				num[s[j-1]-'a']=max(num[s[j-1]-'a'],pre[j-1]);
			}
	}
	cnt=0;
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long num[30],pre[1010],cnt;
int main()
{
	int n,l,lr,ll;
	char s[1010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(lr!=l&&s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?lr+1:lr;
		}
		for(int j=lr+1;j<=l-ll;j++)
			if(s[j]==s[j-1])
				pre[j]=pre[j-1]+1;
			else
			{
				pre[j]=1;
				num[s[j-1]-'a']=max(num[s[j-1]-'a'],pre[j-1]);
			}
	}
	cnt=0;
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int num[30];
int main()
{
	int n,pre[1010],cnt=0,l,lr,ll;
	char s[1010];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s+1;
		l=strlen(s+1);
		for(int j=0;j<26;j++)
			if(num[j]&&j+'a'!=s[1]&&j+'a'!=s[l])
				num[j]=1;
		lr=1;
		while(s[lr+1]==s[lr])
			lr++;
		ll=l;
		while(lr!=l&&s[ll]==s[ll-1])
			ll--;
		ll=l-ll+1;
		if(s[1]==s[l])
			if(lr==l)
				num[s[1]-'a']=(l+1)*num[s[1]-'a']+l;
			else
				num[s[1]-'a']=lr+ll+1;
		else
		{
			num[s[1]-'a']=num[s[1]-'a']>0?lr+1:lr;
			num[s[l]-'a']=num[s[l]-'a']>0?lr+1:lr;
		}
		for(int j=lr+1;j<=l-ll;j++)
			if(s[j]==s[j-1])
				pre[j]=pre[j-1]+1;
			else
			{
				pre[j]=1;
				num[s[j-1]-'a']=max(num[s[j-1]-'a'],pre[j-1]);
			}
	}
	for(int j=0;j<26;j++)
		cnt=max(cnt,num[j]);
	cout<<cnt;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <unordered_map>

#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define For(i,s,n) for (int i=s;i<=n;i++)
#define FOR(i,s,n) for (int i=s;i<n;i++)
#define Ford(i,s,n) for (int i=s;i>=n;i--)

#define MD 1000000007

typedef long long ll;

using namespace std;

string s[100001];
ll ans[100000], n, lenChar[100000];
bool used[100001];

int main() {
	cin >> n;
	For(i, 1, n) {
		cin >> s[i];
	}
	For(i, 1, n) {
		char st = s[i][0], en = s[i][s[i].length() - 1];
		ll stLen = 0, enLen = 0;

		FOR(j, 0, s[i].length()) {
			if (s[i][j] != st) {
				break;
			}
			stLen++;
		}

		Ford(j, s[i].length() - 1, 0) {
			if (s[i][j] != en) {
				break;
			}
			enLen++;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int len = 0;
			FOR(k, 0, s[i].length())
				len += ((s[i][k] == j) ? 1:0);
			lenChar[j] = len;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int res = 0;
			if (st == j && stLen == s[i].length()) {
				ans[j] = (ans[j] + 1) * stLen + ans[j];
				continue;
			}
			if (st == j && en == j) {
				//cout << s[i] << " " << stLen << " " << enLen << endl;
				ans[j] = Max(stLen, lenChar[j]);
				ans[j] = Max(enLen, ans[j]);
				if (used[j])
					ans[j] = Max(stLen + enLen + 1, ans[j]);
				continue;
			}
			if (st == j || en == j) {
				ans[j] = lenChar[j];
				if (st == j)
					ans[j] = Max(ans[j], stLen);
				else
					ans[j] = Max(ans[j], enLen);
				if (used[j])
					ans[j]++;
				continue;
			}
			ans[j] = lenChar[j];
			if (ans[j] == 0 && used[j])
				ans[j] = 1;
		}
		for (char j = 'a'; j <= 'z'; j++) {
			if (ans[j] > 1000000000)
				ans[j] = 1000000000;
		}
		FOR(j, 0, s[i].length()) {
			used[s[i][j]] = true;
		}
		//for (char j = 'a'; j <= 'z'; j++)
		//	cout << char(j) << " " << ans[j] << endl;
	}

	int res = 0;

	for (char j = 'a'; j <= 'z'; j++) {
		res = Max(res, ans[j]);
	}

	cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <unordered_map>

#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define For(i,s,n) for (int i=s;i<=n;i++)
#define FOR(i,s,n) for (int i=s;i<n;i++)
#define Ford(i,s,n) for (int i=s;i>=n;i--)

#define MD 1000000007

typedef long long ll;

using namespace std;

string s[100001];
ll ans[100000], n, lenChar[100000];
bool used[100001];

int main() {
	cin >> n;
	For(i, 1, n) {
		cin >> s[i];
	}
	For(i, 1, n) {
		char st = s[i][0], en = s[i][s[i].length() - 1];
		ll stLen = 1, enLen = 1;

		FOR(j, 1, s[i].length()) {
			if (s[i][j] != st) {
				break;
			}
			stLen++;
		}

		Ford(j, s[i].length() - 1, 0) {
			if (s[i][j] != en) {
				break;
			}
			enLen++;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int len = 0;
			FOR(k, 0, s[i].length())
				len += ((s[i][k] == j) ? 1:0);
			lenChar[j] = len;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int res = 0;
			if (st == j && stLen == s[i].length()) {
				ans[j] = (ans[j] + 1) * stLen + ans[j];
				continue;
			}
			if (st == j && en == j) {
				ans[j] = Max(stLen, lenChar[j]);
				ans[j] = Max(enLen, ans[j]);
				if (used[j])
					ans[j] = Max(stLen + enLen + 1, ans[j]);
				continue;
			}
			if (st == j || en == j) {
				ans[j] = lenChar[j];
				if (st == j)
					ans[j] = Max(ans[j], stLen);
				else
					ans[j] = Max(ans[j], enLen);
				if (used[j])
					ans[j]++;
				continue;
			}
			ans[j] = lenChar[j];
			if (ans[j] == 0 && used[j])
				ans[j] = 1;
		}
		for (char j = 'a'; j <= 'z'; j++) {
			if (ans[j] > 1000000000)
				ans[j] = 1000000000;
		}
		FOR(j, 0, s[i].length()) {
			used[s[i][j]] = true;
		}
		//for (char j = 'a'; j <= 'z'; j++)
		//	cout << char(j) << " " << ans[j] << endl;
	}

	int res = 0;

	for (char j = 'a'; j <= 'z'; j++) {
		res = Max(res, ans[j]);
	}

	cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <unordered_map>

#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define For(i,s,n) for (int i=s;i<=n;i++)
#define FOR(i,s,n) for (int i=s;i<n;i++)
#define Ford(i,s,n) for (int i=s;i>=n;i--)

#define MD 1000000007

typedef long long ll;

using namespace std;

string s[100001];
int ans[100000], n, lenChar[100000];
bool used[100001];

int main() {
	cin >> n;
	For(i, 1, n) {
		cin >> s[i];
	}
	For(i, 1, n) {
		char st = s[i][0], en = s[i][s[i].length() - 1];
		int stLen = 1, enLen = 1;

		FOR(j, 1, s[i].length()) {
			if (s[i][j] != st) {
				break;
			}
			stLen++;
		}

		Ford(j, s[i].length() - 1, 0) {
			if (s[i][j] != en) {
				break;
			}
			enLen++;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int len = 0;
			FOR(k, 0, s[i].length())
				len += ((s[i][k] == j) ? 1:0);
			lenChar[j] = len;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int res = 0;
			if (st == j && stLen == s[i].length()) {
				ans[j] = (ans[j] + 1) * stLen + ans[j];
				continue;
			}
			if (st == j && en == j) {
				ans[j] = Max(stLen, lenChar[j]);
				ans[j] = Max(enLen, ans[j]);
				if (used[j])
					ans[j] = Max(stLen + enLen + 1, ans[j]);
				continue;
			}
			if (st == j || en == j) {
				ans[j] = lenChar[j];
				if (st == j)
					ans[j] = Max(ans[j], stLen);
				else
					ans[j] = Max(ans[j], enLen);
				if (used[j])
					ans[j]++;
				continue;
			}
			ans[j] = lenChar[j];
			if (ans[j] == 0 && used[j])
				ans[j] = 1;
		}
		FOR(j, 0, s[i].length()) {
			used[s[i][j]] = true;
		}
		//for (char j = 'a'; j <= 'z'; j++)
		//	cout << char(j) << " " << ans[j] << endl;
	}

	int res = 0;

	for (char j = 'a'; j <= 'z'; j++) {
		res = Max(res, ans[j]);
	}

	cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <unordered_map>

#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define For(i,s,n) for (int i=s;i<=n;i++)
#define FOR(i,s,n) for (int i=s;i<n;i++)
#define Ford(i,s,n) for (int i=s;i>=n;i--)

#define MD 1000000007

typedef long long ll;

using namespace std;

string s[100001];
ll ans[100000], n, lenChar[100000];
bool used[100001];

int main() {
	cin >> n;
	For(i, 1, n) {
		cin >> s[i];
	}
	For(i, 1, n) {
		char st = s[i][0], en = s[i][s[i].length() - 1];
		ll stLen = 1, enLen = 1;

		FOR(j, 1, s[i].length()) {
			if (s[i][j] != st) {
				break;
			}
			stLen++;
		}

		Ford(j, s[i].length() - 1, 0) {
			if (s[i][j] != en) {
				break;
			}
			enLen++;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int len = 0;
			FOR(k, 0, s[i].length())
				len += ((s[i][k] == j) ? 1:0);
			lenChar[j] = len;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int res = 0;
			if (st == j && stLen == s[i].length()) {
				ans[j] = (ans[j] + 1) * stLen + ans[j];

				continue;
			}
			if (st == j && en == j) {
				ans[j] = Max(stLen, lenChar[j]);
				ans[j] = Max(enLen, ans[j]);
				if (used[j])
					ans[j] = Max(stLen + enLen + 1, ans[j]);
				continue;
			}
			if (st == j || en == j) {
				ans[j] = lenChar[j];
				if (st == j)
					ans[j] = Max(ans[j], stLen);
				else
					ans[j] = Max(ans[j], enLen);
				if (used[j])
					ans[j]++;
				continue;
			}
			ans[j] = lenChar[j];
			if (ans[j] == 0 && used[j])
				ans[j] = 1;
			if (ans[j] > 1000000000)
				ans[j] = 1000000000;
		}
		FOR(j, 0, s[i].length()) {
			used[s[i][j]] = true;
		}
		//for (char j = 'a'; j <= 'z'; j++)
		//	cout << char(j) << " " << ans[j] << endl;
	}

	int res = 0;

	for (char j = 'a'; j <= 'z'; j++) {
		res = Max(res, ans[j]);
	}

	cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <unordered_map>

#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define For(i,s,n) for (int i=s;i<=n;i++)
#define FOR(i,s,n) for (int i=s;i<n;i++)
#define Ford(i,s,n) for (int i=s;i>=n;i--)

#define MD 1000000007

typedef long long ll;

using namespace std;

string s[100001];
int ans[100000], n, lenChar[100000];
bool used[100001];

int main() {
	cin >> n;
	For(i, 1, n) {
		cin >> s[i];
	}
	For(i, 1, n) {
		char st = s[i][0], en = s[i][s[i].length() - 1];
		int stLen = 1, enLen = 1;

		FOR(j, 1, s[i].length()) {
			if (s[i][j] != st) {
				break;
			}
			stLen++;
		}

		Ford(j, s[i].length() - 1, 0) {
			if (s[i][j] != en) {
				break;
			}
			enLen++;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int len = 0;
			FOR(k, 0, s[i].length())
				len += ((s[i][k] == j) ? 1:0);
			lenChar[j] = len;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int res = 0;
			if (st == j && stLen == s[i].length()) {
				ans[j] = (ans[j] + 1) * stLen + ans[j];
				continue;
			}
			if (st == j && en == j) {
				ans[j] = Max(stLen, lenChar[j]);
				ans[j] = Max(enLen, ans[j]);
				if (used[j])
					ans[j] = Max(stLen + enLen + 1, ans[j]);
				continue;
			}
			if (st == j || en == j) {
				ans[j] = lenChar[j];
				if (st == j)
					ans[j] = Max(ans[j], stLen + 1);
				else
					ans[j] = Max(ans[j], enLen + 1);
				if (used[j])
					ans[j]++;
				continue;
			}
			ans[j] = lenChar[j];
			if (ans[j] == 0 && used[j])
				ans[j] = 1;
		}
		FOR(j, 0, s[i].length()) {
			used[s[i][j]] = true;
		}
		//for (char j = 'a'; j <= 'z'; j++)
		//	cout << char(j) << " " << ans[j] << endl;
	}

	int res = 0;

	for (char j = 'a'; j <= 'z'; j++) {
		res = Max(res, ans[j]);
	}

	cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <unordered_map>

#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define For(i,s,n) for (int i=s;i<=n;i++)
#define FOR(i,s,n) for (int i=s;i<n;i++)
#define Ford(i,s,n) for (int i=s;i>=n;i--)

#define MD 1000000007

typedef long long ll;

using namespace std;

string s[100001];
ll ans[100000], n, lenChar[100000];
bool used[100001];

int main() {
	cin >> n;
	For(i, 1, n) {
		cin >> s[i];
	}
	For(i, 1, n) {
		char st = s[i][0], en = s[i][s[i].length() - 1];
		ll stLen = 0, enLen = 0;

		FOR(j, 0, s[i].length()) {
			if (s[i][j] != st) {
				break;
			}
			stLen++;
		}

		Ford(j, s[i].length() - 1, 0) {
			if (s[i][j] != en) {
				break;
			}
			enLen++;
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int len = 0;
			lenChar[j] = 0;
			FOR(k, 0, s[i].length())
				if (s[i][k] == j)
					len++;
				else {
					lenChar[j] = Max(lenChar[j], len);
					len = 0;
				}
		}

		for (char j = 'a'; j <= 'z'; j++) {
			int res = 0;
			if (st == j && stLen == s[i].length()) {
				ans[j] = (ans[j] + 1) * stLen + ans[j];
				continue;
			}
			if (st == j && en == j) {
				//cout << s[i] << " " << stLen << " " << enLen << endl;
				ans[j] = Max(stLen, lenChar[j]);
				ans[j] = Max(enLen, ans[j]);
				if (used[j])
					ans[j] = Max(stLen + enLen + 1, ans[j]);
				continue;
			}
			if (st == j || en == j) {
				ans[j] = lenChar[j];
				if (st == j)
					ans[j] = Max(ans[j], stLen);
				else
					ans[j] = Max(ans[j], enLen);
				if (used[j])
					ans[j]++;
				continue;
			}
			ans[j] = lenChar[j];
			if (ans[j] == 0 && used[j])
				ans[j] = 1;
		}
		for (char j = 'a'; j <= 'z'; j++) {
			if (ans[j] > 1000000000)
				ans[j] = 1000000000;
		}
		FOR(j, 0, s[i].length()) {
			used[s[i][j]] = true;
		}
		//for (char j = 'a'; j <= 'z'; j++)
		//	cout << char(j) << " " << ans[j] << endl;
	}

	int res = 0;

	for (char j = 'a'; j <= 'z'; j++) {
		res = Max(res, ans[j]);
	}

	cout << res << endl;
}


#include <ext/rope>
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
typedef vector<vector<ll>> mat;

const int N = 26;
int cnt[N] , n , out , hv[N];
string s;

int main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        cin >> s;
        bool inHv[N] = {};
        int bCnt = s.size() - 1;
        int fCnt = 0;
        for(auto j : s)
            hv[j - 'a'] = 1 , inHv[j - 'a'] = 1;

        while(bCnt >= 0 && s.back() == s[bCnt]) bCnt--;
        while(fCnt != s.size() && s[0] == s[fCnt]) fCnt++;

        if(bCnt == -1)
        {
            if(cnt[s.back() - 'a'])
                cnt[s.back() - 'a'] = s.size() * 2 + (cnt[s.back() - 'a'] - 1) * s.size() * 2 + cnt[s.back() - 'a'];
            else cnt[s.back() - 'a'] = s.size();
        }
        else if(s.back() == s[0])
        {
            cnt[s.back() - 'a'] = fCnt + s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
        }
        else
        {
            cnt[s.back() - 'a'] = s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
            cnt[s[0] - 'a'] = fCnt + 1 + (cnt[s[0] - 'a'] != 0);
        }

        for(int j = 0 ; j < N ; j++)
            if(!inHv[j])
                cnt[j] = hv[j];
    }
    for(int i = 0 ; i < N ; i++) out = max(out , cnt[i]);
    cout << out;

    return 0;
}


#include <ext/rope>
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
typedef vector<vector<ll>> mat;

const int N = 26;
int cnt[N] , n , out , hv[N];
string s;

int main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        cin >> s;
        bool inHv[N] = {};
        int bCnt = s.size() - 1;
        int fCnt = 0;
        for(auto j : s)
            hv[j - 'a'] = 1 , inHv[j - 'a'] = 1;

        while(bCnt >= 0 && s.back() == s[bCnt]) bCnt--;
        while(fCnt != s.size() && s[0] == s[fCnt]) fCnt++;

        if(bCnt == -1)
        {
            if(cnt[s.back() - 'a'])
                cnt[s.back() - 'a'] = s.size() * 2 + (cnt[s.back() - 'a'] != 0);
            else cnt[s.back() - 'a'] = s.size();
        }
        else if(s.back() == s[0])
        {
            cnt[s.back() - 'a'] = fCnt + s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
        }
        else
        {
            cnt[s.back() - 'a'] = s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
            cnt[s[0] - 'a'] = fCnt + (cnt[s[0] - 'a'] != 0);
        }

        for(int j = 0 ; j < N ; j++)
            if(!inHv[j])
                cnt[j] = hv[j];
    }
    for(int i = 0 ; i < N ; i++) out = max(out , cnt[i]);
    cout << out;

    return 0;
}


#include <ext/rope>
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
typedef vector<vector<ll>> mat;

const int N = 26;
int cnt[N] , n , out , hv[N];
string s;

int main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        cin >> s;
        bool inHv[N] = {};
        int bCnt = s.size() - 1;
        int fCnt = 0;
        for(auto j : s)
            hv[j - 'a'] = 1 , inHv[j - 'a'] = 1;

        while(bCnt >= 0 && s.back() == s[bCnt]) bCnt--;
        while(fCnt != s.size() && s[0] == s[fCnt]) fCnt++;

        if(bCnt == -1)
        {
            if(cnt[s.back() - 'a'])
                cnt[s.back() - 'a'] = s.size() * 2 + (cnt[s.back() - 'a'] - 1) * s.size() * 2 + cnt[s.back() - 'a'];
            else cnt[s.back() - 'a'] = s.size();
        }
        else if(s.back() == s[0])
        {
            cnt[s.back() - 'a'] = fCnt + s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
        }
        else
        {
            cnt[s.back() - 'a'] = s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
            cnt[s[0] - 'a'] = fCnt + (cnt[s[0] - 'a'] != 0);
        }

        for(int j = 0 ; j < N ; j++)
            if(!inHv[j])
                cnt[j] = hv[j];
    }
    for(int i = 0 ; i < N ; i++) out = max(out , cnt[i]);
    cout << out;

    return 0;
}


#include <ext/rope>
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
typedef vector<vector<ll>> mat;

const int N = 26;
int cnt[N] , n , out , hv[N];
string s;

int main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        cin >> s;
        bool inHv[N] = {};
        int bCnt = s.size() - 1;
        int fCnt = 0;
        for(auto j : s)
            hv[j - 'a'] = 1 , inHv[j - 'a'] = 1;

        while(bCnt >= 0 && s.back() == s[bCnt]) bCnt--;
        while(fCnt != s.size() && s[0] == s[fCnt]) fCnt++;

        if(bCnt == -1)
        {
            if(cnt[s.back() - 'a'])
                cnt[s.back() - 'a'] = s.size() * 2 + (cnt[s.back() - 'a'] - 1) * s.size() * 2;
            else cnt[s.back() - 'a'] = s.size();
        }
        else if(s.back() == s[0])
        {
            cnt[s.back() - 'a'] = fCnt + s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
        }
        else
        {
            cnt[s.back() - 'a'] = s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
            cnt[s[0] - 'a'] = fCnt + (cnt[s[0] - 'a'] != 0);
        }

        for(int j = 0 ; j < N ; j++)
            if(!inHv[j])
                cnt[j] = hv[j];
    }
    for(int i = 0 ; i < N ; i++) out = max(out , cnt[i]);
    cout << out;

    return 0;
}


#include <ext/rope>
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
typedef vector<vector<ll>> mat;

const int N = 26;
int cnt[N] , n , out , hv[N];
string s;

int main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        cin >> s;
        bool inHv[N] = {};
        int bCnt = s.size() - 1;
        int fCnt = 0;
        for(auto j : s)
            hv[j - 'a'] = 1 , inHv[j - 'a'] = 1;

        while(bCnt >= 0 && s.back() == s[bCnt]) bCnt--;
        while(fCnt != s.size() && s[0] == s[fCnt]) fCnt++;

        if(bCnt == -1)
        {
            if(cnt[s.back() - 'a'])
                cnt[s.back() - 'a'] = s.size() * 2 + (cnt[s.back() - 'a'] - 1) * s.size() + cnt[s.back() - 'a'];
            else cnt[s.back() - 'a'] = s.size();
        }
        else if(s.back() == s[0])
        {
            cnt[s.back() - 'a'] = fCnt + s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
        }
        else
        {
            cnt[s.back() - 'a'] = s.size() - bCnt - 1 + (cnt[s.back() - 'a'] != 0);
            cnt[s[0] - 'a'] = fCnt + (cnt[s[0] - 'a'] != 0);
        }

        for(int j = 0 ; j < N ; j++)
            if(!inHv[j])
                cnt[j] = hv[j];

        int pre = '1';
        int c = 0;
        int inCnt[N] = {};
        for(int j = 0 ; j < s.size() ; j++)
        {
            if(pre != s[j] && pre != '1')
                inCnt[pre - 'a'] = max(inCnt[pre - 'a'] , c) , c = 0;
            c++;
            pre = s[j];
        }

        inCnt[pre - 'a'] = max(inCnt[pre - 'a'] , c);
        for(int j = 0 ; j < s.size() ; j++)
            if(!(s[0] == s[j] || s[j] == s[s.size() - 1]))
                cnt[j] = hv[j];

        for(int j = 0 ; j < N ; j++)
            cnt[j] = max(cnt[j] , inCnt[j]);
    }
    for(int i = 0 ; i < N ; i++) out = max(out , cnt[i]);
    cout << out;

    return 0;
}