if __name__ == '__main__':
n, m = map(int, input().split())
a = list(input())
b = list(input())
dp = [[0] * (m) for _ in range(n)]
for i in range(0, n):
for j in range(0, m):
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
# 找到一个 lcs 中的字符
if a[i] == b[j]: dp[i][j] = max(1 + dp[i - 1][j - 1], dp[i][j])
print(dp[-1][-1])