N,M = map(int, input().split()) if N>3: print(0) exit() if N==3: print(1) if N==2: S1 = input() S2 = input() ANSSET = set() T1 = S1[0]+S2 if T1[:M]==S1: ANSSET.add(T1) T2 = S2[0]+S1 if T2[:M]==S2: ANSSET.add(T2) for i1 in range(M): if S1[i1]!=S2[i1]: break for i2 in reversed(range(M)): if S1[i2]!=S2[i2]: break if i1==i2 and 0