import sys input = sys.stdin.readline N,M=map(int,input().split()) S=[input().strip() for _ in range(N)] base1 = [] base2 = [] s0,s1=S[0],S[1] for i in range(M): s0i=s0[i] s1i=s1[i] if s0[i]==s1[i]: base1.append(s0i) base2.append(s0i) else: if i==M-1: base1.append(s0i) base1.append(s1i) base2.append(s1i) base2.append(s0i) break if s0[i+1]==s1[i+1]: base1.append(s0i) base1.append(s1i) base2.append(s1i) base2.append(s0i) for j in range(i+2,M): base1.append(s0[j]) base2.append(s1[j]) else: if s0[i+1]==s1i: base1.append(s0i) base1.append(s1i) base1.append(s1[i+1]) for j in range(i+2,M): base1.append(s1[j]) if s1[i+1]==s0i: base2.append(s1i) base2.append(s0i) base2.append(s0[i+1]) for j in range(i+2,M): base2.append(s0[j]) break base1 = "".join(base1) if base1 is not None else None base2 = "".join(base2) if base2 is not None else None def judge(s,b): j=0 for i,ss in enumerate(s): if ss!=b[j]: j+=1 if j>=len(b) or ss!=b[j]: return False j+=1 if j>len(b):return False return j<=len(b) ans=0 for b in [base1,base2]: if b is None:continue if len(b)!=M+1:continue for s in S: if judge(s,b):continue break else: ans+=1 print(ans)