def produce(x, y): K = len(x) left, right = 0, K-1 while x[left] == y[left]: left += 1 while x[right] == y[right]: right -= 1 temp1 = x[:left] + [y[left]] + x[left:] temp2 = x[:right+1] + [y[right]]+ x[right+1:] return [temp1, temp2] def match(x, y): now = 0 temp = y + ["$"] for xx in x: if temp[now] == xx: now += 1 return now == len(temp) - 1 N, M = map(int, input().split()) S = [] for i in range(N): S.append(list(input())) kouho = produce(S[0], S[1]) ans = [1, 1] for i in range(1, N): for j in range(2): ans[j] &= match(kouho[j], S[i]) print(sum(ans))