def check(S, T): N = len(S) i = 0 j = 0 memo = 0 while i < N: if S[i] == T[j]: i += 1 j += 1 elif memo == 0: memo = (i, T[j]) i += 1 else: return False, -1 return True, memo def main(): N, M = map(int, input().split()) S = input() T = input() U = [input() for _ in range(N - 2)] b1, m1 = check(S, T) b2, m2 = check(T, S) res = 0 for _ in range(2): b, m = check(S, T) if b: i, c = m X = S[:i] + c + S[i:] ok = True for u in U: bb, mm = check(X, u) if not bb: ok = False break res += ok S, T = T, S return res print(main())