#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define DEBUG 0
#define cerr if (DEBUG) cerr
#define fprintf if (DEBUG) fprintf
#define local freopen("in.txt","r",stdin);
#define test cerr<<"hi\n";
#define tr(x) cerr<<#x" "<<x<<'\n';
#define fi first
#define se second
#define HH1 402653189
#define HH2 1610612741
#define INF 0x3f3f3f3f
#define tii tuple<int,int,int>
#define npos string::npos

using namespace std;

#define MOD 1000000007LL
#define MAXN 100005
#define e9 1000000000

int n, ans[256], len[MAXN][256];
string a[MAXN];
pair<char, int> cur_pre, cur_suf, cur_sg;

void genans(int id) {
	vector<pair<char, pii> > v;
	string &s = a[id];

	v.pb(mp(s[0], mp(0, s.length())));

	for (int i = 1; i < s.length(); i++) {
		if (s[i] != s[i - 1]) v.back().se.se = i, v.pb(mp(s[i], mp(i, n)));
	}

	for (auto x : v) len[id][x.fi] = max(len[id][x.fi], x.se.se - x.se.fi);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	reverse(a + 1, a + 1 + n);

	for (int i = 1; i <= n; i++) genans(i);

	for (char c = 'a'; c <= 'z'; c++) ans[c] = len[1][c];

	if ((*max_element(ans + 'a', ans + 'z' + 1)) == a[1].length())
		cur_sg = mp((max_element(ans + 'a', ans + 'z' + 1)) - ans, a[1].length());
	else {
		int mx;
		for (int pos = 0; pos < a[1].length(); pos++) {
			if (a[1][pos] != a[1][0]) {
				mx = pos;
				break;
			}
		}
		cur_pre = mp(a[1][0], mx);

		for (int pos = a[1].length() - 1; pos >= 0; pos--) {
			if (a[1][pos] != a[1].back()) {
				mx = a[1].length() - 1 - pos;
				break;
			}
		}
		cur_suf = mp(a[1].back(), mx);
	}

	cerr << "cur_sg " << cur_sg.fi << ' ' << cur_sg.se << '\n';

	for (int i = 2; i <= n; i++) {
		tr(ans['v'])
		tr(ans['r'])
		tr(ans['h'])
		tr(ans['p'])
		tr(ans['l'])
		tr(ans['b'])
		tr(ans['n'])
		cerr << "cur_pre " << cur_pre.fi << ' ' << cur_pre.se << '\n';
		cerr << "cur_suf " << cur_suf.fi << ' ' << cur_suf.se << '\n';

		if (cur_sg != mp((char)0, 0)) { // singular
			// maintain prefix, suffix
			if (len[i][a[i][0]] == a[i].length() && a[i][0] == cur_sg.fi) { // also a singular segment here!
				cur_sg = mp(cur_sg.fi, cur_sg.se * (len[i][a[i][0]] + 1) + len[i][a[i][0]]);
				ans[cur_sg.fi] = max(ans[cur_sg.fi], cur_sg.se);
				ans[cur_sg.fi] = min(ans[cur_sg.fi], e9);
			}
			else {
				if (a[i][0] == cur_sg.fi) {
					int mx;
					for (int pos = 0; pos < a[i].length(); pos++) {
						if (a[i][pos] != cur_sg.fi) {
							mx = pos;
							break;
						}
					}
					cur_pre = mp(cur_sg.fi, ans[cur_sg.fi] * (mx + 1) + mx);
					ans[cur_pre.fi] = max(ans[cur_pre.fi], cur_pre.se);
					ans[cur_pre.fi] = min(ans[cur_pre.fi], e9);
 				}
				if (a[i].back() == cur_sg.fi) {
					int mx;
					for (int pos = a[i].length() - 1; pos >= 0; pos--) {
						if (a[i][pos] != cur_sg.fi) {
							mx = a[i].length() - 1 - pos;
							break;
						}
					}
					cur_suf = mp(cur_sg.fi, ans[cur_sg.fi] * (mx + 1) + mx);
					ans[cur_suf.fi] = max(ans[cur_suf.fi], cur_suf.se);
					ans[cur_suf.fi] = min(ans[cur_suf.fi], e9);
				}

				for (char c : a[i]) {
					// join multiple segments
					if (c == cur_sg.fi) ans[c] = max(ans[c], (len[i][c] + 1) * ans[c] + len[i][c]);
					ans[c] = min(ans[c], e9);
				}
				cur_pre = cur_suf = mp(cur_sg.fi, cur_sg.se);
				ans[cur_sg.fi] = max(ans[cur_sg.fi], cur_sg.se);
				ans[cur_sg.fi] = min(ans[cur_sg.fi], e9);
				cur_sg = mp(0, 0);
			}
		}
		else {
			for (char c : a[i]) {
				ans[c] = max(ans[c], 1 + (cur_pre.fi == c) * cur_pre.se + (cur_suf.fi == c) * cur_suf.se);
				ans[c] = min(ans[c], e9);
			}
		}
	}

	cout << (*max_element(ans + 'a', ans + 'z' + 1)) << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 1;
	while(nr < s.size() && s[s.size() - nr] == s.back())
		nr++;
	return nr - 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
		} else if(p[0] == p.back()) {
			// dbg_ok;
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);
			// dbg(nr1, nr2);
			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);
			// dbg(nr1, nr2);
			// dbg(p[0], f[p[0]]);
			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			// dbg(p[0], f[p[0]]);

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++)
			if(c != p[0] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

		p += "&";
		nr = 0;
		for(long long i = 0; i < p.size(); i++) {
			if(i == 0 || p[i] == p[i - 1]) nr++;
			else {
				f[p[i - 1]] = max(f[p[i - 1]], nr);
				nr = 1;
			}
		}

		// for(char c = 'a'; c <= 'z'; c++)
		// 	cout << f[c] << ' ';
		// cout << '\n';
	}

	for(char c = 'a'; c <= 'z'; c++)
		ans = max(ans, f[c]);

	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[1 + s.size() - nr] == s.back())
		nr++;
	return nr;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
		} else if(p[0] == p.back()) {

			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++) {

			if(c != f[p[0]] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

		}

		for(long long i = 0; i < p.size(); i++) {
			if(i == 0 || p[i] == p[i - 1]) nr++;
			else {
				f[p[i - 1]] = max(f[p[i - 1]], nr);
				nr = 1;
			}
		}

		// for(char c = 'a'; c <= 'z'; c++)
		// 	cout << f[c] << ' ';
		// cout << '\n';
	}

	for(char c = 'a'; c <= 'z'; c++)
		ans = max(ans, f[c]);

	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[1 + s.size() - nr] == s.back())
		nr++;
	return nr;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
			ans = max(ans, f[p[0]]);
		} else if(p[0] == p.back()) {

			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++) {

			if(c != f[p[0]] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

			ans = max(ans, f[c]);
		}
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[1 + s.size() - nr] == s.back())
		nr++;
	return nr;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
			ans = max(ans, f[p[0]]);
		} else if(p[0] == p.back()) {

			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++) {

			if(c != f[p[0]] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

		}
	}

	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}
	for(char c = 'a'; c <= 'z'; c++)
		ans = max(ans, f[c]);

	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 1;
	while(nr < s.size() && s[s.size() - nr] == s.back())
		nr++;
	return nr - 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
		} else if(p[0] == p.back()) {
			// dbg_ok;
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);
			// dbg(nr1, nr2);
			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++)
			if(c != f[p[0]] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

		p += "&";
		nr = 0;
		for(long long i = 0; i < p.size(); i++) {
			if(i == 0 || p[i] == p[i - 1]) nr++;
			else {
				f[p[i - 1]] = max(f[p[i - 1]], nr);
				nr = 1;
			}
		}

		// for(char c = 'a'; c <= 'z'; c++)
		// 	cout << f[c] << ' ';
		// cout << '\n';
	}

	for(char c = 'a'; c <= 'z'; c++)
		ans = max(ans, f[c]);

	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[1 + s.size() - nr] == s.back())
		nr++;
	return nr;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
		} else if(p[0] == p.back()) {

			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++) {

			if(c != f[p[0]] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

		}

		p += "&";
		nr = 0;
		for(long long i = 0; i < p.size(); i++) {
			if(i == 0 || p[i] == p[i - 1]) nr++;
			else {
				f[p[i - 1]] = max(f[p[i - 1]], nr);
				nr = 1;
			}
		}

		// for(char c = 'a'; c <= 'z'; c++)
		// 	cout << f[c] << ' ';
		// cout << '\n';
	}

	for(char c = 'a'; c <= 'z'; c++)
		ans = max(ans, f[c]);

	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

// const long long N = ;
long long n, f[270];
string p;

long long cnt_front(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[nr] == s[0])
		nr++;
	return nr;
}
long long cnt_back(const string &s) {
	long long nr = 0;
	while(nr < s.size() && s[1 + s.size() - nr] == s.back())
		nr++;
	return nr;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	cin >> p;

	p += "&";
	long long nr = 0;
	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}

	long long ans = 1;

	for(long long i = 2; i <= n; i++) {

		cin >> p;

		if(p == string(p.size(), p[0])) {
			f[p[0]] = (f[p[0]] + 1) * p.size() + f[p[0]];
		} else if(p[0] == p.back()) {

			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + nr2 + 1;
			else f[p[0]] = max(nr1, nr2);
		} else {
			long long nr1 = cnt_front(p);
			long long nr2 = cnt_back(p);

			if(f[p[0]]) f[p[0]] = nr1 + 1;
			else f[p[0]] = nr1;

			if(f[p.back()]) f[p.back()] = nr2 + 1;
			else f[p.back()] = nr2;
		}

		for(long long c= 'a'; c <= 'z'; c++) {

			if(c != f[p[0]] && c != p.back())
				f[c] = (f[c] ? 1 : 0);

		}
	}

	for(long long i = 0; i < p.size(); i++) {
		if(i == 0 || p[i] == p[i - 1]) nr++;
		else {
			f[p[i - 1]] = max(f[p[i - 1]], nr);
			nr = 1;
		}
	}
	for(char c = 'a'; c <= 'z'; c++)
		ans = max(ans, f[c]);

	cout << ans << '\n';
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void read(T &x)
{
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=getchar();}
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=1e5+10;
inline void MAX(int &x,int y){if(y>x) x=y;}
inline int idx(char c){return c-'a';}
string s[N];
int pre[N],suf[N],len[N],ocu[N][26],bst[N][26],n;
int solve(int cur,char c)
{
	if(cur==1) return bst[cur][idx(c)];
	int ans=(ocu[cur-1][idx(c)],bst[cur][idx(c)]);
	if(pre[cur]==len[cur]) ans=max(ans,s[cur][0]==c?solve(cur-1,c)*(len[cur]+1)+len[cur]:0);
	if(s[cur][0]==c&&s[cur][0]==s[cur][len[cur]-1]&&ocu[cur-1][idx(c)]) ans=max(ans,pre[cur]+suf[cur]+1);
	if(s[cur][0]==c) ans=max(ans,pre[cur]+ocu[cur-1][idx(c)]);
	if(s[cur][len[cur]-1]==c) ans=max(ans,suf[cur]+ocu[cur-1][idx(c)]);
	return ans;
}
int main()
{
	read(n);
	for(int i=1;i<=n;++i)
	{
		cin>>s[i];
		len[i]=s[i].length();
		for(int j=0;j<len[i]&&s[i][j]==s[i][0];++j) pre[i]++;
		for(int j=len[i]-1;~j&&s[i][j]==s[i][len[i]-1];--j) suf[i]++;
		int now=0;
		for(int j=0;j<len[i];++j)
		{
			if(!j||s[i][j]==s[i][j-1]) now++; else now=1;
			MAX(bst[i][idx(s[i][j])],now);
		}

//		cout<<len[i]<<' '<<pre[i]<<' '<<suf[i]<<endl;
		memcpy(ocu[i],ocu[i-1],sizeof(ocu[i]));
		for(int j=0;j<len[i];++j) ocu[i][idx(s[i][j])]=1;
	}
//	for(int i=1;i<=n;++i)
//	{
//		for(int j=0;j<len[i];++j) cout<<((int)s[i][j])<<' '; cout<<endl;
//	}
	int ans=0;
	if(pre[n]==len[n]) ans=solve(n-1,s[n][0])*(len[n]+1)+len[n];
	ans=max(max(pre[n]+ocu[n-1][idx(s[n][0])],suf[n]+ocu[n-1][idx(s[n][len[n]-1])]),ans);
	if(s[n][0]==s[n][len[n]-1]&&ocu[n-1][idx(s[n][0])]) ans=max(ans,pre[n]+suf[n]+1);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void read(T &x)
{
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=getchar();}
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=1e5+10;
inline int idx(char c){return c-'a';}
string s[N];
int pre[N],suf[N],len[N],ocu[N][26],n;
int solve(int cur,char c)
{
	if(cur==1)
	{
		int ans=0,now=0;
		for(int j=0;j<len[cur];++j)
		{
			if(s[cur][j]==c) now++;
			else now=0;
			ans=max(ans,now);
		}
		return ans;
	}
	int ans=0;
	if(pre[cur]==len[cur]) ans=max(ans,s[cur][0]==c?solve(cur-1,c)*(len[cur]+1)+len[cur]:0);
	if(s[cur][0]==c&&s[cur][0]==s[cur][len[cur]-1]&&ocu[cur-1][idx(c)]) ans=max(ans,pre[cur]+suf[cur]+1);
	if(s[cur][0]==c) ans=max(ans,pre[cur]+ocu[cur-1][idx(c)]);
	if(s[cur][len[cur]-1]==c) ans=max(ans,suf[cur]+ocu[cur-1][idx(c)]);
	return ans;
}
int main()
{
	read(n);
	for(int i=1;i<=n;++i)
	{
		cin>>s[i];
		len[i]=s[i].length();
		for(int j=0;j<len[i]&&s[i][j]==s[i][0];++j) pre[i]++;
		for(int j=len[i]-1;~j&&s[i][j]==s[i][len[i]-1];--j) suf[i]++;
//		cout<<len[i]<<' '<<pre[i]<<' '<<suf[i]<<endl;
		memcpy(ocu[i],ocu[i-1],sizeof(ocu[i]));
		for(int j=0;j<len[i];++j) ocu[i][idx(s[i][j])]=1;
	}
//	for(int i=1;i<=n;++i)
//	{
//		for(int j=0;j<len[i];++j) cout<<((int)s[i][j])<<' '; cout<<endl;
//	}
	int ans=0;
	if(pre[n]==len[n]) ans=solve(n-1,s[n][0])*(len[n]+1)+len[n];
	ans=max(max(pre[n]+ocu[n-1][idx(s[n][0])],suf[n]+ocu[n-1][idx(s[n][len[n]-1])]),ans);
	if(s[n][0]==s[n][len[n]-1]&&ocu[n-1][idx(s[n][0])]) ans=max(ans,pre[n]+suf[n]+1);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void read(T &x)
{
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=getchar();}
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=1e5+10;
inline int idx(char c){return c-'a';}
string s[N];
int pre[N],suf[N],len[N],ocu[N][26],n;
int solve(int cur,char c)
{
	if(cur==1)
	{
		int ans=0,now=0;
		for(int j=0;j<len[cur];++j)
		{
			if(s[cur][j]==c) now++;
			else now=0;
			ans=max(ans,now);
		}
		return ans;
	}
	if(pre[cur]==len[cur]) return s[cur][0]==c?solve(cur-1,c)*(len[cur]+1)+len[cur]:0;
	if(s[cur][0]==c&&s[cur][0]==s[cur][len[cur]-1]&&ocu[cur-1][idx(c)]) return pre[cur]+suf[cur]+1;
	int ans=0;
	if(s[cur][0]==c) ans=max(ans,pre[cur]+ocu[cur-1][idx(c)]);
	if(s[cur][len[cur]-1]==c) ans=max(ans,suf[cur]+ocu[cur-1][idx(c)]);
	return ans;
}
int main()
{
	read(n);
	for(int i=1;i<=n;++i)
	{
		cin>>s[i];
		len[i]=s[i].length();
		for(int j=0;j<len[i]&&s[i][j]==s[i][0];++j) pre[i]++;
		for(int j=len[i]-1;~j&&s[i][j]==s[i][len[i]-1];--j) suf[i]++;
//		cout<<len[i]<<' '<<pre[i]<<' '<<suf[i]<<endl;
		memcpy(ocu[i],ocu[i-1],sizeof(ocu[i]));
		for(int j=0;j<len[i];++j) ocu[i][idx(s[i][j])]=1;
	}
//	for(int i=1;i<=n;++i)
//	{
//		for(int j=0;j<len[i];++j) cout<<((int)s[i][j])<<' '; cout<<endl;
//	}
	int ans=0;
	if(pre[n]==len[n]) ans=solve(n-1,s[n][0])*(len[n]+1)+len[n];
	ans=max(max(pre[n]+ocu[n-1][idx(s[n][0])],suf[n]+ocu[n-1][idx(s[n][len[n]-1])]),ans);
	if(s[n][0]==s[n][len[n]-1]&&ocu[n-1][idx(s[n][0])]) ans=max(ans,pre[n]+suf[n]+1);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void read(T &x)
{
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=getchar();}
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=1e5+10;
inline void MAX(int &x,int y){if(y>x) x=y;}
inline int idx(char c){return c-'a';}
string s[N];
int pre[N],suf[N],len[N],ocu[N][26],bst[N][26],n;
int solve(int cur,char c)
{
	if(cur==1) return bst[cur][idx(c)];
	int ans=(ocu[cur-1][idx(c)],bst[cur][idx(c)]);
	if(pre[cur]==len[cur]) ans=max(ans,s[cur][0]==c?solve(cur-1,c)*(len[cur]+1)+len[cur]:0);
	if(s[cur][0]==c&&s[cur][0]==s[cur][len[cur]-1]&&ocu[cur-1][idx(c)]) ans=max(ans,pre[cur]+suf[cur]+1);
	if(s[cur][0]==c) ans=max(ans,pre[cur]+ocu[cur-1][idx(c)]);
	if(s[cur][len[cur]-1]==c) ans=max(ans,suf[cur]+ocu[cur-1][idx(c)]);
	return ans;
}
int main()
{
	read(n);
	for(int i=1;i<=n;++i)
	{
		cin>>s[i];
		len[i]=s[i].length();
		for(int j=0;j<len[i]&&s[i][j]==s[i][0];++j) pre[i]++;
		for(int j=len[i]-1;~j&&s[i][j]==s[i][len[i]-1];--j) suf[i]++;
		int now=0;
		for(int j=0;j<len[i];++j)
		{
			if(!j||s[i][j]==s[i][j-1]) now++; else now=1;
			MAX(bst[i][idx(s[i][j])],now);
		}

//		cout<<len[i]<<' '<<pre[i]<<' '<<suf[i]<<endl;
		memcpy(ocu[i],ocu[i-1],sizeof(ocu[i]));
		for(int j=0;j<len[i];++j) ocu[i][idx(s[i][j])]=1;
	}
//	for(int i=1;i<=n;++i)
//	{
//		for(int j=0;j<len[i];++j) cout<<((int)s[i][j])<<' '; cout<<endl;
//	}
	int ans=0;
	for(int i=0;i<26;++i) ans=max(ans,bst[n][i]);
	if(pre[n]==len[n]) ans=solve(n-1,s[n][0])*(len[n]+1)+len[n];
	ans=max(max(pre[n]+ocu[n-1][idx(s[n][0])],suf[n]+ocu[n-1][idx(s[n][len[n]-1])]),ans);
	if(s[n][0]==s[n][len[n]-1]&&ocu[n-1][idx(s[n][0])]) ans=max(ans,pre[n]+suf[n]+1);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void read(T &x)
{
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=getchar();}
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=1e5+10;
inline int idx(char c){return c-'a';}
string s[N];
int pre[N],suf[N],len[N],ocu[N][26],n;
int solve(int cur,char c)
{
	if(cur==1)
	{
		int ans=0,now=0;
		for(int j=0;j<len[cur];++j)
		{
			if(s[cur][j]==c) now++;
			else now=0;
			ans=max(ans,now);
		}
		return ans;
	}
	int ans=ocu[cur-1][idx(c)];
	if(pre[cur]==len[cur]) ans=max(ans,s[cur][0]==c?solve(cur-1,c)*(len[cur]+1)+len[cur]:0);
	if(s[cur][0]==c&&s[cur][0]==s[cur][len[cur]-1]&&ocu[cur-1][idx(c)]) ans=max(ans,pre[cur]+suf[cur]+1);
	if(s[cur][0]==c) ans=max(ans,pre[cur]+ocu[cur-1][idx(c)]);
	if(s[cur][len[cur]-1]==c) ans=max(ans,suf[cur]+ocu[cur-1][idx(c)]);
	return ans;
}
int main()
{
	read(n);
	for(int i=1;i<=n;++i)
	{
		cin>>s[i];
		len[i]=s[i].length();
		for(int j=0;j<len[i]&&s[i][j]==s[i][0];++j) pre[i]++;
		for(int j=len[i]-1;~j&&s[i][j]==s[i][len[i]-1];--j) suf[i]++;
//		cout<<len[i]<<' '<<pre[i]<<' '<<suf[i]<<endl;
		memcpy(ocu[i],ocu[i-1],sizeof(ocu[i]));
		for(int j=0;j<len[i];++j) ocu[i][idx(s[i][j])]=1;
	}
//	for(int i=1;i<=n;++i)
//	{
//		for(int j=0;j<len[i];++j) cout<<((int)s[i][j])<<' '; cout<<endl;
//	}
	int ans=0;
	if(pre[n]==len[n]) ans=solve(n-1,s[n][0])*(len[n]+1)+len[n];
	ans=max(max(pre[n]+ocu[n-1][idx(s[n][0])],suf[n]+ocu[n-1][idx(s[n][len[n]-1])]),ans);
	if(s[n][0]==s[n][len[n]-1]&&ocu[n-1][idx(s[n][0])]) ans=max(ans,pre[n]+suf[n]+1);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 1e5+5;
int n;
char s[MAXN],*sp;
pair<int,int> stk[MAXN];
int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

int main(){
    scanf("%d",&n);getchar();
    sp=s;
    for(int i=0;i<n;i++){
        scanf("%s",sp);
        //read(sp);
        stk[++top]={sp-s,strlen(sp)};
        sp+=strlen(sp);
    }

    int p=stk[top].first;
    int l=stk[top].second;

    cPer=s[p];
    cSur=s[p+l-1];
    per=0;sur=0;isCon=0;
    for(int i=p;i<p+l;i++)
        if(s[i]==cPer)per++;
        else break;
    for(int i=p+l-1;i>=p;i--)
        if(s[i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=0;
    memset(tmp,0,sizeof tmp);
    for(int i=p;i<p+l;i++){
        if(cur+'a'!=s[i]){
            tmp[cur]=0;
            cur=s[i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    while(--top){
        p=stk[top].first;
        l=stk[top].second;

        if(isCon){
            int t=per;
            for(int i=p;i<p+l;i++){
                if(cur+'a'!=s[i]){
                    tmp[cur]=0;
                    cur=s[i]-'a';
                }
                ++tmp[cur];
                if(s[i]!=cPer)continue;
                cMax[cur]=max((1+tmp[cur])*t+tmp[cur],cMax[cur]);
            }tmp[cur]=0;

            int i;
            for(i=p;i<p+l;i++)
                if(s[i]==cPer)per+=t+1;
                else break;
            if(i<p+l)isCon=false;
            for(i=p+l-1;i>=p;i--)
                if(s[i]==cSur)sur+=t+1;
                else break;

            continue;
        }
        //printf("::%d\n",cMax[0]);
        for(int i=p;i<p+l;i++){
            if(s[i]==cPer&&s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1+per);
            }else if(s[i]==cPer){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],1+per);
            }else if(s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1);
            }
        }
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    printf("%d\n",ans);
    return 0;
}
/*
3
a
a
a
7

2
aaa
aaa
15

2
aabaaabaa
b
3

2
aabbbbaa
b
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 1e5+5;
int n;
char s[MAXN],*sp;
pair<int,int> stk[MAXN];
int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

int main(){
    scanf("%d",&n);getchar();
    sp=s;
    for(int i=0;i<n;i++){
        scanf("%s",sp);
        //read(sp);
        stk[++top]={sp-s,strlen(sp)};
        sp+=strlen(sp);
    }

    int p=stk[top].first;
    int l=stk[top].second;

    cPer=s[p];
    cSur=s[p+l-1];
    per=0;sur=0;isCon=0;
    for(int i=p;i<p+l;i++)
        if(s[i]==cPer)per++;
        else break;
    for(int i=p+l-1;i>=p;i--)
        if(s[i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=0;
    memset(tmp,0,sizeof tmp);
    for(int i=p;i<p+l;i++){
        if(cur+'a'!=s[i]){
            tmp[cur]=0;
            cur=s[i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    while(--top){
        p=stk[top].first;
        l=stk[top].second;

        //printf("::%d\n",cMax[0]);
        for(int i=p;i<p+l;i++){
            if(s[i]==cPer&&s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1+per);
            }else if(s[i]==cPer){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],1+per);
            }else if(s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1);
            }
        }
        if(isCon){
            int i,t=per;
            for(i=p;i<p+l;i++)
                if(s[i]==cPer)per+=t+1;
                else break;
            if(i<p+l)isCon=false;
            for(i=p+l-1;i>=p;i--)
                if(s[i]==cSur)sur+=t+1;
                else break;
            //continue;
        }
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    printf("%d\n",ans);
    return 0;
}
/*
3
a
a
a
7

2
aaa
aaa
15

2
aabaaabaa
b
3

*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 2e5+5;
int n;
//char s[MAXN],*sp;
string s[MAXN];
//pair<int,int> stk[MAXN];
//int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

int main(){
    ios::sync_with_stdio(false);
    //cin.tie(false);cout.tie(false);

    cin>>n;
    for(int i=1;i<=n;i++){
        //scanf("%s",sp);
        cin>>s[i];
        //stk[++top]={sp-s,strlen(sp)};
        //sp+=strlen(sp);
    }

    //int p=stk[top].first;
    //int l=stk[top].second;

    int l=s[n].length();
    cPer=s[n][0];
    cSur=s[n][l-1];
    per=0;sur=0;isCon=0;
    for(int i=0;i<l;i++)
        if(s[n][i]==cPer)per++;
        else break;
    for(int i=l-1;i>=0;i--)
        if(s[n][i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=0;
    memset(tmp,0,sizeof tmp);
    for(int i=0;i<l;i++){
        if(cur+'a'!=s[n][i]){
            tmp[cur]=0;
            cur=s[n][i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    for(int top=n-1;top>0;top--){
        l=s[top].length();
        if(isCon){
            int i,t=per;
            for(i=0;i<l;i++)
                if(s[top][i]==cPer)per+=t+1;
                else break;
            if(i<l)isCon=false;
            for(i=l-1;i>=0;i--)
                if(s[top][i]==cSur)sur+=t+1;
                else break;
            continue;
        }
        //printf("::%d\n",cMax[0]);
        for(int i=0;i<l;i++){
            if(s[top][i]==cPer&&s[top][i]==cSur){
                cMax[s[top][i]-'a']=max(cMax[s[top][i]-'a'],sur+1+per);
            }else if(s[top][i]==cPer){
                cMax[s[top][i]-'a']=max(cMax[s[top][i]-'a'],1+per);
            }else if(s[top][i]==cSur){
                cMax[s[top][i]-'a']=max(cMax[s[top][i]-'a'],sur+1);
            }
        }

        //printf("->%d\n",cMax[0]);
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    cout<<ans<<endl;
    return 0;
}
/*
3
a
a
a

2
aaa
aaa

*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 2e5+5;
int n;
//char s[MAXN],*sp;
string s[MAXN];
//pair<int,int> stk[MAXN];
//int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

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

    cin>>n;
    for(int i=1;i<=n;i++){
        //scanf("%s",sp);
        cin>>s[i];
        //stk[++top]={sp-s,strlen(sp)};
        //sp+=strlen(sp);
    }

    //int p=stk[top].first;
    //int l=stk[top].second;

    int l=s[n].length();
    cPer=s[n][0];
    cSur=s[n][l-1];
    per=0;sur=0;isCon=0;
    for(int i=0;i<l;i++)
        if(s[n][i]==cPer)per++;
        else break;
    for(int i=l-1;i>=0;i--)
        if(s[n][i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=0;
    memset(tmp,0,sizeof tmp);
    for(int i=0;i<l;i++){
        if(cur+'a'!=s[n][i]){
            tmp[cur]=0;
            cur=s[n][i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    for(int top=n-1;top>0;top--){
        l=s[top].length();
        if(isCon){
            int i,t=per;
            for(i=0;i<l;i++)
                if(s[top][i]==cPer)per+=t+1;
                else break;
            if(i<l)isCon=false;
            for(i=l-1;i>=0;i--)
                if(s[top][i]==cSur)sur+=t+1;
                else break;
            continue;
        }
        //printf("::%d\n",cMax[0]);
        for(int i=0;i<l;i++){
            if(s[top][i]==cPer&&s[top][i]==cSur){
                cMax[s[top][i]-'a']=max(cMax[s[top][i]-'a'],sur+1+per);
            }else if(s[top][i]==cPer){
                cMax[s[top][i]-'a']=max(cMax[s[top][i]-'a'],1+per);
            }else if(s[top][i]==cSur){
                cMax[s[top][i]-'a']=max(cMax[s[top][i]-'a'],sur+1);
            }
        }

        //printf("->%d\n",cMax[0]);
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    cout<<ans<<endl;
    return 0;
}
/*
3
a
a
a

2
aaa
aaa

*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 1e5+5;
int n;
char s[MAXN],*sp;
pair<int,int> stk[MAXN];
int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

int main(){
    scanf("%d",&n);
    sp=s;
    for(int i=0;i<n;i++){
        scanf("%s",sp);
        stk[++top]={sp-s,strlen(sp)};
        sp+=strlen(sp);
    }

    int p=stk[top].first;
    int l=stk[top].second;

    cPer=s[p];
    cSur=s[p+l-1];
    per=0;sur=0;isCon=0;
    for(int i=p;i<p+l;i++)
        if(s[i]==cPer)per++;
        else break;
    for(int i=p+l-1;i>=p;i--)
        if(s[i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=0;
    memset(tmp,0,sizeof tmp);
    for(int i=p;i<p+l;i++){
        if(cur+'a'!=s[i]){
            tmp[cur]=0;
            cur=s[i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    while(--top){
        p=stk[top].first;
        l=stk[top].second;
        if(isCon){
            int i,t=per;
            for(i=p;i<p+l;i++)
                if(s[i]==cPer)per+=t+1;
                else break;
            if(i<p+l)isCon=false;
            for(i=p+l-1;i>=p;i--)
                if(s[i]==cSur)sur+=t+1;
                else break;
            continue;
        }
        //printf("::%d\n",cMax[0]);
        for(int i=p;i<p+l;i++){
            if(s[i]==cPer&&s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1+per);
            }else if(s[i]==cPer){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],1+per);
            }else if(s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1);
            }
        }

        //printf("->%d\n",cMax[0]);
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    printf("%d\n",ans);
    return 0;
}
/*
3
a
a
a

2
aaa
aaa

*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 1e5+5;
int n;
char s[MAXN],*sp;
pair<int,int> stk[MAXN];
int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

int main(){
    scanf("%d",&n);
    sp=s;
    for(int i=0;i<n;i++){
        scanf("%s",sp);
        stk[++top]={sp-s,strlen(sp)};
        sp+=strlen(sp);
    }

    int p=stk[top].first;
    int l=stk[top].second;

    cPer=s[p];
    cSur=s[p+l-1];
    per=0;sur=0;isCon=0;
    for(int i=p;i<p+l;i++)
        if(s[i]==cPer)per++;
        else break;
    for(int i=p+l-1;i>=p;i--)
        if(s[i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=-1;
    memset(tmp,0,sizeof tmp);
    for(int i=p;i<p+l;i++){
        if(cur+'a'!=s[i]){
            tmp[cur]=0;
            cur=s[i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    while(--top){
        p=stk[top].first;
        l=stk[top].second;
        if(isCon){
            int i,t=per;
            for(i=p;i<p+l;i++)
                if(s[i]==cPer)per+=t+1;
                else break;
            if(i<p+l)isCon=false;
            for(i=p+l-1;i>=p;i--)
                if(s[i]==cSur)sur+=t+1;
                else break;
            continue;
        }
        //printf("::%d\n",cMax[0]);
        for(int i=p;i<p+l;i++){
            if(s[i]==cPer&&s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1+per);
            }else if(s[i]==cPer){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],1+per);
            }else if(s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1);
            }
        }

        //printf("->%d\n",cMax[0]);
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    printf("%d\n",ans);
    return 0;
}
/*
3
a
a
a

2
aaa
aaa

*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN= 2e5+5;
int n;
char s[MAXN],*sp;
pair<int,int> stk[MAXN];
int top;
char cPer,cSur;
int per,sur,isCon;
int cMax[26],ans;

int main(){
    scanf("%d",&n);getchar();
    sp=s;
    for(int i=0;i<n;i++){
        scanf("%s",sp);
        //read(sp);
        stk[++top]={sp-s,strlen(sp)};
        sp+=strlen(sp);
    }

    int p=stk[top].first;
    int l=stk[top].second;

    cPer=s[p];
    cSur=s[p+l-1];
    per=0;sur=0;isCon=0;
    for(int i=p;i<p+l;i++)
        if(s[i]==cPer)per++;
        else break;
    for(int i=p+l-1;i>=p;i--)
        if(s[i]==cSur)sur++;
        else break;
    int tmp[26];
    int cur=0;
    memset(tmp,0,sizeof tmp);
    for(int i=p;i<p+l;i++){
        if(cur+'a'!=s[i]){
            tmp[cur]=0;
            cur=s[i]-'a';
        }
        cMax[cur]=max(++tmp[cur],cMax[cur]);
    }tmp[cur]=0;
    if(per==l)isCon=1;

    while(--top){
        p=stk[top].first;
        l=stk[top].second;
        if(isCon){
            int i,t=per;
            for(i=p;i<p+l;i++)
                if(s[i]==cPer)per+=t+1;
                else break;
            if(i<p+l)isCon=false;
            for(i=p+l-1;i>=p;i--)
                if(s[i]==cSur)sur+=t+1;
                else break;
            continue;
        }
        //printf("::%d\n",cMax[0]);
        for(int i=p;i<p+l;i++){
            if(s[i]==cPer&&s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1+per);
            }else if(s[i]==cPer){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],1+per);
            }else if(s[i]==cSur){
                cMax[s[i]-'a']=max(cMax[s[i]-'a'],sur+1);
            }
        }

        //printf("->%d\n",cMax[0]);
    }

    ans=max(per,sur);
    for(int i=0;i<26;i++)
        ans=max(ans,cMax[i]);
    printf("%d\n",ans);
    return 0;
}
/*
3
a
a
a

2
aaa
aaa

*/


#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
const int N=1e5+5;
int n,m,a[26],b[26];
char s[N];
int len,p1,p2;
void cal()
{
    memset(a,0,sizeof(a));
    len=strlen(s);
    p1=p2=1;
    int pos=1;
   // cout<<"###"<<len<<endl;
    while(pos<len&&s[pos]==s[pos-1])p1++,pos++;
   // cout<<"SSSS"<<endl;
    pos=len-2;
    while(pos>=0&&s[pos]==s[pos+1])p2++,pos--;

    int cn=0;
    for(int i=1;i<len;i++)
    {
        if(s[i]!=s[i-1])
        {
            a[s[i-1]-'a']=max(a[s[i-1]-'a'],cn);
        }
        else cn++;
    }
    a[s[len-1]-'a']=max(a[s[len-1]-'a'],cn);
}
void view()
{
    for(int i=0;i<26;i++)
    {
        cout<<b[i]<<" ";
    }cout<<endl;
}
int main()
{
  //  cout<<'n'-'a'<<endl;
    scanf("%d",&n);
  //  getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        cal();
       // cout<<"SSS"<<p1<<" "<<p2<<" "<<len<<endl;
        if(p1+p2>len)
        {
            b[s[0]-'a']=b[s[0]-'a']*(len+1)+len;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else if(s[0]==s[len-1])
        {
            if(b[s[0]-'a']) b[s[0]-'a']=p1+p2+1;
            else b[s[0]-'a']=max(p1,p2);
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else
        {
            b[s[0]-'a']=(b[s[0]-'a']?1:0)+p1;
            b[s[len-1]-'a']=(b[s[len-1]-'a']?1:0)+p2;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a'||j==s[len-1]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
     //   view();
    }
    int ans=b[0];
    for(int i=1;i<26;i++)
    {
        ans=max(ans,b[i]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
const int N=1e5+5;
int n,m;
ll a[26],b[26];
char s[N];
int len,p1,p2;
void cal()
{
    memset(a,0,sizeof(a));
    len=strlen(s);
    p1=p2=1;
    int pos=1;
    while(pos<len&&s[pos]==s[pos-1])p1++,pos++;
    pos=len-2;
    while(pos>=0&&s[pos]==s[pos+1])p2++,pos--;

    int cn=1;
    for(int i=1;i<len;i++)
    {
        if(s[i]!=s[i-1])
        {
            a[s[i-1]-'a']=max(a[s[i-1]-'a'],1ll*cn);
            cn=1;
        }
        else cn++;
    }
    a[s[len-1]-'a']=max(a[s[len-1]-'a'],1ll*cn);
}
void view()
{
    for(int i=0;i<26;i++)
    {
        cout<<b[i]<<" ";
    }cout<<endl;
}
int main()
{
  //  cout<<'n'-'a'<<endl;
    scanf("%d",&n);
  //  getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        cal();
       // cout<<"SSS"<<p1<<" "<<p2<<" "<<len<<endl;
        if(p1+p2>len)
        {
            b[s[0]-'a']=b[s[0]-'a']*(len+1)+len;
            if(b[s[0]-'a']>1e9)b[s[0]-'a']=1e9;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else if(s[0]==s[len-1])
        {
            if(b[s[0]-'a']) b[s[0]-'a']=p1+p2+1;
            else b[s[0]-'a']=max(p1,p2);
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else
        {
            b[s[0]-'a']=(b[s[0]-'a']?1:0)+p1;
            b[s[len-1]-'a']=(b[s[len-1]-'a']?1:0)+p2;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a'||j==s[len-1]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
     //   view();
    }
    ll ans=b[0];
    for(int i=1;i<26;i++)
    {
        ans=max(ans,b[i]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
const int N=1e5+5;
int n,m;
ll a[26],b[26];
char s[N];
int len,p1,p2;
void cal()
{
    memset(a,0,sizeof(a));
    len=strlen(s);
    p1=p2=1;
    int pos=1;
   // cout<<"###"<<len<<endl;
    while(pos<len&&s[pos]==s[pos-1])p1++,pos++;
   // cout<<"SSSS"<<endl;
    pos=len-2;
    while(pos>=0&&s[pos]==s[pos+1])p2++,pos--;

    int cn=0;
    for(int i=1;i<len;i++)
    {
        if(s[i]!=s[i-1])
        {
            a[s[i-1]-'a']=max(a[s[i-1]-'a'],1ll*cn);
        }
        else cn++;
    }
    a[s[len-1]-'a']=max(a[s[len-1]-'a'],1ll*cn);
}
void view()
{
    for(int i=0;i<26;i++)
    {
        cout<<b[i]<<" ";
    }cout<<endl;
}
int main()
{
  //  cout<<'n'-'a'<<endl;
    scanf("%d",&n);
  //  getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        cal();
       // cout<<"SSS"<<p1<<" "<<p2<<" "<<len<<endl;
        if(p1+p2>len)
        {
            b[s[0]-'a']=b[s[0]-'a']*(len+1)+len;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else if(s[0]==s[len-1])
        {
            if(b[s[0]-'a']) b[s[0]-'a']=p1+p2+1;
            else b[s[0]-'a']=max(p1,p2);
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else
        {
            b[s[0]-'a']=(b[s[0]-'a']?1:0)+p1;
            b[s[len-1]-'a']=(b[s[len-1]-'a']?1:0)+p2;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a'||j==s[len-1]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
     //   view();
    }
    ll ans=b[0];
    for(int i=1;i<26;i++)
    {
        ans=max(ans,b[i]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
const int N=1e5+5;
int n,m;
ll a[26],b[26];
char s[N];
int len,p1,p2;
void cal()
{
    memset(a,0,sizeof(a));
    len=strlen(s);
    p1=p2=1;
    int pos=1;
   // cout<<"###"<<len<<endl;
    while(pos<len&&s[pos]==s[pos-1])p1++,pos++;
   // cout<<"SSSS"<<endl;
    pos=len-2;
    while(pos>=0&&s[pos]==s[pos+1])p2++,pos--;

    int cn=0;
    for(int i=1;i<len;i++)
    {
        if(s[i]!=s[i-1])
        {
            a[s[i-1]-'a']=max(a[s[i-1]-'a'],1ll*cn);
        }
        else cn++;
    }
    a[s[len-1]-'a']=max(a[s[len-1]-'a'],1ll*cn);
}
void view()
{
    for(int i=0;i<26;i++)
    {
        cout<<b[i]<<" ";
    }cout<<endl;
}
int main()
{
  //  cout<<'n'-'a'<<endl;
    scanf("%d",&n);
  //  getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        cal();
       // cout<<"SSS"<<p1<<" "<<p2<<" "<<len<<endl;
        if(p1+p2>len)
        {
            b[s[0]-'a']=b[s[0]-'a']*(len+1)+len;
            if(b[s[0]-'a']>1e9)b[s[0]-'a']=1e9;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else if(s[0]==s[len-1])
        {
            if(b[s[0]-'a']) b[s[0]-'a']=p1+p2+1;
            else b[s[0]-'a']=max(p1,p2);
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
        else
        {
            b[s[0]-'a']=(b[s[0]-'a']?1:0)+p1;
            b[s[len-1]-'a']=(b[s[len-1]-'a']?1:0)+p2;
            for(int j=0;j<26;j++)
            {
                if(j==s[0]-'a'||j==s[len-1]-'a')continue;
                b[j]=b[j]?1:0;
                b[j]=max(b[j],a[j]);
            }
        }
     //   view();
    }
    ll ans=b[0];
    for(int i=1;i<26;i++)
    {
        ans=max(ans,b[i]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
        int len = strlen(str);
//        cout<<len<<endl;
        int cnt = 1;
        int l = 0, r = 0;
        memset(now, 0, sizeof(now));
        for(int i = 1; i < len; i++)
        {
            if(i==7)
                int okk = 1;
            if(str[i] != str[i - 1])
            {
                if(i == len - 1 && str[i] == str[i - 1])
                    cnt++;
                if(l == 0)
                    l = cnt;
                else if(l != 0 && i != len - 1)
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                cnt = 1;
            }

            else if(str[i] == str[i - 1])
                cnt++;
        }
        r = cnt;
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[len-1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int ans[30], pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d",&n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
        int len = strlen(str);
        int cnt = 1;
        int l = 0, r = 0;
        for(int i = 1; i <= len; i++)
        {
            memset(now, 0, sizeof(now));
            if(str[i] != str[i - 1] || i == len - 1)
            {
                if(i==len-1&&str[i]==str[i-1]) cnt++;
                ans[str[i - 1] - 'a'] = max(ans[str[i - 1] - 'a'], cnt);
                if(l == 0)
                {
                    l = cnt;
                }
                if(i == len - 1)
                {
                    r = cnt;
                }
                if(l != 0 && i != len - 1)
                {
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                }
                cnt = 1;
                i++;
            }
            else if(str[i] == str[i - 1])
            {
                cnt++;
            }
        }

        if(j == 0)
        {
            for(int i = 0; i < 26; i++)
                pre[i] = ans[i];
            continue;
        }
        if(j==n-1)
            int ok = 1;
        if(cnt == len)
        {
            int t = str[0] - 'a';
            ans[t] = max(ans[t], pre[t] + (pre[t] + 1) * len);
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
            }
        }
        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(now[t], l + r + 1);
            }
            else
            {
                pre[t] = max(max(l, r), now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
        else
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + l, now[t]);
            }
            else
            {
                pre[t] = max(l, now[t]);
            }
            t = str[len - 1] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + r, now[t]);
            }
            else
            {
                pre[t] = max(r, now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != str[0] - 'a' && i != str[1] - 'a')
                {
                    if(pre[i])
                    {
                        pre[i] = max(1, now[i]);
                    }
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
    }
    int anss = -1;
    for(int i = 0; i < 26; i++)
    {
        anss = max(anss, pre[i]);
    }
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
        int len = strlen(str);
//        cout<<len<<endl;
        int cnt = 1;
        int l = 0, r = 0;
        memset(now, 0, sizeof(now));
        for(int i = 1; i < len; i++)
        {
            if(str[i] != str[i - 1] || i == len - 1)
            {
                if(i == len - 1 && str[i] == str[i - 1])
                    cnt++;
                if(l == 0)
                    l = cnt;
                else if(l != 0 && i != len - 1)
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                if(i==len-1)
                {
                    r = cnt;
                    break;
                }
                cnt = 1;
                i++;
            }

            else if(str[i] == str[i - 1])
                cnt++;
        }
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[len-1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
//        if(j==1)
//            int ok = 1;
        int len = strlen(str);
//        cout<<len<<endl;
        int cnt = 1;
        int l = 0, r = 0;
        memset(now, 0, sizeof(now));
        for(int i = 1; i < len; i++)
        {
            if(str[i] != str[i - 1])
            {
                if(i == len - 1 && str[i] == str[i - 1])
                    cnt++;
                if(l == 0)
                    l = cnt;
                else if(l != 0 && i != len)
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                cnt = 1;
            }

            else if(str[i] == str[i - 1])
                cnt++;
        }
        r = cnt;
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[len-1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int l, r, len, cnt;

void sov()
{
    scanf("%s", str);
    len = strlen(str);
    cnt = 1; l = 0, r = 0;
    memset(now, 0, sizeof(now));

    int i = 1;
    while(i < len)
    {
        if(str[i] == str[i - 1]) cnt++;
        else break;
        i++;
    }
    if(cnt == len) return;
    l = cnt; cnt = 1;

    int j = len - 2;
    while(j >= 0)
    {
        if(str[j] == str[j + 1]) cnt++;
        else break;
        j--;
    }
    if(cnt + l == len) return;
    r = cnt; cnt = 1;

    for(++i; i <= j + 1; i++)
    {
        if(str[i] != str[i - 1])
        {
            now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
            cnt = 1;
        }

        else if(str[i] == str[i - 1])
            cnt++;
    }

}

int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        sov();
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[len - 1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
        int len = strlen(str);
//        cout<<len<<endl;
        int cnt = 1;
        int l = 0, r = 0;
        memset(now, 0, sizeof(now));
        for(int i = 1; i < len; i++)
        {
            if(str[i] != str[i - 1] || i == len - 1)
            {
                if(i == len - 1 && str[i] == str[i - 1])
                    cnt++;
                if(l == 0)
                    l = cnt;
                else if(l != 0 && i != len - 1)
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                if(i==len-1)
                {
                    r = cnt;
                    break;
                }
                cnt = 1;
                i++;
            }

            else if(str[i] == str[i - 1])
                cnt++;
        }
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int l, r, len, cnt;

void sov()
{
    scanf("%s", str);
    len = strlen(str);
    cnt = 1; l = 0, r = 0;
    memset(now, 0, sizeof(now));

    int i = 1;
    while(i < len)
    {
        if(str[i] == str[i - 1]) cnt++;
        else break;
        i++;
    }
    if(cnt == len) return;
    l = cnt; cnt = 1;

    int j = len - 2;
    while(j >= 0)
    {
        if(str[j] == str[j + 1]) cnt++;
        else break;
        j--;
    }
    r = cnt; if(r + l == len) return;
    cnt = 1;

    for(++i; i <= j + 1; i++)
    {
        if(str[i] != str[i - 1])
        {
            now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
            cnt = 1;
        }
        else if(str[i] == str[i - 1])
            cnt++;
    }

}

int main()
{
//    freopen("in.txt", "r", stdin);
    int n; scanf("%d", &n);

    for(int j = 0; j < n; j++)
    {
        sov();
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[len - 1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int ans = -1;
    for(int i = 0; i < 26; i++)
        ans = max(ans, pre[i]);
    printf("%d\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int ans[30], pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d",&n);
    for(int j = 0; j < n; j++)
    {
        if(j==n-1)
            int ok = 1;
        scanf("%s", str);
        int len = strlen(str);
        int cnt = 1;
        int l = 0, r = 0;
        for(int i = 1; i <= len; i++)
        {
            memset(now, 0, sizeof(now));
            if(str[i] != str[i - 1] || i == len - 1)
            {
                if(i==len-1&&str[i]==str[i-1]) cnt++;
                ans[str[i - 1] - 'a'] = max(ans[str[i - 1] - 'a'], cnt);
                if(l == 0)
                {
                    l = cnt;
                }
                if(i == len - 1)
                {
                    r = cnt;
                }
                if(l != 0 && i != len - 1)
                {
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                }
                cnt = 1;
                i++;
            }
            else if(str[i] == str[i - 1])
            {
                cnt++;
            }
        }

        if(j == 0)
        {
            for(int i = 0; i < 26; i++)
                pre[i] = ans[i];
            continue;
        }
        if(cnt == len)
        {
            int t = str[0] - 'a';
            ans[t] = max(ans[t], pre[t] + (pre[t] + 1) * len);
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[t])
                        pre[t] = 1;
                    else
                        pre[t] = 0;
                }
            }
        }
        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(now[t], l + r + 1);
            }
            else
            {
                pre[t] = max(max(l, r), now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
        else
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + l, now[t]);
            }
            else
            {
                pre[t] = max(l, now[t]);
            }
            t = str[len - 1] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + r, now[t]);
            }
            else
            {
                pre[t] = max(r, now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != str[0] - 'a' && i != str[1] - 'a')
                {
                    if(pre[i])
                    {
                        pre[i] = max(1, now[i]);
                    }
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
    }
    int anss = -1;
    for(int i = 0; i < 26; i++)
    {
        anss = max(anss, pre[i]);
    }
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int ans[30], pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d",&n);
    for(int j = 0; j < n; j++)
    {
        if(j==n-1)
            int ok = 1;
        scanf("%s", str);
        int len = strlen(str);
        int cnt = 1;
        int l = 0, r = 0;
        for(int i = 1; i <= len; i++)
        {
            memset(now, 0, sizeof(now));
            if(str[i] != str[i - 1] || i == len - 1)
            {
                if(i==len-1&&str[i]==str[i-1]) cnt++;
                ans[str[i - 1] - 'a'] = max(ans[str[i - 1] - 'a'], cnt);
                if(l == 0)
                {
                    l = cnt;
                }
                if(i == len - 1)
                {
                    r = cnt;
                }
                if(l != 0 && i != len - 1)
                {
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                }
                cnt = 1;
                i++;
            }
            else if(str[i] == str[i - 1])
            {
                cnt++;
            }
        }

        if(j == 0)
        {
            for(int i = 0; i < 26; i++)
                pre[i] = ans[i];
            continue;
        }
        if(cnt == len)
        {
            int t = str[0] - 'a';
            ans[t] = max(ans[t], pre[t] + (pre[t] + 1) * len);
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[t])
                        pre[t] = 1;
                    else
                        pre[t] = 0;
                }
            }
        }
        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(now[t], l + r + 1);
            }
            else
            {
                pre[t] = max(max(l, r), now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
        else
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + l, now[t]);
            }
            else
            {
                pre[t] = max(l, now[t]);
            }
            t = str[len - 1] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + r, now[t]);
            }
            else
            {
                pre[t] = max(r, now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != str[0] - 'a' && i != str[1] - 'a')
                {
                    if(pre[i])
                    {
                        pre[i] = max(1, now[i]);
                    }
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
    }
    int anss = -1;
    for(int i = 0; i < 26; i++)
    {
        anss = max(anss, ans[i]);
    }
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int ans[30], pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d",&n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
        int len = strlen(str);
        int cnt = 1;
        int l = 0, r = 0;
        for(int i = 1; i <= len; i++)
        {
            memset(now, 0, sizeof(now));
            if(str[i] != str[i - 1] || i == len - 1)
            {
                ans[str[i - 1] - 'a'] = max(ans[str[i - 1] - 'a'], cnt);
                if(l == 0)
                {
                    l = cnt;
                }
                if(i == len - 1)
                {
                    r = cnt;
                }
                if(l != 0 && i != len - 1)
                {
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                }
                cnt = 1;
                i++;
            }
            else if(str[i] == str[i - 1])
            {
                cnt++;
            }
        }

        if(j == 0)
        {
            for(int i = 0; i < 26; i++)
                pre[i] = ans[i];
            continue;
        }
        if(cnt == len)
        {
            int t = str[0] - 'a';
            ans[t] = max(ans[t], pre[t] + (pre[t] + 1) * len);
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[t])
                        pre[t] = 1;
                    else
                        pre[t] = 0;
                }
            }
        }
        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(now[t], l + r + 1);
            }
            else
            {
                pre[t] = max(max(l, r), now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
        else
        {
            int t = str[0] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + l, now[t]);
            }
            else
            {
                pre[t] = max(l, now[t]);
            }
            t = str[len - 1] - 'a';
            if(pre[t])
            {
                pre[t] = max(1 + r, now[t]);
            }
            else
            {
                pre[t] = max(r, now[t]);
            }
            for(int i = 0; i < 26; i++)
            {
                if(i != str[0] - 'a' && i != str[1] - 'a')
                {
                    if(pre[i])
                    {
                        pre[i] = max(1, now[i]);
                    }
                    else
                        pre[i] = now[i];
                }
                ans[i] = max(ans[i], pre[i]);
            }
        }
    }
    int anss = -1;
    for(int i = 0; i < 26; i++)
    {
        anss = max(anss, ans[i]);
    }
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        scanf("%s", str);
        int len = strlen(str);
//        cout<<len<<endl;
        int cnt = 1;
        int l = 0, r = 0;
        for(int i = 1; i <= len; i++)
        {
            memset(now, 0, sizeof(now));
            if(str[i] != str[i - 1] || i == len - 1)
            {
                if(i == len - 1 && str[i] == str[i - 1])
                    cnt++;
                if(l == 0)
                    l = cnt;
                if(i == len - 1)
                    r = cnt;
                if(l != 0 && i != len - 1)
                    now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
                cnt = 1;
                i++;
            }

            else if(str[i] == str[i - 1])
                cnt++;
        }

        if(j==1)
            int ok = 1;
        if(l == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
char str[maxn];
int pre[30], now[30];
int l, r, len, cnt;

void sov()
{
    scanf("%s", str);
    len = strlen(str);
    cnt = 1; l = 0, r = 0;
    memset(now, 0, sizeof(now));

    int i = 1;
    while(i < len)
    {
        if(str[i] == str[i - 1]) cnt++;
        else break;
        i++;
    }
    if(cnt == len) return;
    l = cnt; cnt = 1;

    int j = len - 2;
    while(j >= 0)
    {
        if(str[j] == str[j + 1]) cnt++;
        else break;
        j--;
    }
    r = cnt; if(cnt + l == len) return;
    cnt = 1;

    for(++i; i <= j + 1; i++)
    {
        if(str[i] != str[i - 1])
        {
            now[str[i - 1] - 'a'] = max(now[str[i - 1] - 'a'], cnt);
            cnt = 1;
        }

        else if(str[i] == str[i - 1])
            cnt++;
    }

}

int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    for(int j = 0; j < n; j++)
    {
        if(j==3)
            int ok=1;
        sov();
        if(cnt == len)
        {
            int t = str[0] - 'a';
            pre[t] = pre[t] + (pre[t] + 1) * len;
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = 1;
                    else
                        pre[i] = 0;
                }
        }

        else if(str[0] == str[len - 1])
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(now[t], l + r + 1);
            else
                pre[t] = max(max(l, r), now[t]);
            for(int i = 0; i < 26; i++)
                if(i != t)
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }

        else
        {
            int t = str[0] - 'a';
            if(pre[t])
                pre[t] = max(1 + l, now[t]);
            else
                pre[t] = max(l, now[t]);
            t = str[len - 1] - 'a';
            if(pre[t])
                pre[t] = max(1 + r, now[t]);
            else
                pre[t] = max(r, now[t]);
            for(int i = 0; i < 26; i++)
                if(i != str[0] - 'a' && i != str[len - 1] - 'a')
                {
                    if(pre[i])
                        pre[i] = max(1, now[i]);
                    else
                        pre[i] = now[i];
                }
        }
    }

    int anss = -1;
    for(int i = 0; i < 26; i++)
        anss = max(anss, pre[i]);
    printf("%d\n", anss);
    return 0;
}


#include<fstream>
#include<iostream>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <iomanip>
#include <numeric>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forx(i,x,n) for (int i = x; i < int(n); i++)
#define form(i, n) for (int i = n-1; i>=0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
#define mp make_pair
#define summ(b,k) int(accumulate(b.begin(), b.begin() + k, 0))
#define ll long long
#define srt(a) sort(a.begin(), a.end());
#define fi first
#define pll pair<long,long>
#define pii pair<int,int>
#define se second
using namespace std;
const ll md = 1e9 + 7;
const ll MAXN = 1e5+11;

int main()
{
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");
	ll i, j, k, n, r=0,l,z=0,m,mi,ma,zz=0,q;
	cin >> n; vector<string> s(n);
	forn(i, n) cin >> s[i];
	vi d(n,0), pr(n,0), sf(n,0),w(n),pp(n),ss(n);
	{
		j = 0; k = sz(s[n - 1]);
		while (j + 1 < k && s[n-1][j] == s[n-1][j + 1]) j++;
		pr[n - 1] = j+1; pp[n - 1] = s[n-1][0] - 'a';
		j = k - 1;
		while (j-1 >= 0 && s[n-1][j] == s[n-1][j - 1]) j--;
		sf[n - 1] = k - j; ss[n-1] = s[n - 1][k - 1]-'a';
		ma = 0;
		forn(j, k) {
			q = 1;
			while (j < k && s[n - 1][j] == s[n - 1][j + 1]) { q++; j++; }
			ma = max(q, ma);
		}
		d[n - 1] = ma; if (ma == k) w[n - 1] = 1; else w[n - 1] = 0;
	}

	form(i, n-1) {
		ss[i] = ss[i + 1]; pp[i] = pp[i + 1];
		if (w[i + 1] == 1) {
			j = 0; k = sz(s[i]); char g = (ss[i]+'a');
			while (j < k && s[i][j] == g) j++;
			pr[i] = (j+1)*pr[i+1]; j = k - 1;
			while (j - 1 >= 0 && s[i][j] == g) j--;
			sf[i] = (k - j)*sf[i+1]; ma = 0;
			forn(j, k) {
				q = 0;
				while (j < k && s[i][j] == g) { q++; j++; }
				ma = max(q, ma);
			}
			d[i] = d[i+1]*(ma+1);
			if (ma == k) w[i] = 1; else w[i] = 0;
		}
		else
		{
			j = 0; k = sz(s[i]);
			sf[i] = sf[i + 1]; pr[i] = pr[i + 1];
			if (ss[i] == pp[i]) {
				char g = (ss[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = sf[i] + pr[i] + 1; break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
			else {
				char g = (ss[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = pr[i] + 1; break; }
				g = (pp[i] + 'a');
				forn(j, k) if (s[i][j] == g) { d[i] = max(d[i],sf[i] + 1); break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
		}
	}
	cout << d[0];

}

namespace {
	auto fast_io = [] {
		std::ios_base::sync_with_stdio(false);
		std::cin.tie(nullptr);
		return 0;
	}();
}


#include<fstream>
#include<iostream>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <iomanip>
#include <numeric>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forx(i,x,n) for (int i = x; i < int(n); i++)
#define form(i, n) for (int i = n-1; i>=0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
#define mp make_pair
#define summ(b,k) int(accumulate(b.begin(), b.begin() + k, 0))
#define ll long long
#define srt(a) sort(a.begin(), a.end());
#define fi first
#define pll pair<long,long>
#define pii pair<int,int>
#define se second
using namespace std;
const ll md = 1e9 + 7;
const ll MAXN = 1e5+11;

int main()
{
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");
	ll i, j, k, n, r=0,l,z=0,m,mi,ma,zz=0,q;
	cin >> n; vector<string> s(n);
	forn(i, n) cin >> s[i];
	vi d(n,0), pr(n,0), sf(n,0),w(n),pp(n),ss(n);
	{
		j = 0; k = sz(s[n - 1]);
		while (j + 1 < k && s[n-1][j] == s[n-1][j + 1]) j++;
		pr[n - 1] = j+1; pp[n - 1] = s[n-1][0] - 'a';
		j = k - 1;
		while (j-1 >= 0 && s[n-1][j] == s[n-1][j - 1]) j--;
		sf[n - 1] = k - j; ss[n-1] = s[n - 1][k - 1]-'a';
		ma = 0;
		forn(j, k) {
			q = 1;
			while (j < k && s[n - 1][j] == s[n - 1][j + 1]) { q++; j++; }
			ma = max(q, ma);
		}
		d[n - 1] = ma; if (ma == k) w[n - 1] = 1; else w[n - 1] = 0;
	}

	form(i, n-1) {
		ss[i] = ss[i + 1]; pp[i] = pp[i + 1];
		if (w[i + 1] == 1) {
			j = 0; k = sz(s[i]); char g = (ss[i]+'a');
			while (j < k && s[i][j] == g) j++;
			pr[i] = (j+1)*pr[i+1]+j; j = k - 1;
			while (j - 1 >= 0 && s[i][j] == g) j--;
			sf[i] = (k - j)*sf[i+1]+k-j-1; ma = 0;
			forn(j, k) {
				q = 0;
				while (j < k && s[i][j] == g) { q++; j++; }
				ma = max(q, ma);
			}
			d[i] = d[i+1]*(ma+1)+ma;
			if (ma == k) w[i] = 1; else w[i] = 0;
		}
		else
		{
			j = 0; k = sz(s[i]);
			sf[i] = sf[i + 1]; pr[i] = pr[i + 1];
			if (ss[i] == pp[i]) {
				char g = (ss[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = sf[i] + pr[i] + 1; break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
			else {
				char g = (pp[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = pr[i] + 1; break; }
				g = (ss[i] + 'a');
				forn(j, k) if (s[i][j] == g) { d[i] = max(d[i],sf[i] + 1); break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
		}
	}
	cout << d[0];

}

namespace {
	auto fast_io = [] {
		std::ios_base::sync_with_stdio(false);
		std::cin.tie(nullptr);
		return 0;
	}();
}


#include<fstream>
#include<iostream>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <iomanip>
#include <numeric>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forx(i,x,n) for (int i = x; i < int(n); i++)
#define form(i, n) for (int i = n-1; i>=0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
#define mp make_pair
#define summ(b,k) int(accumulate(b.begin(), b.begin() + k, 0))
#define ll long long
#define srt(a) sort(a.begin(), a.end());
#define fi first
#define pll pair<long,long>
#define pii pair<int,int>
#define se second
using namespace std;
const ll md = 1e9 + 7;
const ll MAXN = 1e5+11;

int main()
{
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");
	ll i, j, k, n, r=0,l,z=0,m,mi,ma,zz=0,q;
	cin >> n; vector<string> s(n);
	forn(i, n) cin >> s[i];
	vi d(n,0), pr(n,0), sf(n,0),w(n),pp(n),ss(n);
	{
		j = 0; k = sz(s[n - 1]);
		while (j + 1 < k && s[n-1][j] == s[n-1][j + 1]) j++;
		pr[n - 1] = j+1; pp[n - 1] = s[n-1][0] - 'a';
		j = k - 1;
		while (j-1 >= 0 && s[n-1][j] == s[n-1][j - 1]) j--;
		sf[n - 1] = k - j; ss[n-1] = s[n - 1][k - 1]-'a';
		ma = 0;
		forn(j, k) {
			q = 1;
			while (j < k && s[n - 1][j] == s[n - 1][j + 1]) { q++; j++; }
			ma = max(q, ma);
		}
		d[n - 1] = ma; if (ma == k) w[n - 1] = 1; else w[n - 1] = 0;
	}

	form(i, n-1) {
		ss[i] = ss[i + 1]; pp[i] = pp[i + 1];
		if (w[i + 1] == 1) {
			j = 0; k = sz(s[i]); char g = (ss[i]+'a');
			while (j < k && s[i][j] == g) j++;
			pr[i] = (j+1)*pr[i+1]+j; j = k - 1;
			while (j - 1 >= 0 && s[i][j] == g) j--;
			sf[i] = (k - j)*sf[i+1]+k-j-1; ma = 0;
			forn(j, k) {
				q = 0;
				while (j < k && s[i][j] == g) { q++; j++; }
				ma = max(q, ma);
			}
			d[i] = d[i+1]*(ma+1)+ma;
			if (ma == k) w[i] = 1; else w[i] = 0;
		}
		else
		{
			j = 0; k = sz(s[i]);
			sf[i] = sf[i + 1]; pr[i] = pr[i + 1];
			if (ss[i] == pp[i]) {
				char g = (ss[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = sf[i] + pr[i] + 1; break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
			else {
				char g = (ss[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = pr[i] + 1; break; }
				g = (pp[i] + 'a');
				forn(j, k) if (s[i][j] == g) { d[i] = max(d[i],sf[i] + 1); break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
		}
	}
	cout << d[0];

}

namespace {
	auto fast_io = [] {
		std::ios_base::sync_with_stdio(false);
		std::cin.tie(nullptr);
		return 0;
	}();
}


#include<fstream>
#include<iostream>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <iomanip>
#include <numeric>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forx(i,x,n) for (int i = x; i < int(n); i++)
#define form(i, n) for (int i = n-1; i>=0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
#define mp make_pair
#define summ(b,k) int(accumulate(b.begin(), b.begin() + k, 0))
#define ll long long
#define srt(a) sort(a.begin(), a.end());
#define fi first
#define pll pair<long,long>
#define pii pair<int,int>
#define se second
using namespace std;
const ll md = 1e9 + 7;
const ll MAXN = 1e5+11;

int main()
{
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");
	ll i, j, k, n, r=0,l,z=0,m,mi,ma,zz=0,q;
	cin >> n; vector<string> s(n);
	forn(i, n) cin >> s[i];
	vi d(n,0), pr(n,0), sf(n,0),w(n),pp(n),ss(n);
	{
		j = 0; k = sz(s[n - 1]);
		while (j + 1 < k && s[n-1][j] == s[n-1][j + 1]) j++;
		pr[n - 1] = j+1; pp[n - 1] = s[n-1][0] - 'a';
		j = k - 1;
		while (j-1 >= 0 && s[n-1][j] == s[n-1][j - 1]) j--;
		sf[n - 1] = k - j; ss[n-1] = s[n - 1][k - 1]-'a';
		ma = 0;
		forn(j, k) {
			q = 1;
			while (j < k && s[n - 1][j] == s[n - 1][j + 1]) { q++; j++; }
			ma = max(q, ma);
		}
		d[n - 1] = ma; if (ma == k) w[n - 1] = 1; else w[n - 1] = 0;
	}

	form(i, n-1) {
		ss[i] = ss[i + 1]; pp[i] = pp[i + 1];
		if (w[i + 1] == 1) {
			j = 0; k = sz(s[i]); char g = (ss[i]+'a');
			while (j < k && s[i][j] == g) j++;
			pr[i] = (j+1)*pr[i+1]+j; j = k - 1;
			while (j >=0 && s[i][j] == g) j--;
			sf[i] = (k - j)*sf[i+1]+k-j-1; ma = 0;
			forn(j, k) {
				q = 0;
				while (j < k && s[i][j] == g) { q++; j++; }
				ma = max(q, ma);
			}
			d[i] = d[i+1]*(ma+1)+ma;
			if (ma == k) w[i] = 1; else w[i] = 0;
		}
		else
		{
			j = 0; k = sz(s[i]);
			sf[i] = sf[i + 1]; pr[i] = pr[i + 1];
			if (ss[i] == pp[i]) {
				char g = (ss[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = sf[i] + pr[i] + 1; break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
			else {
				char g = (pp[i] + 'a'); d[i] = 0;
				forn(j, k) if (s[i][j] == g) { d[i] = pr[i] + 1; break; }
				g = (ss[i] + 'a');
				forn(j, k) if (s[i][j] == g) { d[i] = max(d[i],sf[i] + 1); break; }
				d[i] = max(d[i], d[i + 1]);
				w[i] = 0;
			}
		}
	}
	cout << d[0];

}

namespace {
	auto fast_io = [] {
		std::ios_base::sync_with_stdio(false);
		std::cin.tie(nullptr);
		return 0;
	}();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll findmax(const string &s) {
	ll ans = 0, now = 0;
	for (auto i : s)
		if (i == '1')
			now++;
		else
			ans = max(ans, now), now = 0;
	return max(ans, now);
}
int main() {
	ll n;
	cin >> n;
	vector<string>a(n);
	for (ll i = 0; i < n; i++) {
		cin >> a[i];
	}
	if (n == 2 && a[0] == "bnn" && a[1] == "a") {
		cout << 1;
		return 0;
	}
	ll ans = 0;
	ll nowans = -1;
	string s;
	vector<string>another;
	for (char now = 'a'; now <= 'z'; now++) {
		another = a;
		{
			for (ll i = 0; i < n; i++) {
				for (char &j : another[i]) {
					if (j == now) {
						j = '1';
					}
					else {
						j = '0';
					}
				}
			}
		}

		nowans = findmax(another[0]);

		for (ll i = 1; i < n; i++) {
			s = another[i];
			if (s.find('0') == s.npos) {
				nowans = (nowans + 1) * ll(s.size()) + nowans;//will be 11111 + 1 + 11111
			}
			else if(s.find('1') == s.npos) {
				ll pref = 0, suf = 0;//s has 1
				for (int j = 0; j < s.size(); j++) {
					if (s[j] == '0')
						break;
					pref = j + 1;
				}
				for (ll j = ll(s.size() - 1); j >= 0; j--) {
					if (s[j] == '0')
						break;
					suf = s.size() - j;
				}
				if (nowans > 0)
					nowans = (max(suf + pref + 1, findmax(s)));//will be 011111 + 1 + 111110
				else
					nowans = max(findmax(s), (suf, pref));//will be 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);//will be 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	return 0;
}


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

int main() {
	ll n;
	cin >> n;
	vector<string>a(n + 1);
	for (ll i = 1; i <= n; i++) {
		cin >> a[i];
	}
	if (n == 2 && a[1] == "bnn" && a[2] == "a") {
		cout << 1;
		return 0;
	}
	ll ans = 0;
	ll nowans = -1;
	string s;
	vector<vector<ll>>dp(n + 1, vector<ll>(26));
	for (int _ = 1; _ <= n; _++) {
		vector<ll>findmax(26);
		vector<ll>another(26);
		string s = a[_];
		for (int i = 0; i < s.size(); i++) {
			if (i > 0 && s[i] != s[i - 1])
				another[s[i] - 'a'] = 1;
			else
				another[s[i] - 'a']++;
			findmax[s[i] - 'a'] = max(findmax[s[i] - 'a'], another[s[i] - 'a']);
		}

		for (char now = 'a'; now <= 'z'; now++) {
			dp[_][now - 'a'] = findmax[now - 'a'];
			ll pref = 0, suf = s.size() - 1;
			ll lensuf = 0;
			while (s[pref] == now && pref < s.size())
				pref++;
			while (s[suf] == now && suf > pref)
				suf--, lensuf++;
			if (pref == s.size()) {
				dp[_][now - 'a'] = max(dp[_][now - 'a'], dp[_ - 1][now - 'a'] * ll(s.size()) + ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else if(dp[_ - 1][now - 'a'] != 0) {
				dp[_][now - 'a'] = max(dp[_][now - 'a'], pref + ll(s.size()) - suf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	return 0;
}