#include <iostream>

#define N 1010
using namespace std;

int dp[N][N];

int main() {
    int m, n;
    char s1[N], s2[N];
    scanf("%d%d", &n, &m);
    scanf("%s%s", s1, s2);
    for (int i = 0; i <= n; i++) {  //0-n要包含n
        for (int j = 0; j <= m; ++j) {
            if (i == 0 || j == 0) {
                dp[i][j] = 0;
                continue;
            } else {
                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], dp[i][j - 1]);
                }
            }
        }
    }
    printf("%d\n", dp[n][m]);
    return 0;
}