#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int N, L; string S[1000]; int D[1000][1000] = {}; struct DSU { int N; vector V; DSU(int n) { N = n; V.resize(N); rep(i, N) V[i] = i; } int root(int a) { if (V[a] == a) return a; return V[a] = root(V[a]); } void unite(int r, int s) { V[root(s)] = root(r); } }; int main() { cin >> N >> L; rep(i, N) cin >> S[i]; rep(i, N) rep(j, i) rep(k, L) D[i][j] += ((S[i][k] != S[j][k]) ? 1 : 0); priority_queue>> G; rep(i, N) rep(j, i) G.push({ -D[i][j],{i,j} }); DSU dsu(N); int ans = 0; while (G.size()) { int d = -G.top().first, u = G.top().second.first, v = G.top().second.second; G.pop(); if (dsu.root(u) == dsu.root(v)) continue; dsu.unite(u, v); ans += d; } cout << ans << endl; return 0; }