#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, m; cin >> n >> m; vector s(n); for(int i = 0; i < n; i++) cin >> s[i]; auto ok = [&](string t){ vector indices(n); for(int j = 0; j <= m; j++){ for(int i = 0; i < n; i++){ if(t[j] == s[i][indices[i]]){ indices[i]++; }else{ if(indices[i] == j-1) return false; } } } return true; }; for(int j = 0; j < m; j++){ vector cnt(26); for(int i = 0; i < n; i++){ cnt[s[i][j]-'a']++; } int cnt_max = *max_element(cnt.begin(), cnt.end()); if(cnt_max == n) continue; if(cnt_max == n-1){ int ans = 0; for(int i = 0; i < 26; i++){ if(cnt[i] == 1){ int ii = -1; for(int k = 0; k < n; k++) { if(s[k][j]-'a' == i) ii = k; } int jj = (i+1)%n; string t = s[ii].substr(0, j); t += s[jj][j]; t += s[ii].substr(j, m-j); // debug_value(t) if(ok(t)) ans++; } } cout << ans << endl; return 0; }else{ cout << 0 << endl; return 0; } } }