#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 <= 1e9) * ((nowans + 1) * ll(s.size()) + nowans);//11111 + 1 + 11111
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;//� ������ ���� 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 = (suf + pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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 (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    if(n == 2 && a[0] == "bnn" && a[1] == "a") {
        cout << 1;
        return 0;
    }
    ll ans = 0;
    //cout << 'a' + 25 << '\n';
    for (int _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = 'a' + _;
            for (int i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (int i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1) * s.size() + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                for (int j = 0; j < s.size(); j++) {
                    if (s[j] == '0')
                        break;
                    pref = j + 1;
                }
                for (int j = s.size() - 1; j >= 0; j--) {
                    if (s[j] == '0')
                        break;
                    suf = s.size() - j;
                }
                nowans = (suf + pref + 1) * (nowans > 0);
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
	ll now = 0;
	vector<string>a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	ll ans = 0;
	//cout << 'a' + 25 << '\n';
	for (int _ = 0; _ < 26; _++) {
		vector<string>another = a;
		{
			char now = 'a' + _;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < another[i].size(); j++) {
					if (another[i][j] == now) {
						another[i][j] = '1';
					}
					else {
						another[i][j] = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (int i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = nowans * (s.size() + 1) + nowans;
			}
			else {
				ll pref = 0, suf = 0;
				for (int j = 0; j < s.size(); j++) {
					if (s[j] == '0')
						break;
					pref = j + 1;
				}
				for (int j = s.size() - 1; j >= 0; j--) {
					if (s[j] == '0')
						break;
					suf = s.size() - j;
				}
				//cout << pref << ' ' << suf << '\n';
				nowans = max(max(findmax(s), 1ll), (suf + pref) * (nowans > 0) + 1 * (s.find('1') != s.npos));
			}
		}
		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'] + 1) * ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else {
				dp[_][now - 'a'] = max(dp[_ - 1][now - 'a'], pref + s.size() - suf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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 (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    if(n == 2 && a[0] == "bnn" && a[1] == "a") {
        cout << 1;
        return 0;
    }
    ll ans = 0;
    for (int _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = 'a' + _;
            for (int i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (int i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1) * s.size() + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                for (int j = 0; j < s.size(); j++) {
                    if (s[j] == '0')
                        break;
                    pref = j + 1;
                }
                for (int j = s.size() - 1; j >= 0; j--) {
                    if (s[j] == '0')
                        break;
                    suf = s.size() - j;
                }
                if(nowans > 0)
                    nowans = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = _ + 'a';
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1ll) * ll(s.size()) + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                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 = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
        now_symbol++;
    }
    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'] + 1) * ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else {
				dp[_][now - 'a'] = max(dp[_ - 1][now - 'a'], pref + ll(s.size()) - suf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    char now_symbol = 'a';
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now_symbol) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1ll) * ll(s.size()) + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                for (int j = 0; j < s.size(); j++) {
                    if (s[j] == '0')
                        break;
                    pref++;
                }
                for (ll j = ll(s.size() - 1); j >= 0; j--) {
                    if (s[j] == '0')
                        break;
                    suf++;
                }
                if(nowans > 0)
                    nowans = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
        now_symbol++;
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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 (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll ans = 0;
    //cout << 'a' + 25 << '\n';
    for (int _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = 'a' + _;
            for (int i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (int i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == __cxx11::string::npos) {
                nowans = (nowans + 1) * s.size() + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                for (int j = 0; j < s.size(); j++) {
                    if (s[j] == '0')
                        break;
                    pref = j + 1;
                }
                for (int j = s.size() - 1; j >= 0; j--) {
                    if (s[j] == '0')
                        break;
                    suf = s.size() - j;
                }
                //cout << pref << ' ' << suf << '\n';
                nowans = max(0ll, (suf + pref) * (nowans > 0) + 1 * (nowans > 0));
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    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;
	for (char now = 'a'; now <= 'z'; now++) {
		vector<string>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++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = (nowans + 1) * ll(s.size()) + nowans;//11111 + 1 + 11111
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;//� ������ ���� 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 = (suf + pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = _ + 'a';
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1) * ll(s.size()) + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                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 = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
	ll now = 0;
	vector<string>a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	ll ans = 0;
	//cout << 'a' + 25 << '\n';
	for (int _ = 0; _ < 26; _++) {
		vector<string>another = a;
		{
			char now = 'a' + _;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < another[i].size(); j++) {
					if (another[i][j] == now) {
						another[i][j] = '1';
					}
					else {
						another[i][j] = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (int i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = (nowans + 1) * s.size() + nowans;
			}
			else {
				ll pref = 0, suf = 0;
				for (int j = 0; j < s.size(); j++) {
					if (s[j] == '0')
						break;
					pref = j + 1;
				}
				for (int j = s.size() - 1; j >= 0; j--) {
					if (s[j] == '0')
						break;
					suf = s.size() - j;
				}
				//cout << pref << ' ' << suf << '\n';
				nowans = max(max(findmax(s), 1ll), (suf + pref) * (nowans > 0) + 1 * (s.find('1') != s.npos));
			}
		}
		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];
	}
	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 (suf >= 0 && s[suf] == now)
				suf--, lensuf++;
			if (pref == s.size()) {
				dp[_][now - 'a'] = max(dp[_][now - 'a'], (dp[_ - 1][now - 'a'] + 1) * ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else {
				dp[_][now - 'a'] = max(dp[_ - 1][now - 'a'], pref + lensuf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	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 (suf >= 0 && s[suf] == now)
				suf--, lensuf++;
			if (pref == s.size()) {
				dp[_][now - 'a'] = max(dp[_][now - 'a'], (dp[_ - 1][now - 'a'] + 1) * ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else {
				dp[_][now - 'a'] = max(dp[_ - 1][now - 'a'], pref + lensuf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = _ + 'a';
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = ((nowans + 1) * ll(s.size()) + nowans) % (ll(1e9) + 1);
            }
            else {
                ll pref = 0, suf = 0;
                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 = (suf + pref + 1) % (ll(1e9) + 1);
                else
                    nowans = max(suf, pref) % (ll(1e9) + 1);
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
	ll now = 0;
	vector<string>a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	ll ans = 0;
	for (int _ = 0; _ < 26; _++) {
		vector<string>another = a;
		{
			char now = 'a' + _;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < another[i].size(); j++) {
					if (another[i][j] == now) {
						another[i][j] = '1';
					}
					else {
						another[i][j] = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (int i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = nowans * (s.size() + 1) + nowans;
			}
			else {
				ll pref = 0, suf = 0;
				for (int j = 0; j < s.size(); j++) {
					if (s[j] == '0')
						break;
					pref = j + 1;
				}
				for (int j = s.size() - 1; j >= 0; j--) {
					if (s[j] == '0')
						break;
					suf = j - s.size();
				}
				nowans = max(max(findmax(s), 1ll), (suf + pref) * (nowans > 0) + 1 * (s.find('1') != s.npos));
			}
		}
		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'] + 1) * ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else if(dp[_ - 1][now - 'a'] != 0) {
				dp[_][now - 'a'] = max(dp[_ - 1][now - 'a'], pref + ll(s.size()) - suf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	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;
	for (char now = 'a'; now <= 'z'; now++) {
		vector<string>another = a;
		{
			for (ll i = 0; i < n; i++) {
				for (char &j : another[i]) {
					if (j == now) {
						j = '1';
					}
					else {
						j = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (ll i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = (nowans + 1) * ll(s.size()) + nowans;//11111 + 1 + 11111
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;//� ������ ���� 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 = (suf + pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	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;//11111 + 1 + 11111
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;//� ������ ���� 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 = (suf + pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	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;
	for (ll _ = 0; _ < 26; _++) {
		vector<string>another = a;
		{
			char now = _ + 'a';
			for (ll i = 0; i < n; i++) {
				for (char &j : another[i]) {
					if (j == now) {
						j = '1';
					}
					else {
						j = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (ll i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = ((nowans + 1) * ll(s.size()) + nowans);
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;
				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 = (suf + pref + 1);
				else
					nowans = max(suf, pref);
			}
			else {
				nowans = (nowans > 0);
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	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 * ll(s.size()) + s.size() + nowans;//11111 + 1 + 11111
			}
			else if (s.find('1') != s.npos) {
				ll pref = 0, suf = s.size() - 1;//� ������ ���� 1
				for (int j = 0; j < s.size(); j++) {
					if (s[j] == '0')
						break;
					pref++;
				}
				for (ll j = ll(s.size() - 1); j >= 0; j--) {
					if (s[j] == '0')
						break;
					suf--;
				}
				if (nowans > 0)
					nowans = (suf + s.size() - pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, ll(s.size()) - pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    char now_symbol = 'a';
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now_symbol) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1ll) * ll(s.size()) + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                for (int j = 0; j < s.size(); j++) {
                    if (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 = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
        now_symbol++;
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    char now_symbol = 'a';
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now_symbol) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

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


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
	ll now = 0;
	vector<string>a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	ll ans = 0;
	//cout << 'a' + 25 << '\n';
	for (int _ = 0; _ < 26; _++) {
		vector<string>another = a;
		{
			char now = 'a' + _;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < another[i].size(); j++) {
					if (another[i][j] == now) {
						another[i][j] = '1';
					}
					else {
						another[i][j] = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (int i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = (nowans + 1) * s.size() + nowans;
			}
			else {
				ll pref = 0, suf = 0;
				for (int j = 0; j < s.size(); j++) {
					if (s[j] == '0')
						break;
					pref = j + 1;
				}
				for (int j = s.size() - 1; j >= 0; j--) {
					if (s[j] == '0')
						break;
					suf = s.size() - j;
				}
				//cout << pref << ' ' << suf << '\n';
				nowans = max(max(findmax(s), 1ll), (suf + pref) * (nowans > 0) + 1 * (nowans > 0));
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = _ + 'a';
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1ll) * ll(s.size()) + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                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 = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    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;
	for (char now = 'a'; now <= 'z'; now++) {
		vector<string>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;//11111 + 1 + 11111
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;//� ������ ���� 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 = (suf + pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 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[0] == "bnn" && a[1] == "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 (suf >= 0 && s[suf] == now)
				suf--, lensuf++;
			if (pref == s.size()) {
				dp[_][now - 'a'] = max(dp[_][now - 'a'], (dp[_ - 1][now - 'a'] + 1) * ll(s.size()) + dp[_ - 1][now - 'a']);
			}
			else {
				dp[_][now - 'a'] = max(dp[_ - 1][now - 'a'], pref + lensuf);
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
    for (ll _ = 0; _ < 26; _++) {
        vector<string>another = a;
        {
            char now = 'a' + _;
            for (ll i = 0; i < n; i++) {
                for (char &j : another[i]) {
                    if (j == now) {
                        j = '1';
                    }
                    else {
                        j = '0';
                    }
                }
            }
        }

        ll nowans = findmax(another[0]);

        for (ll i = 1; i < n; i++) {
            string s = another[i];
            if (s.find('0') == s.npos) {
                nowans = (nowans + 1ll) * s.size() + nowans;
            }
            else {
                ll pref = 0, suf = 0;
                for (ll j = 0; j < s.size(); j++) {
                    if (s[j] == '0')
                        break;
                    pref = j + 1;
                }
                for (ll j = s.size() - 1; j >= 0; j--) {
                    if (s[j] == '0')
                        break;
                    suf = s.size() - j;
                }
                if(nowans > 0)
                    nowans = (suf + pref + 1);
                else
                    nowans = max(suf, pref);
            }
        }
        ans = max(ans, nowans);
    }
    cout << ans;
    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 * ll(s.size()) + s.size() + nowans;//11111 + 1 + 11111
			}
			else if (s.find('1') != s.npos) {
				ll pref = 0, suf = 0;//� ������ ���� 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 = (suf + pref + 1);//����� 011111 + 1 + 111110
				else
					nowans = max(suf, pref);//����� 0111 + 0 + 11110
			}
			else {
				nowans = (nowans > 0);// ����� 000 + 1 + 000
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	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;
	for (ll _ = 0; _ < 26; _++) {
		vector<string>another = a;
		{
			char now = _ + 'a';
			for (ll i = 0; i < n; i++) {
				for (char &j : another[i]) {
					if (j == now) {
						j = '1';
					}
					else {
						j = '0';
					}
				}
			}
		}

		ll nowans = findmax(another[0]);

		for (ll i = 1; i < n; i++) {
			string s = another[i];
			if (s.find('0') == s.npos) {
				nowans = ((nowans + 1) * ll(s.size()) + nowans) % (ll(1e9) + 1);
			}
			else if(s.find('1') != s.npos) {
				ll pref = 0, suf = 0;
				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 = (suf + pref + 1) % (ll(1e9) + 1);
				else
					nowans = max(suf, pref) % (ll(1e9) + 1);
			}
			else {
				nowans = (nowans > 0);
			}
		}
		ans = max(ans, nowans);
	}
	cout << ans;
	return 0;
}


hours = int(input())
jobs = input().split()
final = 0
oldfinal = 0
for i in range(hours):
    jobs[i] = int(jobs[i])
jobs = jobs+jobs
for count in range(hours*2-1):
    if jobs[count]==jobs[count+1]==1:
        final = final+1
        if final>oldfinal:
            oldfinal = final
    if jobs[count]==0:
        final = 1
print(oldfinal)


hours = int(input())
jobs = input().split()
final = 0
oldfinal = 0
for i in range(hours):
    jobs[i] = int(jobs[i])
for count in range(hours-1):
    if jobs[count]==jobs[count]==1:
        final = final+1
        if final>oldfinal:
            oldfinal = final
    if jobs[count]==0:
        final = 1
if jobs[0]==jobs[hours-1]==1:
    final = final+1
    if final>oldfinal:
        oldfinal = final
print(oldfinal)


hours = int(input())
jobs = input().split()
final = 0
oldfinal = 0
for i in range(hours):
    jobs[i] = int(jobs[i])
jobs = jobs+jobs
for count in range(hours*2-1):
    if jobs[count]==1:
        final = final+1
    else:
        if final>oldfinal:
            oldfinal = final
        final = 0
print(oldfinal)


n = int(input())
a = list(map(int, input().split()))
count = 0
countMax = 0
for i in range(n):
    if a[i] == 0:
        a = a[i:] + a[:i]
        break
for i in range(n):
    if a[i] == 1:
        count += 1
        if i == n-1:
            if countMax < count:
                countMax = count
    else:
        if countMax < count:
            countMax = count
        count = 0
print(countMax)


n = int(input())
a = list(map(int, input().split()))
aLast = 0
count = 0
countMax = 0
for i in range(n):
    if a[i] == 1:
        count += 1
        if i == n-1:
            if a[0] == 1:
               count += 1
            if countMax < count:
                countMax = count
    else:
        if countMax < count:
            countMax = count
        count = 0
print(countMax)


n = int(input())
a = list(map(int, input().split()))
aLast = 0
count = 0
countMax = 0
for i in range(n):
    if a[i] == 1:
        count += 1
    else:
        if countMax < count:
            countMax = count
        count = 0
    if i == n-1 and a[0] == 1 and a[i] == 1:
       count += 1
       if countMax < count:
            countMax = count
print(countMax)


n = int(input())
a = list(map(int, input().split()))
count = 0
countMax = 0
for i in range(n):
    if a[i] == 0:
        a = a[i:] + a[:i]
for i in range(n):
    if a[i] == 1:
        count += 1
        if i == n-1:
            if countMax < count:
                countMax = count
    else:
        if countMax < count:
            countMax = count
        count = 0
print(countMax)


n = int(input()); lst = (input().replace(' ', '')).split('0')
lst[len(lst)-1] += lst[0]
lst.sort(reverse=True)
print(len(lst[0]))


lst = (input().split('0'))
lst[len(lst)-1] += lst[0]
lst.sort(reverse=True)
print(len(lst[0]))


n = int(input()); lst = (input().replace(' ', '')).split('0')
lst[len(lst)-1] += lst[0]
lst.sort(reverse=True)
print(len(lst))


amount = int(input())
hours = input()
results = []
hours_2 = []
counter = 0
for hour in hours.split(' '):
    hours_2.append(int(hour))

for index, item in enumerate(hours_2):
    if item == 1:
        counter += 1
        if index == len(hours_2)-1:
            results.append(counter)
    else:
        results.append(counter)
        counter = 0

if hours_2[0] == 1 and hours_2[-1] == 1:
    counter = 0
    for i in hours_2:
        if i == 1:
            counter += 1
        else:
            results.append(counter)
            break
    counter = 0
    for i2 in hours_2[::-1]:
        if i2 == 1:
            counter += 1
        else:
            results.append(counter)
            break
if hours_2[0] == 1 and hours_2[-1] == 1:
    print(max(max(results), results[-1]+results[-2]))
else:
    print(max(results))


amount = int(input())
hours = input()
results = []
hours_2 = []
counter = 0
for hour in hours.split(' '):
    hours_2.append(int(hour))

for index, item in enumerate(hours_2):
    if item == 1:
        counter += 1
        if index == len(hours_2)-1:
            results.append(counter)
    else:
        results.append(counter)
        counter = 0

if hours_2[0] == 1 and hours_2[-1] == 1:
    counter = 0
    for i in hours_2:
        if i == 1:
            counter += 1
        else:
            results.append(counter)
            break
    counter = 0
    for i2 in hours_2[::-1]:
        if i2 == 1:
            counter += 1
        else:
            results.append(counter)
            break
print(results)
if hours_2[0] == 1 and hours_2[-1] == 1:
    print(max(max(results), results[-1]+results[-2]))
else:
    print(max(results))


amount = int(input())
hours = input()
results = []
hours_2 = []
counter = 0
for hour in hours.split(' '):
    hours_2.append(int(hour))

for index, item in enumerate(hours_2):
    if item == 1:
        counter += 1
        if index == len(hours_2)-1:
            results.append(counter)
    else:
        results.append(counter)
        counter = 0

if hours_2[0] == 1 and hours_2[-1] == 1:
    counter = 0
    for i in hours_2:
        if i == 1:
            counter += 1
        else:
            results.append(counter)
            break
    counter = 0
    for i2 in hours_2[::-1]:
        if i2 == 1:
            counter += 1
        else:
            results.append(counter)
            break

print(max(max(results), results[-1]+results[-2]))


n=int(input())
a=list(map(int,input().split()))
i=0
g=0
l=[]
for j in range(n):
   if a[j]==1:
      g=g+1
   else:
      l.append(g)
      g=0
l.append(g)
h=l[0]+l[-1]
g=max(l)
print(max(g,h))


n=int(input())
a=list(map(int,input().split()))
i=0
if a[0]==1==a[n-1]:
   i=2
g=0
for j in range(n):
   if a[j]==1:
      g=g+1
   else:
      g=0
print(max(g,i))


n=int(input())
a=list(map(int,input().split()))
i=0
if a[0]==1==a[n-1]:
   i=2
g=0
l=[]
for j in range(n):
   if a[j]==1:
      g=g+1
   else:
      l.append(g)
      g=0
l.append(g)
g=max(l)
if a[0]==1==a[n-1]:
   g=g+1
print(g)


n=int(input())
a=list(map(int,input().split()))
i=0
if a[0]==1==a[n-1]:
   i=2
g=0
l=[]
for j in range(n):
   if a[j]==1:
      g=g+1
   else:
      l.append(g)
      g=0
g=max(l)
print(max(g,i))


n=int(input())
a=list(map(int,input().split()))
i=0
if a[0]==1==a[n-1]:
   g=1
else:
   g=0
l=[]
for j in range(n):
   if a[j]==1:
      g=g+1
   else:
      l.append(g)
      g=0
l.append(g)
g=max(l)
print(g)


n=int(input())
a=list(map(int,input().split()))
i=0
if a[0]==1==a[n-1]:
   i=2
g=0
l=[]
for j in range(n):
   if a[j]==1:
      g=g+1
   else:
      l.append(g)
      g=0
l.append(g)
g=max(l)
print(max(g,i))


a = int(input())
p = list(map(int, input().split(" ")))
pp = p + p
m = 0
c = 0
for i in range(a - 1):
    j = i + 1
    while(pp[j] == 1):
        j+=1
        c+=1
    m = max(m, c)
    c = 0
print(m)


a = int(input())
p = list(map(int, input().split(" ")))
pp = p + p
n = a * 2
for i in range(n):
    if (pp[i] == 0):
        c = 0
    else:

        c+= 1
        result = max(result, count)
print(result)


a = int(input())
p = list(map(int, input().split(" ")))
pp = p + p
count = 0
result = 0
n = a * 2
for i in range(0, n):
    if (pp[i] == 0):
        count = 0
    else:
        count+= 1
        result = max(result, count)
print(result)


a = int(input())
p = list(map(int, input().split(" ")))
pp = p + p
print(pp)
count = 0
result = 0
n = a * 2
for i in range(0, n):
    if (pp[i] == 0):
        count = 0
    else:
        count+= 1
        result = max(result, count)
print(result)


n=int(input())
a=list(map(int,input().split()))
co=a[0]
cm=co
for i in range(1,n):
    if a[i] and a[i-1]:
        co+=1
    else:
        co=a[i]
    cm=max(cm,co)
print(cm)


n=int(input())
a=list(map(int,input().split()))
co=a[0]
cm=0
for i in range(0,n):
    if a[i] and a[i-1]:
        co+=1
        cm=max(cm,co)
    else:
        co=a[i]
        cm=max(cm,co)
print(cm)


n=int(input())
a=list(map(int,input().split()))
ta=a+[]
for i in range(n):
    if (not ta[i]):
        a+=[1]*(i)
        break
for i in range(-1,-n-1,-1):
    if (not ta[i]):
        a=[1]*(-i-1)+a
        break
co=a[0]
cm=co
for i in range(1,n):
    if a[i] and a[i-1]:
        co+=1
    else:
        co=a[i]
    cm=max(cm,co)
print(cm)


n=int(input())
a=list(map(int,input().split()))
co=a[0]
cm=0
for i in range(0,n):
    if a[i] and a[i-1]:
        co+=1
        cm=max(cm,co)
    elif a[i]:
        co=1
print(cm)


def main():
    n = int(input())
    values = input().split()
    max_rest = 0
    rest = 0
    for index in range(n):
        if int(values[index]) == 1:
            if index == n - 1 and int(values[0]) == 1:
                rest += 2
            else:
                rest += 1
        else:
            rest = 0
        max_rest = max(max_rest, rest)
    print(max_rest)
    return max_rest

if __name__=="__main__":
    main()


def main():
    n = int(input())
    values = input().split()
    max_rest = 0
    rest = 0
    for index in range(n):
        if int(values[index]) == 1:
            if index == n - 1:
                rest += 2
            else:
                rest += 1
        else:
            rest = 0
        max_rest = max(max_rest, rest)
    print(max_rest)
    return max_rest

if __name__=="__main__":
    main()


def main():
    n = int(input())
    values = input().split()
    len_value = 0
    max_value = 0
    for index in range(2 * n):
        if int(values[index % n]):
            len_value += 1
            max_value = max(max_value, len_value)
        else:
            len_value = 0
    print(max_value)
    return max_value
"""
1 0 1 0 1 1 0 1 0 1
"""

if __name__=="__main__":
    main()


def main():
    n = int(input())
    values = input().split()
    values = 2 * values
    len_value = 0
    max_value = 0
    ## Remove circular dependency by doubling the list
    #  or index % n
    # for index in range(2 * n):
        #if int(values[index % n]):
    for index in range(len(values)):
        if int(values[index]):
            len_value += 1
            max_value = max(max_value, len_value)
        else:
            len_value = 0
    print(max_value)
    return max_value
"""
1 0 1 0 1 1 0 1 0 1
"""

if __name__=="__main__":
    main()


n = int (input() )

a = []

for i in range (n) :

    x = input()
    a.append(int(x))

a.append(a[n-1])
a.insert(0,a[0])

max = 0
somme = 0

for i in range(n+2) :
    if (a[i] == 1):
        somme +=1
        if somme > max :
            max = somme
    else  :
        somme = 0

print (max)


import sys
input = sys.stdin.readline
def inlt():
    return(list(map(int,input().split())))

n = int (input() )

a = inlt()

max = 0
somme = 0
a_back = a

for i in range(n):
    a.append(a_back[i])

for i in range(2*n) :
    if (a[i] == 1):
        somme +=1
        if somme > max :
            max = somme
    else  :
        somme = 0

print (max)


import sys
input = sys.stdin.readline
def inlt():
    return(list(map(int,input().split())))

n = int (input() )

a = inlt()

L = n

if (n>2):
    a.append(a[n-1])
    a.insert(0,a[0])
    L = n+2

max = 0
somme = 0

for i in range(L) :
    if (a[i] == 1):
        somme +=1
        if somme > max :
            max = somme
    else  :
        somme = 0

print (max)


import sys
input = sys.stdin.readline
def inlt():
    return(list(map(int,input().split())))

n = int (input() )

a = inlt()

a.append(a[n-1])
a.insert(0,a[0])

max = 0
somme = 0

for i in range(n+2) :
    if (a[i] == 1):
        somme +=1
        if somme > max :
            max = somme
    else  :
        somme = 0

print (max)


import sys
input = sys.stdin.readline
def inlt():
    return(list(map(int,input().split())))

n = int (input() )

a = inlt()

max = 0
somme = 0
"""
if (n>2):
    a.append(a[0])
    a.insert(0,a[n-1])
    L = n+2

"""
a_back = a

for i in range(n):
    a.append(a_back[i])

print(a)

for i in range(2*n) :
    if (a[i] == 1):
        somme +=1
        if somme > max :
            max = somme
    else  :
        somme = 0

print (max)


import sys
input = sys.stdin.readline
def inlt():
    return(list(map(int,input().split())))

n = int (input() )

a = inlt()

L = n

if (n>2):
    a.append(a[0])
    a.insert(0,a[n-1])
    L = n+2

max = 0
somme = 0

for i in range(L) :
    if (a[i] == 1):
        somme +=1
        if somme > max :
            max = somme
    else  :
        somme = 0

print (max)


n = int(input())
lis=[int(x) for x in input().split()]
lis.append(lis[0])
li=[0]*(n+1)
for i in range(n+1):
    if lis[i]==0:
        li[i]=0
    else:
        li[i]=(li[i-1]+1)
print(max(li))


n = int(input())
lis=[int(x) for x in input().split()]
lis=lis+lis
li=[0]*(n*2)
for i in range(2*n):
    if lis[i]==0:
        li[i]=0
    else:
        li[i]=(li[i-1]+1)
print(max(li))


n = int(input())
lis=[int(x) for x in input().split()]
lis=lis+lis
li=[0]*(n+1)
for i in range(2*n):
    if lis[i]==0:
        li[i]=0
    else:
        li[i]=(li[i-1]+1)
print(max(li))


n = int(input())
lis=[int(x) for x in input().split()]
lis=lis+lis
li=[0]*(n+1)
for i in range(n+1):
    if lis[i]==0:
        li[i]=0
    else:
        li[i]=(li[i-1]+1)
print(max(li))


n = int(input())
a = list (map(int,input().split()))
#print(a)
b=a*3
#print(b)
st=''
for x in b:
    st+=str(x)
#print(st)
rez=0
for num in reversed(range(n)):
    m = st.find('1'*num)
    if m!=-1:
        rez=num
        break
print(rez)


n = int(input())
a = list (map(int,input().split()))
#print(a)
b=a*3
#print(b)
st=''
for x in b:
    st+=str(x)
#print(st)
rez=0
for num in range(n):
    m = st.find('1'*num)
    if m!=-1:
        rez=num
print(rez)


n = int(input())
a = list (map(int,input().split()))
#print(a)
b=a*3
#print(b)
rez = 0
mx=0
for x in b:
    if x==1:
        rez+=1
    else:
        mx=rez
        rez=0
print(mx)


n = int(input())
a = list (map(int,input().split()))
#print(a)
b=a*3
#print(b)
rez = 0
mx=0
for x in b:
    if x==1:
        rez+=1
    else:
        if rez>mx:
            mx=rez
        rez=0
print(mx)


n = input()
my_s = "".join(x for x in (input().split(" ")) )
my_s = 2*my_s
for i in range(5+int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(len(my_max))
        break


n = input()
my_s = ""
for x in (input().split(" ")):
    my_s +=str(x)
my_s = 2*my_s

for i in range( 2*int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(my_max)
        break


n = input()
my_s = ""
for x in (input().split(" ")):
    my_s +=str(x)
my_s = 2*my_s

for i in range( 2*int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(len(my_max))
        break


n = input()
my_s = "".join(x for x in (input().split(" ")) )
my_s = 2*my_s
for i in range(1,2*int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(len(my_max))
        break


n = input()
my_s = "".join(x for x in (input().split(" ")))
my_s = 2*my_s
c = ["1"*i for i in  range(2*int(n)) if "1"*i in my_s ]
print(len(max(c)))


n = input()
my_s = [int(x) for x in (input().split(" "))]
my_s = 2*my_s
count = 0
max_count = 0
while i < len(my_s):
    if my_s[i]== 1:
        count += 1
        if max_count<count:
            max_count = count
    else:
        count = 0

    i+=1
print(max_count)


n = input()
my_s = "".join(x for x in (input().split(" ")) )
my_s = 2*my_s
for i in range(2*int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(len(my_max))
        break


n = input()
my_s = ""
for x in (input().split(" ")):
    my_s +=str(x)
my_s = 2*my_s
for i in range(int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(len(my_max))
        break


n = input()
my_s = [int(x) for x in (input().split(" "))]
my_s = 2*my_s
count = 0
max_count = 0
for  i in range( len(my_s)):
    if my_s[i]== 1:
        count += 1
        if max_count<count:
            max_count = count
    else:
        count = 0
print(max_count)


n = input()
my_s = "".join(x for x in (input().split(" ")) )
my_s = 2*my_s
for i in range(int(n)):
    if i * "1" in my_s:
        my_max = "1" *i
    else:
        print(len(my_max))
        break


n=int(input())
schedule=list(map(int,input().split()))
start=schedule.index(0)
i=start+1
i=i%n
highest,count=0,0
while i!=start:
     if schedule[i] == 1:
          count+=1
     else:
          if count>highest:
               highest=count
               count=0
     i=(i+1)%n
else:
     if count>highest:
          highest=count
          count=0
print(highest)


n=int(input())
schedule=list(map(int,input().split()))
start=schedule.index(0)
i=start+1
i=i%n
highest,count=0,0
while i!=start:
     if schedule[i] == 1:
          count+=1
     else:
          if count>highest:
               highest=count
          count=0
     i=(i+1)%n
else:
     if count>highest:
          highest=count
          count=0
print(highest)


n=int(input())
schedule=list(map(int,input().split()))
start=schedule.index(0)
i=start+1
highest,count=0,0
while i!=start:
     if schedule[i] == 1:
          count+=1
     else:
          if count>highest:
               highest=count
               count=0
     i=(i+1)%n
else:
     if count>highest:
          highest=count
          count=0
print(highest)


n = int(input())
data = [int(i) for i in input().split()]
flag = False if data[0] == 0 else True
ans = 2 if data[0] == data[-1] == 1 else 0
count = 1 if data[0] == 1 else 0
for i in range(1, n):
    if flag:
        if data[i] == 1:
            count += 1
        else:
            count = 0
            flag = False
    else:
        if data[i] == 1:
            count = 1
            flag = True
        else:
            count = 0
    ans = max(ans, count)
print(ans)


n = int(input())
data = [int(i) for i in input().split()]
if data[0] == data[-1] == 1:
    first0 = data.index(0)
    last0 = n - list(reversed(data)).index(0) - 1
    ans = data[0:first0].count(1) + data[last0:].count(1)
else:
    ans = 1 if data[0] == 1 else 0
flag = True if data[0] == 1 else False
count = 1 if flag else 0
for i in range(1, n):
    if flag:
        if data[i] == 1:
            count += 1
        else:
            count = 0
            flag = False
    else:
        if data[i] == 1:
            count = 1
            flag = True
        else:
            count = 0
    ans = max(ans, count)
print(ans)


n = int(input())
data = [int(i) for i in input().split()]
flag = False if data[0] == 0 else True
first0 = data.index(0)
last0 = list(reversed(data)).index(0)
ans = data[0:first0].count(1) + data[last0:-1].count(1) if data[0] == data[-1] else 0
count = 1 if data[0] == 1 else 0
for i in range(1, n):
    if flag:
        if data[i] == 1:
            count += 1
        else:
            count = 0
            flag = False
    else:
        if data[i] == 1:
            count = 1
            flag = True
        else:
            count = 0
    ans = max(ans, count)
print(ans)


n = int(input())
data = [int(i) for i in input().split()]
flag = False if data[0] == 0 else True
ans = 2 if data[0] == data[-1] == 1 else 0
count = 0
for i in range(n):
    if flag:
        if data[i] == 1:
            count += 1
        else:
            count = 0
            flag = False
    else:
        if data[i] == 1:
            count = 1
            flag = True
        else:
            count = 0
    ans = max(ans, count)
print(ans)


input()
x=2*list(map(int,input().split()))+[0,0,0]
days=0
i=0
D=[]

if 1 not in x:
    print(0)
    exit(0)
while i<len(x):
    if x[i]:
        days=0
        while x[i+1]:
            days+=1
            i+=1
        D.append(days)
        i+=days+1
    else:
        i+=1

print(max(D)+1)


input()
x=2*list(map(int,input().split()))+[0,0,0]
days=0
i=0
D=[]

if 1 not in x:
    print(0)
    exit(0)
while i<len(x):
    days=0
    if x[i]:
        days=0
        while x[i+1]:
            days+=1
            i+=1
        D.append(days)
        i+=1
    else:
        i+=1

print(max(D)+1)


input()
x=2*list(map(int,input().split()))+[0,0,0]
days=0
D=[]

for i in range(len(x)):
    if x[i]:
        days=0
        while x[i+1]:
            days+=1
            i+=1
        D.append(days)
print(max(D)+1)


input()
x=2*list(map(int,input().split()))+[0,0,0]
days=0
i=0
D=[]

if 1 not in x:
    print(0)
    exit(0)
while i<len(x):
    days=0
    if x[i]:
        days=0
        while x[i+1]:
            days+=1
            i+=1
        D.append(days)
        i+=days+1
    else:
        i+=1

print(max(D)+1)


input()
x=2*list(map(int,input().split()))+[0,0,0]
days=0
D=[]

if 1 not in x:
    print(0)
    exit(0)
for i in range(len(x)):
    if x[i]:
        days=0
        while x[i+1]:
            days+=1
            i+=1
        D.append(days)
print(max(D)+1)


import math

n = int(input())

a = list(map(int,input().split()))

for i in range(0,n):
    a.append(a[i])

n *= 2
ans = 0
a.append(0)
for i in range(0,n):
    cur = 0
    while a[i] == 1 and i < n:
        cur += 1
        i += 1
    if cur > ans:
        ans = cur

print(ans)


import math

n = int(input())

a = list(map(int,input().split()))

for i in range(0,n):
    a.append(a[i])

n *= 2
ans = 0
a.append(0)
while i < n:
    cur = 0
    while a[i] == 1 and i < n:
        cur += 1
        i += 1
    if cur > ans:
        ans = cur

print(ans)


n = int(input())

a = list(map(int,input().split()))

for i in range(0,n):
    a.append(a[i])

i = 0

n *= 2
ans = 0
a.append(0)
while i < n:
    cur = 0
    while a[i] == 1:
        cur += 1
        i += 1
    if cur > ans:
        ans = cur
    i += 1
print(ans)


import math

n = int(input())

a = list(map(int,input().split()))

for i in range(0,n):
    a.append(a[i])

n *= 2
ans = 0
a.append(0)
while i < n:
    cur = 0
    while a[i] == 1 and i < n:
        cur += 1
        i += 1
    if cur > ans:
        ans = cur
    i += 1

print(ans)


n = int(input())
a = list(map(int, input().split()))

mstart = -1
mend = -1
start, end = -1, -1
cur = 0
ma = -1

for i in range(n):
	if(i == (n - 1) and a[i] == 1):
		mend = i
	if(a[i] == 1):
		cur += 1
		if(mstart != -1):
			mstart = i
	if(cur >= ma):
		ma = cur
		start = mstart
		end = mend

	if(a[i] == 0):
		end = i
		cur = 0

if(end != (n - 1) and start != 0):
	print(ma)
elif(end == (n - 1)):
	for i in range(n):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)
else:
	for i in range(n - 1, -1, -1):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)


n = int(input())
a = list(map(int, input().split()))

mstart = -1
mend = -1
start, end = -1, -1
cur = 0
ma = -1

for i in range(n):
	if(i == (n - 1) and a[i] == 1):
		mend = i
	if(a[i] == 1):
		cur += 1
		if(mstart == -1):
			mstart = i
	if(cur >= ma):
		ma = cur
		start = mstart
		end = mend

	if(a[i] == 0):
		mend = i
		mstart = -1
		cur = 0

if(end != (n - 1) and start != 0):
	print(ma)
elif(end == (n - 1)):
	for i in range(n):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)
else:
	for i in range(n - 1, -1, -1):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)


n = int(input())
a = list(map(int, input().split()))

mstart = -1
mend = -1
start, end = -1, -1
cur = 0
ma = -1

for i in range(n):
	if(i == (n - 1) and a[i] == 1):
		mend = i
	if(a[i] == 1):
		cur += 1
		if(mstart != -1):
			mstart = i
	if(cur > ma):
		ma = cur
		start = mstart
		end = mend

	if(a[i] == 0):
		end = i
		cur = 0

if(end != (n - 1) and start != 0):
	print(ma)
elif(end == (n - 1)):
	for i in range(n):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)
else:
	for i in range(n - 1, -1, -1):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)


n = int(input())
a = list(map(int, input().split()))

mstart = -1
mend = -1
start, end = -1, -1
cur = 0
ma = -1

for i in range(n):
	if(i == (n - 1) and a[i] == 1):
		mend = i
	if(a[i] == 1):
		cur += 1
		if(mstart == -1):
			mstart = i
	if(cur >= ma):
		ma = cur
		start = mstart
		end = mend

	if(a[i] == 0):
		end = i
		mstart = -1
		cur = 0

if(end != (n - 1) and start != 0):
	print(ma)
elif(end == (n - 1)):
	for i in range(n):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)
else:
	for i in range(n - 1, -1, -1):
		if(a[i] == 1):
			ma += 1
		else:
			break
	print(ma)


n = int(input())
l = list(map(int, input().split()))

dw = 0

if(l[-1] == 1):
    dw += 1
dwf = dw

for i in range(n-1):
    if(l[i] == 1):
        dw += 1
        if(dw > dwf):
            dwf = dw
    else:
        dw = 0
if(l[0] != 1):
    if(l[n-1] == 1):
        dw += 1
        if(dw > dwf):
            dwf = dw
print(dwf)


n = int(input())
l = list(map(int, input().split()))

dw = 0
dwf = dw

for i in range(n):
    if(l[i] == 1):
        dw += 1
        if(dw > dwf):
            dwf = dw
    else:
        dw = 0
i = 0
while(l[i] == 1):
    dw += 1
    if(dw > dwf):
        dwf = dw
    i += 1
print(dwf)


n = int(input())
l = list(map(int, input().split()))

dw = 0

if(l[-1] == 1):
    dw += 1
dwf = dw

for i in range(n-1):
    if(l[i] == 1):
        dw += 1
        if(dw > dwf):
            dwf = dw
    else:
        dw = 0
if(l[0] != 1):
    if(l[-1] == 1):
        dw += 1
        if(dw > dwf):
            dwf = dw
print(dwf)


n = int(input())
a = input().split()
k = 1
b = []
q = 0
for i in range(len(a)):
    a[i] = int(a[i])
a = a + a
for i in range(2*n - 1):
    if a[i] != 1 and a[i] != a[i+1]:
        k = 1
        b.append(k)
    elif a[i] == 1 and a[i] == a[i+1]:
        k += 1
        b.append(k)
for i in range(len(b)):
    if b[i] > q:
        q = b[i]
print(q)


n = int(input())
a = input().split()
b = []
q = 0
for i in range(len(a)):
    a[i] = int(a[i])
a = a + a
for i in range(2*n - 1):
    if a[i] != 1 and a[i] != a[i+1]:
        k = 1
    elif a[i] == 1 and a[i] == a[i+1]:
        k += 1
        b.append(k)
for i in range(len(b)):
    if b[i] > q:
        q = b[i]
if q > 0:
    print(q)


n = int(input())
a = input().split()
b = []
q = 0
for i in range(len(a)):
    a[i] = int(a[i])
a = a + a
for i in range(2*n - 1):
    if a[i] != 1 and a[i] != a[i+1]:
        k = 1
    elif a[i] == 1 and a[i] == a[i+1]:
        k += 1
        b.append(k)
for i in range(len(b)):
    if b[i] > q:
        q = b[i]
print(q)


n = int(input())
a = input().split()
k = 1
for i in range(len(a)):
            a[i] = int(a[i])
for i in range(n-1):
            if a[i] == 1 and a[i+1] == a[i]:
                        k += 1
            elif a[i] == a[n-1] and a[i] == 1:
                        k += 1
print(k)


n = int(input())
a = input().split()
k = 1
for i in range(len(a)):
            a[i] = int(a[i])
for i in range(n-1):
            if a[i] == 1 and a[i+1] == a[i]:
                        k += 1
                        print(k)
            elif a[i] == a[n-1] and a[i] == 1:
                        k += 1
                        print(k)


n = int(input())
import random
a = [random.randint(0,1)for i in range(n)]
k = 1
for i in range(n-1):
            if a[i] == 1 and a[i] == a[i+1]:
                        k += 1
            elif a[n-1] == a[0] and a[i] == 1 and a[i] == a[i+1]:
                        k += 1


n = int(input())
a = input().split()
b = []
q = 0
for i in range(len(a)):
    a[i] = int(a[i])
a = a + a
for i in range(2*n - 1):
    if a[i] != 1 and a[i] != a[i+1]:
        k = 1
        b.append(k)
    elif a[i] == 1 and a[i] == a[i+1]:
        k += 1
        b.append(k)
for i in range(len(b)):
    if b[i] > q:
        q = b[i]
print(q)


n = int(input())
a = input().split()
b = []
for i in range(len(a)):
    a[i] = int(a[i])
a = a + a
for i in range(2*n - 1):
    if a[i] != 1 and a[i] != a[i+1]:
        k = 1
    elif a[i] == 1 and a[i] == a[i+1]:
        k += 1
        b.append(k)
print(max(b))


n = int(input())
a = input().split()
k = 0
for i in range(len(a)):
            a[i] = int(a[i])
for i in range(n-1):
            if a[i] == 1 and a[i+1] == a[i]:
                        k += 1
            elif a[i] == a[n-1] and a[i] == 1:
                        k += 1
print(k)


n = int(input())
l = list(map(int, input().split()))
l = l + l
for i in range((n*2)-1):
    if l[i]!=0:
        l[i]+=l[i-1]
print(max(l))


n = int(input())
l = list(map(str, input().split()))
count = 0
x = 0
ed = []
nol = []
s = ''.join(l)
col_1 = s.count('1')
col_0 = s.count('0')
for i in range(len(s)):
    if s[i] == '1':
        count += 1
        x -= x
        ed.append(str(count))
        m = max(ed)
    elif s[i] == '0':
        x += 1
        count -= count
        nol.append(x)
if col_1 == 0:
    m = col_1
if len(s) >= 3 and col_0!=0 and (s[0]==s[1]==s[-2]==s[-1]=='1' or s[0]==s[-2]==s[-1]=='1' or s[0]==s[1]==s[-1]=='1' or s[0]==s[-1]=='1'):
    a = ''.join(ed)
    pi = a.index('1', 1)
    ppi = a.rindex('1')
    n_k = str(len(a[:pi])+len(a[ppi:]))
    if n_k > m:
        m = n_k
print(m)


n = int(input())
a = list(map(int, input().split()))
b = []
count = 0
c = []
x = 0
m = 0
if 1 <= n <= (2*10**5) and (len(a) == n):
    for i in range(len(a)):
        if a[i] == 1:
            count += 1
            x = x - x
            b.append(count)
            m = max(b)

        elif a[i] == 0:
            x += 1
            count = count-count
            c.append(x)
    if a[0] == a[n-1] == 1 and 2 > m:
        m += 1
    print(m)


n=int(input())

l=list(map(int,input().split()))
count=0
m=[]
for i in range(len(l)):
	if(l[i]==1):
		if(i==n-1):
			for j in range(len(l)-1):
				if(l[j]==1):
					count=count+1
		count=count+1
	else:
		m.append(count)
		count=0
m.append(count)
print(max(m))


n=int(input())

l=list(map(int,input().split()))
count=0
m=[]
for i in range(len(l)):
	if(l[i]==1):
		if(i==n-1):
			if(l[0]==1):
				count=count+1
		count=count+1
	else:
		m.append(count)
		count=0
m.append(count)
print(max(m))


n=int(input())

l=list(map(int,input().split()))
count=0
m=[]
for i in range(len(l)):
	if(l[i]==1):
		if(i==n-1):
			for j in range(len(l)-1):
				if(l[j]==1):
					count=count+1
				else:
					m.append(count)
					break;
		count=count+1
	else:
		m.append(count)
		count=0
m.append(count)
print(max(m))


N = gets.to_i
s = gets.split.join
s += s[0]
p s.scan(/1+/).map{ |s| s.size }.max || 0


N = gets.to_i
s = gets.split.join
s += s
p s.scan(/1+/).map{ |s| s.size }.max || 0


n = gets.to_i
a = gets.split.map(&:to_i)
ans = 0
c = 0
a += a
(n*2).times do |i|
    if a[i] == 1
        c += 1
        ans = c if ans < c
    else
        c = 0
    end
end
puts [ans,n].min


# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
a = gets.split.map(&:to_i) # "0" works "1" rests
doublea = a*2 # simulate it with 'mod' operator in other language (looping)
longest_rests = 0
rests = 0
doublea.each { |flag|
    if flag == 0 # works
        rests = 0
    else
        rests += 1

        if rests > longest_rests
            longest_rests = rests
        end
    end
}

puts longest_rests


gets
r=(gets*2).tr('
','').gsub(/1+/).map(&:size).max
p r==nil ?0:r


gets
r=(gets*2).tr('
','').split(?0).map(&:size).max
p r==nil ?0:r


quantidade=gets
numeros=gets
vetordenumeros=numeros.split
vetordenumeros=vetordenumeros+vetordenumeros
i=0
contador=0
for numero in vetordenumeros do
    if numero.to_i==1
        i+=1
    else
        if contador<i
            contador=i
        end
        i=0
    end
end
puts contador


#In the name of God

s = raw_input()
k = int(raw_input())
w = map(int, raw_input().split())

a = sorted(s, key=lambda x: w[ord(x)-97], reverse=True)
ss = s+ ''.join(a[0]*k)
summ = 0
for i in range(len(ss)):
	summ += w[ord(ss[i])-97] * (i+1)
print summ
#print reduce(lambda res, x: int(res)+w[ord(x)-97], a[:k])


R = raw_input
s, k = R(), int(R())
w = map(int, R().split())
v = 0
for i, c in enumerate(s, 1):
	v += i * w[(ord(c) - 97)]
m = max(w)
for j in range(k):
	v += (i + j + 1) * m
print v


abc = list('abcdefghijklmnopqrstuvwxyz')
n = list(raw_input())
k = int(raw_input())
v = map(int,raw_input().split(' '))
s = 0
m = max(v)
j = 1
for i in n:
	s += j * v[abc.index(i)]
	print i,j, v[abc.index(i)],j * v[abc.index(i)],s
	j+=1
for i in range(j,j+k):
	print i,m,s
	s+= i *m
print s


abc = list('abcdefghijklmnopqrstuvwxyz')
n = list(raw_input())
k = int(raw_input())
v = map(int,raw_input().split(' '))
s = 0
m = max(v)
j = 1
for i in n:
	s += j * v[abc.index(i)]
	j+=1
for i in range(j,j+k):
	s+= i *m
print s


abc = list('abcdefghijklmnopqrstuvwxzy')
n = list(raw_input())
k = int(raw_input())
v = map(int,raw_input().split(' '))
s = 0
m = max(v)
j = 1
for i in n:
	s += j * v[abc.index(i)]
	j+=1
for i in range(j,j+k):
	s+= i *m
print s


abc = list('abcdefghijklmnopqrstuvwxzy')
n = list(raw_input())
k = int(raw_input())
v = map(int,raw_input().split(' '))
s = 0
m = 0
j = 1
for i in n:
	s += j * v[abc.index(i)]
	m = max(m,v[abc.index(i)])
	j+=1
for i in range(j,j+k):
	s+= i *m
print s


# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

s = raw_input()
k = int(raw_input())
w = map(int, raw_input().split())
c = chr(w.index(max(w)) + ord("a"))
s += c * k

ans = 0
for i, si in enumerate(s):
    ans += w[ord(s[i]) - ord("a")] * (i + 1)
print ans


s=map(lambda x:ord(x)-ord('a'),list(raw_input()))
k,f=input(),0
w=map(int,raw_input().split())
for i,x in enumerate(s):
    f+=(i+1)*w[x]
print f+(2*len(s)+1+k)*k/2*max(w)


s=map(lambda x:ord(x)-ord('a'),list(raw_input()))
k=input()
w=map(int,raw_input().split())
f=0
for i,x in enumerate(s):
    f+=(i+1)*w[x]
print f+(2*len(s)+1+k)*k/2*max(w)


s=raw_input()
k=input()
t=map(int,raw_input().split())
length=len(s)
maximum=max(t)
value=(k+length)*(k+length+1)/2-k*(k+1)/2
value*=maximum

c='abcdefghijklmnopqrstuvwxyz'

hash={}
j=0
for i in c:
    hash[i]=t[j]
    j+=1
j=1
for i in s:
    value+=j*hash[i]
    j+=1

print value


s=raw_input()
k=input()
t=map(int,raw_input().split())
length=len(s)
maximum=max(t)
value=(k+length)*(k+length+1)/2-length*(length+1)/2
value*=maximum
c='abcdefghijklmnopqrstuvwxyz'
hash={}
j=0
for i in c:
    hash[i]=t[j]
    j+=1
j=1
for i in s:
    value+=j*hash[i]
    j+=1

print value


s=raw_input()
k=input()
t=map(int,raw_input().split())
length=len(s)
maximum=max(t)
value=(k+length)*(k+length+1)/2-k*(k+1)/2
value*=maximum

c='abcdefghijklmnopqrstuvwxyz'
li=[]
for i in xrange(26):
    li+=[[t[i],c[i]]]
li=sorted(li)
k=1
for i,j in li:
    if j in s:
        value+=k*i
        k+=1
print value


s = raw_input()
k = map(int, raw_input())[0]
w = map(int, raw_input().split())

mx = max(w)
mx_i = w.index(mx)

s = s[:-1] + ''.join([chr(97 + mx_i)] * k) + s[-1]

print sum((i+1) * w[ord(c)-97] for i, c in enumerate(s))


s = raw_input()
k = map(int, raw_input())[0]
w = map(int, raw_input().split())

mx = max(w)
mx_i = w.index(mx)

s += ''.join([chr(97 + mx_i)] * k)

print sum((i+1) * w[ord(c)-97] for i, c in enumerate(s))


s = raw_input()
k = int(raw_input())
w = map(int, raw_input().split())

mx = max(w)
mx_i = w.index(mx)

s += ''.join([chr(97 + mx_i)] * k)

print sum((i+1) * w[ord(c)-97] for i, c in enumerate(s))


s = raw_input()
k = int(raw_input())
w = map(int, raw_input().split())
maxw = max(w)
res = 0
for i in range(len(s)):
  res += (i+1)*w[i]
for i in range(k):
  res += (len(s)+i+1)*maxw
print res


s = raw_input()
k = int(raw_input())
w = map(int, raw_input().split())
maxw = max(w)
res = 0
for i in range(len(s)):
  res += (i+1)*w[ord(s[i])-ord('a')]
for i in range(k):
  res += (len(s)+i+1)*maxw
print res


original=raw_input("")
num=int(raw_input(""))
weights=raw_input("")
weights=weights.split(" ")

max=int(weights[0])
for i in xrange(1,len(weights)) :
    if int(weights[i])>max :
        max=int(weights[i])
    i+=1

i=len(original)
newWeight=0
while num>0 :
    i+=1
    newWeight+=(max*i)
    num-=1

weight=0
for i in xrange(0,len(original)) :
    x=ord(original[i])-97
    weight+=(int(weights[x])*(i+1))
    i+=1

print(int(weight)+int(newWeight))


inp = raw_input

s = inp()
k = int(inp())
ch = map(int, inp().split())
m = max(ch)

ans = 0
for i,c in enumerate(s):
  ans += ch[ord(c) - ord('a')]*(i+1)
print ans + k*m*(2*len(s) + k+1)/2;


s, k, w = raw_input(), input(), map(int, raw_input().split())
print sum(i*w[ord(c)-97] for i,c in enumerate(s, 1)) + max(w)*(2*len(s)+k+1)*k/2


R = lambda:raw_input()
s, k, w, c = R(), int(R()), map(int, R().split()), 0
l, m = len(s), max(w)
for i in xrange(l, l+k):
	c += m*(i+1)
for i in xrange(l): c += w[ord(s[i])-ord('a')]*(i+1)
print c


R=lambda:raw_input()
s,k,w=R(),int(R()),map(int,R().split())
l=len(s)
c=max(w)*(k*(k+1)/2+l*k)
for i in xrange(l):c+=w[ord(s[i])-97]*(i+1)
print c


s= raw_input()
k = int(raw_input())
w = map(int,raw_input().split())
m = max(w)
res = 0
for i in range(len(s)):
    res+=(i+1)*w[ord(s[i])-97]
for i in range(len(s),len(s)+k):
    res+=(i+1)*m

print res


import string
l=lambda:map(int,raw_input().split())
s=raw_input()
k=input()
w=l()
d={}
letters=string.lowercase
for i in range(26):
    d[letters[i]]=w[i]
w=sorted(w,reverse=True)
c=0
x=len(s)
for i in range(x):
    c+=d[s[i]]*(i+1)
print c+(x*k+(k+1)*k/2)*w[0]


s = raw_input()
k = input()
w = map(int, raw_input().split())
s += chr(97+w.index(max(w)))*k
print sum([(i+1)*w[ord(s[i])-97] for i in xrange(len(s))])


s = raw_input()
n = int(raw_input())
val = [int(x) for x in raw_input().split(' ')]

ans = 0
mx = 0

for idx, i in enumerate(s):
    #print ord(i) - ord('a')
    ans += val[ord(i) - ord('a')] * (idx+1)
    mx = max(mx, val[ord(i) - ord('a')])

ans += mx * (n*len(s)+(1+n)*n/2)

print ans


s = raw_input()
n = int(raw_input())
val = [int(x) for x in raw_input().split(' ')]

ans = 0
mx = 0

for idx, i in enumerate(s):
    #print ord(i) - ord('a')
    ans += val[ord(i) - ord('a')] * (idx+1)
    #mx = max(mx, val[ord(i) - ord('a')])

ans += max(val) * (n*len(s)+(1+n)*n/2)

print ans


s=raw_input()
k=int(raw_input())
a=map(int,raw_input().split())
m=max(a)
res=0
c=1
for i in s:
	res+=c*a[ord(i)-97]
	c+=1
for i in xrange(0,k):
	res+=(c*m)
	c+=1
print res


s, k, arr = raw_input(),input(),map(int,raw_input().split())
print sum((i + 1) * arr[ord(s[i]) - 97] for i in range(len(s)))+(len(s)*2+k+1)*k/2*max(arr)


import math

str = raw_input()
n = raw_input()
n = int(n)
wc = raw_input().split()
for i in range(0, len(wc)):
	wc[i] = int(wc[i])

val = []
for i in range(0, len(str)):
	index = ord(str[i]) - 97
	val.append(wc[index])

wc.sort()
maxi = wc[len(wc)-1]
for i in range(0,n):
	val.append(maxi)

val.sort()
res = 0
for i in range(0, len(val)):
	res = res + (val[i] * (i+1))
print res


import math

str = raw_input()
n = raw_input()
n = int(n)
wc = raw_input().split()
for i in range(0, len(wc)):
	wc[i] = int(wc[i])

val = []
for i in range(0, len(str)):
	index = ord(str[i]) - 97
	val.append(wc[index])

wc.sort()
maxi = wc[len(wc)-1]
for i in range(0,n):
	val.append(maxi)

res = 0
for i in range(0, len(val)):
	res = res + (val[i] * (i+1))
print res


save = raw_input()
k = int(raw_input())
value = [int(i) for i in raw_input().split()]
key = [i for i in 'abcdefghijklmnopqrstuvwxyz']
map = {}
for i,v in zip(key,value):
    map[i] = v
max = value[0]
for i in range(1,26):
    if value[i] > max:
        max = value[i]

res = 0
n = len(save)
for i in range(n):
    res += (i+1)*map[save[i]]
for i in range(n+1,n+k+1):
    res += i*max
print res


s,k,a=raw_input(),input(),map(int,raw_input().split())
print sum((i+1)*a[ord(s[i])-97] for i in range(len(s)))+(len(s)*2+k+1)*k/2*max(a)


s=raw_input()
l=input()
x=raw_input().split(' ')
arr=[]
i=0
sm=0
maxi=0
while i<26:
    arr.append(int(x[i]))
    if arr[i]>maxi:
        maxi=arr[i]
    i=i+1
i=0
while i<len(s):
    sm=sm+arr[ord(s[i])-97]*(i+1)
    i=i+1
fir=l+len(s)
sec=len(s)
sm=sm+((fir*(fir+1))/2-(sec*(sec+1))/2)*maxi
print sm


def get_ints():
    return map(int, raw_input().strip().split(' '))

s = raw_input()
k = int(raw_input())
w = get_ints()

ans = 0
for i in xrange(len(s)):
    ans += w[ord(s[i]) - ord('a')] * (i + 1)

for i in xrange(k):
    ans += max(w) * (i + len(s) + 1)

print ans


s = raw_input()
k = int(raw_input())
a = map(int, raw_input().split())
ans = 0
for i in xrange(len(s)):
	ans+=a[ord(s[i])-97]*(i+1)
z = len(s) + 1
for i in xrange(k):
	ans+=(max(a)*z)
	z+=1
print ans


s=raw_input()
k=input()
l=map(int,raw_input().split())
s=str(s)
summ=0
m=list("abcdefghijklmnopqrstuvwxyz")
for i in xrange(len(s)):
    for j in xrange(len(m)):
        if s[i]==m[j]:
            summ+=(i+1)*l[j]
j=len(s)+1
while j<len(s)+k+1:
    summ+=j*max(l)
    j+=1
print summ


s=raw_input()
k=int(raw_input())
w=map(int,raw_input().split())
ma=0
for x in w:
    if x>ma:
        ma=x
i=1
L=len(s)
ans=0
while i<=L:
    x=ord(s[i-1])-ord('a')
    ans+=w[x]*i
    i=i+1
print ans
while i<=L+k:
    ans+=ma*i
    i=i+1
print ans


s=raw_input()
k=int(raw_input())
w=map(int,raw_input().split())
ma=0
for x in w:
    if x>ma:
        ma=x
i=1
L=len(s)
ans=0
while i<=L:
    x=ord(s[i-1])-ord('a')
    ans+=w[x]*i
    i=i+1
while i<=L+k:
    ans+=ma*i
    i=i+1
print ans


s = list(raw_input())
k = int(raw_input())
w = raw_input().split()
wi = dict()
alpha = 'abcdefghijklmnopqrstuvwxyz'
g,out = 0,0
for i in alpha:
    wi.update({i:w[g]})
    g = g+1
i = 1
for si in s:
    out = out+(i*int(wi[si]))
    i = i+1
for j in range(i,i+k):
    out = out+(j*int(wi[si]))
print out


s = list(raw_input())
k = int(raw_input())
w = raw_input().split()
w = [int(i) for i in w]
wi = dict()
alpha = 'abcdefghijklmnopqrstuvwxyz'
g,out = 0,0
for i in alpha:
    wi.update({i:w[g]})
    g = g+1
i = 1
for si in s:
    out = out+(i*int(wi[si]))
    i = i+1
w = max(w)
for j in range(i,i+k):
    out = out+(j*w)
print out


s = raw_input()
k = int(raw_input())
letters = map(int,raw_input().split())
maxval = max(letters)
res = 0
for i in range(1,len(s)+1):
    res += (letters[ord(s[i-1]) - 97]*i)

res += maxval*((k*len(s)) +(k*(k+1)/2))
print res


s = list(raw_input())
k = int(raw_input())
w = map(int,raw_input().split(" "))
lens = len(s)
score = 0
for i, si in enumerate(s):
	score += w[ord(si)-97]*(i+1)
wmax = max(w)
for i in range(k):
	score += wmax*(lens+i+1)
print score


#!/usr/bin/env python

def read():
    s = raw_input()
    k = int(raw_input())
    return k, s, map(int, raw_input().split())

def work((k, s, vList)):
    mul = max(vList)

    ans = 0

    for i in range(len(s)):
        ch = s[i]
        chIdx = ord(ch) - ord('a')
        ans += vList[chIdx] * (i + 1)

    for i in range(k):
        ans += mul * (i + k + 1)

    print ans

if __name__ == "__main__":
    work(read())


#!/usr/bin/env python

def read():
    s = raw_input()
    k = int(raw_input())
    return k, s, map(int, raw_input().split())

def work((k, s, vList)):
    mul = max(vList)

    ans = 0

    for i in range(len(s)):
        ch = s[i]
        chIdx = ord(ch) - ord('a')
        ans += vList[chIdx] * (i + 1)

    for i in range(k):
        ans += mul * (i + len(s) + 1)

    print ans

if __name__ == "__main__":
    work(read())


#!/usr/bin/python

def solve():
	s=raw_input()
	k=input()
	w=map(int,raw_input().split())
	total=0
	for i in xrange(len(s)):
		total+=w[ord(s[i])-ord('a')]*(i+1)
	alpha=max(w)
	for i in xrange(k):
		#print w[i],len(s)+(i+1)
		total+=alpha*(len(s)+(i+1))
	return total

print solve()


s = raw_input().strip()
k = int(raw_input())
a = map(int, raw_input().split())
ans = 0
i = 0
for c in s:
    i += 1
    ans += a[ord(c) - 97] * i
print ans + max(a) * (2 * i + k + 1) * k / 2


#include<stdio.h>
#include<string.h>

int main()

{
	char a[10000];
	scanf("%s",a);

	long long int x,w[10000],i,y,sum=0;
	scanf("%lld",&x);

	for(i=97;i<123;i++)
		scanf("%lld",&w[i]);

	long long int s=w[97];
	for(i=98;i<123;i++)
		if(w[i]>s)
			s=w[i];

	y=strlen(a);

	for(i=0;i<y;i++)
	{
		sum = sum + (i+1)*w[a[i]];
	}

	for(i=y+1;i<=y+x;i++)
		sum = sum + i*s;

	printf("%lld",sum);

	return 0;
}


#include<stdio.h>

int main()

{
	char string[1000];
	int i,j,c,max,ans=0,add,array[26];
	scanf("%s",string);
	scanf("%d",&add);
	for(i=0;i<26;i++)
		scanf("%d",&array[i]);
	max=array[0];
	for(i=1;i<26;i++)
	{
		if(array[i]>max)
		{
			max=array[i];
		}
	}
	for(i=0;string[i]!='\0';i++)
	{
		j=i+1;
		ans=ans+(j*array[i]);
	}
	i++;
	for(c=0;c<add;i++,c++)
	{
		ans=ans+(i*max);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>

int main()

{
	char string[1001];
	long long int i,j,c,max,ans=0,add,array[26];
	scanf("%s",string);
	scanf("%lld",&add);
	for(i=0;i<26;i++)
		scanf("%lld",&array[i]);
	max=array[0];
	for(i=1;i<26;i++)
	{
		if(array[i]>max)
		{
			max=array[i];
		}
	}
	for(i=0;string[i]!='\0';i++)
	{
		j=i+1;
		ans=ans+(j*array[string[i]-'a']);
	}
	for(c=0;c<add;i++,c++)
	{
		j=i+1;
		ans=ans+(max*j);
	}
	printf("\n%lld",ans);
	return 0;
}


#include<stdio.h>

int main()

{
	char string[1001];
	long long int i,j,c,max,ans=0,add,array[26];
	scanf("%s",string);
	scanf("%lld",&add);
	for(i=0;i<26;i++)
		scanf("%lld",&array[i]);
	max=array[0];
	for(i=1;i<26;i++)
	{
		if(array[i]>max)
		{
			max=array[i];
		}
	}
	printf("\n%lld",max);
	for(i=0;string[i]!='\0';i++)
	{
		j=i+1;
		ans=ans+(j*array[string[i]-'a']);
	}
	for(c=0;c<add;i++,c++)
	{
		j=i+1;
		ans=ans+(max);
	}
	printf("\n%lld",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char c[10000];
	scanf("%s",c);
	int a[26],i,max=-1,total,num;
	scanf("%d",&num);
	for(i=0;i<26;++i)
	{
		scanf("%d",&a[i]);
		if(a[i]>max)
		max=a[i];
	}
	for(i=0;i<strlen(c);++i)
	{
		total=total+a[c[i]-'a']*(i+1);
	}
	for(;i<num+strlen(c);++i)
	total+=(i+1)*max;
	printf("%d",total);

}


#include<stdio.h>
#include<string.h>
int main()
{
	char c[10000];
	scanf("%s",c);
	int a[26],i,max=-1,total=0,num;
	scanf("%d",&num);
	for(i=0;i<26;++i)
	{
		scanf("%d",&a[i]);
		if(a[i]>max)
		max=a[i];
	}
	for(i=0;i<strlen(c);++i)
	{
		total=total+a[c[i]-'a']*(i+1);
	}
	for(;i<num+strlen(c);++i)
	total+=(i+1)*max;
	printf("%d",total);

}


#include<stdio.h>
#include<string.h>
int main()
{
	char c[10000];
	scanf("%s",c);
	int a[26],i,max=-1,total,num;
	scanf("%d",&num);
	for(i=0;i<26;++i)
	{
		scanf("%d",&a[i]);
		if(a[i]>max)
		max=a[i];
	}
	for(i=0;i<strlen(c)&&i<num;++i)
	{
		total=total+a[c[i]-'a']*(i+1);
	}
	if(num<=strlen(c))
	printf("%d",total);
	else
	{
		for(;i<num;++i)
		total+=(i+1)*max;
		printf("%d",total);
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#define MOD 1000000007

int *array;

int min(int a, int b) {
    return a<b?a:b;
}

int max(int a, int b) {
    return a>b?a:b;
}

int compare(const void* a, const void* b) {
    return *(int*)a-*(int*)b;
}

int cmp(const void *a, const void *b){
    int ia = *(int *)a;
    int ib = *(int *)b;
    return array[ia] < array[ib] ? -1 : array[ia] > array[ib];
}

int abs(int n) {
    if(n < 0)
        return n*(-1);
    return n;
}

void swap(int *a, int *b) {
    *a = (*a)^(*b);
    *b = (*a)^(*b);
    *a = (*a)^(*b);
}

int binSearch(int a[], int x, int l, int r) {
    if(r >= l) {
        int mid = l + ((r-l)>>1);
        if(a[mid] == x)
            return mid;
        if(a[mid] > x) {
            return binSearch(a, x, l, mid-1);
        }
        else if(a[mid] < x) {
            return binSearch(a, x, mid+1, r);
        }
    }
    return -1;
}

void testCase() {
    int n, i, max_c=INT_MIN, k;
    char s[1001];
    scanf("%s", s);
    scanf("%d", &k);
    n = strlen(s);
    int a[26];
    for(i=0;i<26;i++) {
        scanf("%d", &a[i]);
        max_c = max(max_c, a[i]);
    }
    long long int res=0;
    for(i=0;i<n;i++) {
        res += (i+1)*a[i];
    }
    for(i=0;i<k;i++) {
        res += (n+i+1)*max_c;
    }
    printf("%lld", res);
}

int main() {
    int t=1;
    // scanf("%d", &t);
    while(t--) {
        testCase();
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#define MOD 1000000007

int *array;

int min(int a, int b) {
    return a<b?a:b;
}

int max(int a, int b) {
    return a>b?a:b;
}

int compare(const void* a, const void* b) {
    return *(int*)a-*(int*)b;
}

int cmp(const void *a, const void *b){
    int ia = *(int *)a;
    int ib = *(int *)b;
    return array[ia] < array[ib] ? -1 : array[ia] > array[ib];
}

int abs(int n) {
    if(n < 0)
        return n*(-1);
    return n;
}

void swap(int *a, int *b) {
    *a = (*a)^(*b);
    *b = (*a)^(*b);
    *a = (*a)^(*b);
}

int binSearch(int a[], int x, int l, int r) {
    if(r >= l) {
        int mid = l + ((r-l)>>1);
        if(a[mid] == x)
            return mid;
        if(a[mid] > x) {
            return binSearch(a, x, l, mid-1);
        }
        else if(a[mid] < x) {
            return binSearch(a, x, mid+1, r);
        }
    }
    return -1;
}

void testCase() {
    int n, i, max_c=INT_MIN, k;
    char s[1001];
    scanf("%s", s);
    scanf("%d", &k);
    n = strlen(s);
    int a[26];
    for(i=0;i<26;i++) {
        scanf("%d", &a[i]);
        max_c = max(max_c, a[i]);
    }
    long long int res=0;
    for(i=0;i<n;i++) {
        res += (i+1)*a[s[i]-'a'];
    }
    for(i=0;i<k;i++) {
        res += (n+i+1)*max_c;
    }
    printf("%lld", res);
}

int main() {
    int t=1;
    // scanf("%d", &t);
    while(t--) {
        testCase();
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
   int i,sum=0,n,arr[26],max=-1;
   char str[1001];
   gets(str);
   scanf("%d",&n);
   for(i=0;i<26;i++)
      {
       scanf("%d",&arr[i]);
       if(max<=arr[i])
           max=arr[i];

      }
    for(i=0;i<strlen(str);i++)
       {
       sum+=(arr[str[i]-97])*(i+1);

       }
    int a=strlen(str);
    for(i=a+1;i<=(a+n);i++)
       {sum+=i*max;
       }
    printf("%d\n",sum);

}


#include<stdio.h>
#include<string.h>
int main()
{
char a[1005];
int i,k,ct[26]={0},val,max=0;
scanf("%s",a);
scanf("%d",&k);
for(i=0;i<26;i++)
{scanf("%d",&val);ct[i]=val;if(val>max) max=val;}

i=0;
long long int sum=0;
while(a[i]!='\0')
{
sum+=(long long int)(ct[a[i]-97])*(i+1);
i++;
}
//printf("%lld\n",sum);
sum+=(((long long int)k*(2*i+k+1))/2*max);
printf("%lld\n",sum);
return 0;
}


#include <stdio.h>
#include <string.h>

int arr[26];

int main()
{
    int k, i, max = - 1, sum = 0;
	char str[1001];
	scanf("%s %d", str, &k);

	for(i=0; i<26; i++){
        scanf("%d", &arr[i]);
        if(arr[i] > max)
            max = arr[i];
	}

	for(i=0; i<strlen(str); i++)
        sum = sum + arr[str[i]-'a'] * (i + 1);

    for(i=i; i<(strlen(str) + k); i++)
        sum = sum + max * (i + 1);

    printf("%d", sum);

	return 0;
}


#include <string.h>
#include <stdio.h>
int main()
{
     char ch[1001];
     scanf("%s",ch);
     int len=strlen(ch);
     int k;
     scanf("%d",&k);
     int i,j,a[26];
     for(i=1;i<=26;i++)
     {
          scanf("%d",&a[i]);

     }
     int max=0,m,l;
     for(j=1;j<=26;j++)
     {
          if(a[j]>max)
          max=a[j];
     }
     int sum=0;
     for(j=1;j<=(len);j++)
     {
          m=ch[j-1]-96;
          l=a[m];
          sum+=j*l;

     }
     for(j=len+1;j<=len+k;j++)
     {
          sum+=j*max;
     }
     printf("%d",sum+1);
}