#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int n, m;
string s1, s2;
int main() {
    cin >> n >> m >> s1 >> s2;
    vector<vector<int>> dp(n+1, vector<int>(m + 1, 0));
    for (int i = 1; i <=n; i++)
        for (int j = 1; j <=m; j++) {
            if (s1[i-1] == s2[j-1]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else  dp[i][j] = max(dp[i - 1][j - 1], max(dp[i][j - 1], dp[i - 1][j]));
        }
    cout<<dp[n][m];
}
// 64 位输出请用 printf("%lld")