n,m = map(int,input().split()) s = [list(input()) for _ in range(n)] from collections import Counter diff = 0 flg = True for i in range(m): if s[0][i] != s[1][i]: diff += 1 free = -1 if diff == 1: tmp =[] for i in range(m): if s[0][i] != s[1][i]: free = i tmp.append(s[0][i] + s[1][i]) else: tmp.append(s[0][i]) else: for ii in range(2): x,y = 0,0 diff = 0 tmp =[] if ii == 0:s1,s2 = s[0],s[1] else:s1,s2 = s[1],s[0] for i in range(m-1): if (s1[x] != s2[y]) and x == y: tmp.append(s1[x]) x += 1 idx = i if s1[x] != s2[y]: diff += 1 tmp.append(s1[x]) x+=1 y+= 1 tmp.append(s2[-1]) if diff == 0: break if diff != 0: flg = False if not flg : print(0) else: for i in range(2,n): if len(tmp) == m: for j in range(m-1): if s[i][j] != tmp[j] and j != free: flg = False elif s[i][j] != tmp[j]: if s[i][j] in tmp[j] and s[i][j+1] in tmp[j]: tmp = tmp[:free] + [s[i][j]] + [s[i][j+1]] + tmp[free+1::] break else: flg = False if len(tmp) == m+1: x,y = 0,0 diff = 0 for j in range(m-1): if (s1[i][x] != tmp[y]) and x == y: y += 1 if s1[i][x] != tmp[y]: diff += 1 if diff != 0: flg = False if not flg: print(0) if len(tmp) == m : print(2) else: print(1)