#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
// #define DEBUGGING

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

namespace __init {

struct InitIO {
    InitIO() {
        cin.tie(0);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
#include "../debug.cpp"
#else
#define DEBUG(...) 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

const ll INF = 5e15;
VV<ll> memo(310, V<ll>(310, INF));
ll H, W;
V<ll> cost_l, cost_r;

ll calc(ll l, ll r) {
    if(memo[l][r] != INF) return memo[l][r];
    if(r - l <= 1) return 0;

    ll ret = INF;
    for(ll del = l; del < r; del++) {
        ll cost = 0;
        if(l <= del - 1) {
            cost += cost_l[del];
            cost += calc(l, del);
            DEBUG(l, r, del, cost, cost_l[del], calc(l, del));
        }
        if(del + 1 < r) {
            cost += cost_r[del];
            cost += calc(del + 1, r);
        }
        DEBUG(l, r, del, cost);
        chmin(ret, cost);
    }

    memo[l][r] = ret;
    return memo[l][r] = ret;
}

ll calc(const VV<ll> &imos, VV<ll> &memo, ll h1, ll h2) {
    if(memo[h1][h2] != INF) return memo[h1][h2];
    if(h1 == H || h2 == H) return 0;
    ll ret = INF;
    if(h1 < H) chmin(ret, calc(imos, memo, h1 + 1, h2) + imos[h1][h2]);
    if(h2 < H) chmin(ret, calc(imos, memo, h1, h2 + 1) + imos[h1][h2]);
    return memo[h1][h2] = ret;
}

int main() {
    cin >> H >> W;
    V<string> vs(H);
    cost_l = V<ll>(W, 0);
    cost_r = V<ll>(W, 0);
    for(auto &s : vs) cin >> s;
    auto sum = make_v(0, W, H + 1, 26);

    for(ll w = 0; w < W; w++) {
        for(ll h = 0; h < H; h++) {
            for(ll i = 0; i < 26; i++) sum[w][h + 1][i] = sum[w][h][i];
            sum[w][h + 1][vs[h][w] - 'a']++;
        }
    }

    for(ll w = 0; w < W - 1; w++) {
        auto imos = make_v((ll)0, H + 1, H + 1);
        for(ll h1 = 0; h1 < H; h1++) for(ll h2 = 0; h2 < H; h2++) {
            if(vs[h1][w] != vs[h2][w + 1]) continue;
            ll dh = abs(h1 - h2);
            ll ph1 = (h1 > h2 ? 0 : dh);
            ll ph2 = (h1 > h2 ? dh : 0);
            imos[ph1][ph2]++;
            ll hmax = max(h1, h2);
            imos[ph1 + (H - hmax)][ph2 + (H - hmax)]--;
        }
        for(ll h1 = 0; h1 < H; h1++) for(ll h2 = 0; h2 < H; h2++) imos[h1 + 1][h2 + 1] += imos[h1][h2];
        auto memow = make_v(INF, H + 1, H + 1);
        ll cost = calc(imos, memow, 0, 0);
        DEBUG(w, imos, cost);
        cost_r[w] = cost;
        cost_l[w + 1] = cost;
    }

    DEBUG(cost_l, cost_r);
    cout << calc(0, W) << endl;
    return 0;
}


Measured memory_bytes: 14680