9
lfpgbnlzyn
c
s
w
r
m
q
y
yinfblfcdhidphyfvgkxyuwomahiibbhnigdslsguhjkplibnhhrshtekwgefxeugyyyyy
 6
u
p
m
z
v
vv
 4
qe
nd
iqryhukieskfvaeettersinksrmazelxtgvartuz
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 6
nf
ysvchtjkfgsbyfzxjaykey
advqnf
wl
pi
ppppfvvmaebvhkoagppppp
 3
j
k
kkailgwqnxtdsttkkkkkkk
 4
o
yhnx
e
ennn
 10
j
dyfu
udzj
wwow
xtnw
f
c
q
o
gggg
 6
w
g
c
j
y
f
 4
r
o
w
wwwwwwwwww
 2
zxwp
ppppctfisvflhtjggg
 5
m
g
u
t
tttt
 2
n
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 3
lt
en
eeeeeeeeee
 4
vr
hp
l
llbn

7 5 541 10 10 4 4 1 21 5 9 93 21 3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringMultiplication {
	static int[][] dp;
	static int N;
	
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
		// input file name goes above
		
		N = Integer.parseInt(sc.readLine());
		
		dp = new int[N+1][26];
		for(int i = 1; i <= N; i++) {
			String p = sc.readLine();
			char suff = p.charAt(p.length()-1);
			int suffl = 0;
			char pref = p.charAt(0);
			int prefl = 0;
			for(int e = 0; e < p.length() && p.charAt(e) == pref; e++, prefl++);
			for(int e = p.length()-1; e >= 0 && p.charAt(e) == suff; e--, suffl++);
			if(prefl == p.length()) {
				int pre = convert(pref);
				dp[i][pre] = (dp[i-1][pre]+1)*p.length() + dp[i-1][pre];
			}
			else if(suff == pref) {
				if(dp[i-1][convert(pref)] != 0) {
					dp[i][convert(pref)] = prefl + suffl + 1;
				}
			}
			else {
				if(dp[i-1][convert(pref)] != 0) {
					dp[i][convert(pref)] = prefl + 1;
				}
				if(dp[i-1][convert(suff)] != 0) {
					dp[i][convert(suff)] = suffl + 1;
				}
			}
			p+= "\n";
			char curr = ' ';
			int length = 0;
			for(int e = 0; e < p.length(); e++) {
				if(p.charAt(e) != curr) {
					if(length != 0) {
						dp[i][convert(curr)] = Math.max(dp[i][convert(curr)], length);
					}
					curr = p.charAt(e);
					length = 1;
					continue;
				}
				length++;
			}
			for(int e = 0; e < 26; e++) {
				if(dp[i-1][e] > 0 && dp[i][e] == 0) {
					dp[i][e] = 1;
				}
			}
			}
		
		int ans = 0;
		for(int i = 0; i < 26; i++) {
			ans = Math.max(dp[N][i], ans);
		}
		System.out.println(ans);
		
		sc.close();
	}
	
	public static int convert(char c) {
		return (c-'a');
	}

}
