import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

void readlnTo(T...)(ref T t) {
    auto s = readln().split();
    assert(s.length == t.length);
    foreach(ref ti; t) {
        ti = s[0].to!(typeof(ti));
        s = s[1..$];
    }
}

const real eps = 1e-10;


long cost(char[][] c, uint a, long n) {
    auto s1 = new char[](c.length);
    auto s2 = new char[](c.length);
    foreach(ulong i; iota(c.length)) {
        s1[i] = c[i][a];
        s2[i] = c[i][a+1];
    }
    s1.reverse();
    s2.reverse();
    long ans;
    void addCost() {
        foreach(ulong i; iota(min(s1.length, s2.length))) {
            ans += s1[i] == s2[i] ? 1:0;
        }
    }
    if(n < 0) {
        swap(s1, s2);
        n = -n;
    }
    while(min(s1.length, s2.length) > 0) {
        if(n > 1) {
            addCost();
            s2 = s2[1..$];
            --n;
        } else {
            addCost();
            s2 = s2[1..$];
            addCost();
            s1 = s1[1..$];
        }
    }
    return ans;
}

void main(){
    int h, w;
    readlnTo(h, w);
    char[][] c;
    foreach(i; iota(h)) {
        c ~= readln().chomp().to!(char[]);
    }
    long ans;
    foreach(uint i; iota(w-1)) {
        long minCost = long.max;
        for(long j = -h-1; j <= h+1; ++j) {
            if(j == 0) {
                continue;
            }
            minCost = min(minCost, cost(c, i, j));
        }
        ans += minCost;
    }
    writeln(ans);
}

