#!/usr/bin/env PyPy3 from collections import Counter, defaultdict, deque import itertools import re import math from functools import reduce import operator import bisect import heapq import functools mod=10**9+7 import sys input=sys.stdin.readline n,m=map(int,input().split()) s=[input().rstrip() for _ in range(n)] ans = 0 for i in range(1,n): now = 0 for k in range(m): if now < m and s[0][k] == s[i][now]: now += 1 if now != m - 1: print(0) exit() if n == 2: c = 0 for j in range(m): if s[0][j] == s[1][j]: c += 1 if c >= m - 2: print(2) else: print(1) else: l = [0] * 26 for i in range(n): tmp = [0] * 26 for j in range(m): tmp[ord(s[i][j])-ord('a')] += 1 for j in range(26): l[j] = max(l[j],tmp[j]) if sum(l[j]) == m + 1: print(1) else: print(0)