/*#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int st, ed;
int n, m, Num[401000], cnt, Up[401000], chk[401000], par[401000], Deg[401000];
vector<int>E[401000];
vector<int>G[401000];
vector<int>P[401000];
int C;
void DFS(int a){
    Num[a] = ++cnt;
    int i, r = Num[a], x;
    for(i=0;i<E[a].size();i++){
        x = E[a][i];
        if(Num[x]){
            r = min(r, Num[x]);
            continue;
        }
        par[x] = a;
        DFS(x);
        if(Up[x] >= Num[a]){
            chk[x] = 1;
        }
        r = min(r, Up[x]);
    }
    Up[a] = r;
}
void MakeBCC(int a, int ck){
    if(ck){
        G[ck].push_back(a);
        P[a].push_back(ck);
    }
    int i, x;
    for(i=0;i<E[a].size();i++){
        x = E[a][i];
        if(par[x]!=a)continue;
        if(chk[x]){
            C++;
            G[C].push_back(a);
            P[a].push_back(C);
            MakeBCC(x,C);
        }
        else{
            MakeBCC(x,ck);
        }
    }
}
bool vv[401000];
vector<int>F[401000];
void Do2(int be, int ed){
    int i;
}
void Do(int nd, int g){
    int i,x,ee,j,nxt=-1;
    for(i=0;i<G[g].size();i++)vv[G[g][i]]=true;
    if(g == P[ed][0]){
        ee = ed;
    }
    else{
        for(i=0;i<G[g].size();i++){
            x = G[g][i];
            if(x!=ed && P[ed].size() == 2){
                ee = x;
                nxt = P[ed][0];
                if(nxt==g)nxt=P[ed][1];
            }
        }
    }
    for(i=0;i<G[g].size();i++){
        x = G[g][i];
        for(j=0;j<E[x].size();j++){
            int y = E[x][j];
            if(!vv[y])continue;
            F[x].push_back(y);
        }
    }
    Do2(nd,ee);
    for(i=0;i<G[g].size();i++){
        F[G[g][i]].clear();
        vv[G[g][i]]=false;
    }
    if(nxt!=-1){
        Do(ee,nxt);
    }
}
int main(){
    int i, a, b, j, TC;
    scanf("%d",&TC);
    while(TC--){
        scanf("%d%d%d%d",&n,&m,&st,&ed);
        for(i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            E[a].push_back(b);
            E[b].push_back(a);
        }
        cnt = 0;
        DFS(1);
        C = 1;
        MakeBCC(1,0);
        for(i=1;i<=n;i++){
            if(P[i].size()>=3){
                printf("No\n");
                return 0;
            }
        }
        if(P[st].size()>=2 || P[ed].size()>=2){
            printf("No\n");
            return 0;
        }
        for(i=1;i<=C;i++){
            for(j=0;j<G[i].size();j++){
                a = G[i][j];
                if(P[a].size() == 2) Deg[i]++;
            }
        }
        if(Deg[P[st][0]]>1 || Deg[P[ed][0]]>1){
            printf("No\n");
            return 0;
        }
        printf("Yes\n");
        Do(st,P[st][0]);
        for(i=1;i<=n;i++){
            E[i].clear();
            P[i].clear();
            chk[i]=0;
            par[i]=0;
            Num[i]=0;
        }
        for(i=1;i<=C;i++){
            G[i].clear();
            Deg[i] = 0;
        }
    }
}*/
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, m;
char p[310][310], q[310], r[310], tp[310];
int w[310][310], S[310], D[310][310], Res;
void Do(){
    int i,j;
    for(i=0;i<=n;i++){
        for(j=1;j<=n-i;j++){
            tp[j] = r[j+i];
        }
        for(j=1;j<=n-i;j++){
            S[j]=S[j-1];
            if(q[j]==tp[j])S[j]++;
        }
        for(j=0;j<=n-i;j++){
            w[j][i+j] = S[n-i] - S[j];
        }
        for(j=1;j<=n-i;j++){
            tp[j] = q[j+i];
        }
        for(j=1;j<=n-i;j++){
            S[j]=S[j-1];
            if(tp[j]==r[j])S[j]++;
        }
        for(j=0;j<=n-i;j++){
            w[i+j][j] = S[n-i] - S[j];
        }
    }
    for(i=0;i<=n;i++){
        for(j=0;j<=n;j++){
            if(i==0&&j==0)D[i][j]=0;
            else if(i==0)D[i][j]=D[i][j-1];
            else if(j==0)D[i][j]=D[i-1][j];
            else D[i][j]=min(D[i-1][j],D[i][j-1]);
            D[i][j]+=w[i][j];
        }
    }
    Res += D[n][n];
}
int main(){
    int i, j;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        scanf("%s",p[i]+1);
    }
    for(i=1;i<m;i++){
        for(j=1;j<=n;j++){
            q[j] = p[n-j+1][i];
            r[j] = p[n-j+1][i+1];
        }
        Do();
    }
    printf("%d\n",Res);
}

Measured memory_bytes: 1152