#include <iostream>
using namespace std;
int n,m;
const int N=1005;
int f[N][N];
char a[N],b[N];
int main()
{
cin>>n>>m;
cin>>a+1;
cin>>b+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
//忽略00情况
f[i][j]=max(f[i][j-1],f[i-1][j]);
if(a[i]==b[j]) f[i][j]=max(f[i][j],f[i-1][j-1]+1);
}
cout<<f[n][m];
//cout << "Hello world!" << endl;
return 0;
}