// Arup Guha
// 3/6/2021
// Solution to 2020 SER Problem: Longest Common Subsequence

import java.util.*;
import java.io.*;

public class lcs {

	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int nL = Integer.parseInt(tok.nextToken());
	
		// We will store adj[i][j] = 1 if we CAN move from i to j.
		int[][] adj = new int[nL][nL];
		for (int i=0; i<nL; i++) {
			Arrays.fill(adj[i], 1);
			adj[i][i] = 0;
		}
		
		// Read in each word, fill in impossible transitions.
		for (int i=0; i<n; i++) {
			char[] w = stdin.readLine().toCharArray();
			
			// You can't get from the letter at index z to index y...
			for (int z=1; z<nL; z++)
				for (int y=0; y<z; y++)
					adj[w[z]-'A'][w[y]-'A'] = -1;
		}
		
		// Since the graph is so small, we can adjust Floyd's to do max distance.
		for (int k=0; k<nL; k++) {
			for (int i=0; i<nL; i++) {
				for (int j=0; j<nL; j++) {
					if (adj[i][j] ==-1 || adj[i][k] == -1 || adj[k][j] == -1) continue;
					adj[i][j] = Math.max(adj[i][j], adj[i][k]+adj[k][j]);
				}
			}
		}
		
		// Just do the largest.
		int res = 0;
		for (int i=0; i<nL; i++)
			for (int j=0; j<nL; j++)
				res = Math.max(res, adj[i][j]);
		System.out.println(res+1);
	}
}