from collections import defaultdict from functools import lru_cache from sys import flags, stdin import math import re import queue import typing import itertools import bisect import statistics # import numpy as np # from numpy.core.function_base import _needs_add_docstring # from numpy.core.numeric import outer input = stdin.readline MOD = 1000000007 INF = 122337203685477580 def solve(): n, m = map(int, input().split()) if n > 2: print(0) return a = input() b = input() # print(a[1:-1]) # print(b[:-2]) if a[1:-1] == b[:-2] and b[1:-1] == a[:-2]: print(2) elif a[1:-1] == b[:-2] or b[1:-1] == a[:-2]: print(1) else: print(0) return if __name__ == '__main__': solve()