#include <bits/stdc++.h>
using namespace std;
const int N=1005;
int f[N][N];
int n,m;
int main(){
memset(f, 0x00, sizeof f);
cin>>n>>m;
string a, b;
cin>>a>>b;
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j){
f[i][j]=max(f[i-1][j], f[i][j-1]);
if(a[i-1]==b[j-1])
f[i][j]=max(f[i][j], f[i-1][j-1]+1);
}
cout<<f[n][m]<<endl;
return 0;
}