n,m = map(int,input().split())
a = input()
b = input()
al = len(a) + 1
bl = len(b) + 1
f = [[0 for _ in range(bl)]for _ in range(al)]
for i in range(1,al):
for j in range(1,bl):
if a[i-1] == b[j-1]:
f[i][j] += f[i-1][j-1] + 1
else:
f[i][j] = max(f[i-1][j],f[i][j-1])
print(f[al-1][bl-1])