#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { constexpr int C = 26; int n, m; cin >> n >> m; vector s(n); REP(i, n) cin >> s[i]; if (n == 2) { while (s[0].back() == s[1].back()) { s[0].pop_back(); s[1].pop_back(); } REP(i, 2) reverse(ALL(s[i])); while (s[0].back() == s[1].back()) { s[0].pop_back(); s[1].pop_back(); } if (s[0].length() == 1) { cout << 2 << '\n'; return 0; } if (s[0][1] != s[1][0]) swap(s[0], s[1]); if (s[0][1] != s[1][0]) { cout << 0 << '\n'; return 0; } if (s[0].length() == 2) { cout << (s[0][0] == s[1][1] ? 2 : 1) << '\n'; } else { FOR(i, 1, s[0].length()) { if (s[0][i] != s[1][i - 1]) { cout << 0 << '\n'; return 0; } } assert(false); cout << 1 << '\n'; } } else { REP(i, n) reverse(ALL(s[i])); vector num[C]{}; REP(i, n) num[s[i].back() - 'a'].emplace_back(i); REP(_, m + 1) { int c = -1; REP(i, C) { if (num[i].size() >= 2) { if (c != -1) { cout << 0 << '\n'; return 0; } c = i; } } if (c == -1) { cout << 0 << '\n'; return 0; } const vector v = num[c]; num[c].clear(); for (int i : v) { s[i].pop_back(); if (!s[i].empty()) num[s[i].back() - 'a'].emplace_back(i); } } REP(i, C) { if (!num[i].empty()) { cout << 0 << '\n'; return 0; } } cout << 1 << '\n'; } return 0; }