#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
char s1[N], s2[N];
int len1, len2;
int c[N][N];
int main()
{
//最优子结构状态值初始化(当然也可以不写,因为全局变量默认为0)
memset(c, 0, sizeof(c));
//输入序列s1和s2的长度
cin>>len1>>len2;
//输入需要求最长公共子序列的序列s1和s2
cin>>s1+1>>s2+1;
for(int i = 1; i <= len1; i ++){
for(int j = 1; j <= len2; j ++){
if(s1[i] == s2[j])c[i][j] = c[i - 1][j - 1] + 1;
else c[i][j] = max(c[i - 1][j], c[i][j - 1]);
}
}
cout<<c[len1][len2]<<'\n';
return 0;
}