#include #include #include #include #include struct UnionFind { std::vector par, sz; int gnum; explicit UnionFind(int n) : par(n), sz(n, 1), gnum(n) { std::iota(par.begin(), par.end(), 0); } int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); } void unite(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (sz[u] < sz[v]) std::swap(u, v); sz[u] += sz[v]; par[v] = u; --gnum; } bool same(int u, int v) { return find(u) == find(v); } bool ispar(int v) { return v == find(v); } int size(int v) { return sz[find(v)]; } }; void solve() { int n, m; std::cin >> n >> m; std::vector ss(n); for (auto& s : ss) std::cin >> s; std::vector> es; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { int c = 0; for (int k = 0; k < m; ++k) { if (ss[i][k] != ss[j][k]) ++c; } es.emplace_back(c, i, j); } } std::sort(es.begin(), es.end()); int ans = 0; UnionFind uf(n); for (auto [c, u, v] : es) { if (uf.same(u, v)) continue; ans += c; uf.unite(u, v); } std::cout << ans << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }