#include<cstdio>
#include<iostream>
using namespace std;
#define N 1001
int dp[N][N];
int main(){
    int m , n ;
    scanf("%d%d",&m,&n);
    char S1[N];
    char S2[N];
    scanf("%s%s",S1,S2);
    for(int i = 0 ; i <= n ; ++i){
        for(int j = 0 ; j <= m ; ++j){
            if(i == 0 || j == 0){
                dp[i][j] == 0;
                continue;
            }
            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]);
}