import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void times(alias pred)(int n) {
    foreach(i; 0..n) pred();
}
auto rep(alias pred, T = typeof(pred()))(int n) {
    T[] res = new T[n];
    foreach(ref e; res) e = pred();
    return res;
}

void main() {
    int H, W;
    readf("%d %d\n", &H, &W);

    if (W!=3)return;

    int[][] grid = H.rep!(() => readln.chomp.to!(char[])).transposed.map!(a => a.map!(b => b.to!char-'a'+1).array).array;

    long ans = 0;
    int[] h = W.rep!(() => 0);
    while(true) {
        if (h[1]==H || (h[0]==H && h[2]==H)) break;

        int[] cnt = W.rep!(() => 0);
        foreach(x; 0..W) {
            foreach(y1; h[x]..H) {
                foreach(y2; y1..H) {
                    if (x>0 && grid[x-1][y2] == grid[x][y1]) cnt[x]++;
                    if (x<W-1 && grid[x+1][y2] == grid[x][y1]) cnt[x]++;
                }
            }
        }
        int index;
        if (cnt[0]<cnt[1] || cnt[2]<cnt[1]) {
            index = cnt[0]>cnt[2] ? 0:2;
        } else {
            index = 1;
        }
        foreach(j; 0..H) {
            auto y = H-j-1;
            if (y>=h[index]) {
                if (index>0 && grid[index-1][y] == grid[index][y]) ans++;
                if (index<W-1 && grid[index+1][y] == grid[index][y]) ans++;
            }
            if (y>h[index]) {
                grid[index][y] = grid[index][y-1];
            } else if (y==h[index]) {
                grid[index][y] = -1;
                h[index]++;
            }
        }
    }
    ans.writeln;
}
