#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 100;
int n, m;
char s[maxn], t[maxn];
int dp[maxn][maxn];
void solve()
{
cin >> n >> m;
cin >> s >> t;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i] == t[j])
{
dp[i + 1][j + 1] = dp[i][j] + 1;
}
else
{
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
}
}
}
cout << dp[n][m] << endl;
}
int main()
{
solve();
}