#include #include #include #include #include #include using namespace std; template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } char winner(const map& dp) { set cands; cands.insert('A'); cands.insert('B'); cands.insert('C'); cands.insert('Z'); while(true) { int tot = 0; map votes; for(auto out: dp) { int i = 0; while(cands.count(out.first[i]) == 0) i++; votes[out.first[i]] += out.second; tot += out.second; } int minvotes = tot; char bad = 'Z'; for(auto out: votes) { if(2 * out.second > tot) return out.first; if(out.second <= minvotes) { minvotes = out.second; bad = out.first; } } cands.erase(bad); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; vector ballots(n); for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { char c; cin >> c; ballots[i] += c; } } if(k == 2) { int bad = 0; for(auto out: ballots) bad += out == "BA"; if(bad <= (3*n)/4) cout << "1\n"; else cout << "0\n"; return 0; } map dp; for(auto out: ballots) dp[out]++; int bamt = 0, camt = 0; bamt += dp["BAC"] + dp["BCA"]; camt += dp["CAB"] + dp["CBA"]; for(int bspoil = 0; bspoil <= bamt; bspoil++) for(int cspoil = 0; cspoil <= camt; cspoil++) { map ndp; ndp["ABCZ"] = dp["ABC"]; ndp["ACBZ"] = dp["ACB"]; ndp["BCAZ"] = dp["BCA"]; ndp["BACZ"] = dp["BAC"]; ndp["CABZ"] = dp["CAB"]; ndp["CBAZ"] = dp["CBA"]; { int bavail = bspoil; int buse = min(bavail, ndp["BCAZ"]); if(buse) { ndp["ZBCA"] += buse; ndp["BCAZ"] -= buse; bavail -= buse; if(bavail) { buse = min(bavail, ndp["BACZ"]); if(buse) { ndp["ZBAC"] += buse; ndp["BACZ"] -= buse; } } } } { int cavail = cspoil; int cuse = min(cavail, ndp["CBAZ"]); if(cuse) { ndp["ZCBA"] += cuse; ndp["CBAZ"] -= cuse; cavail -= cuse; if(cavail) { cuse = min(cavail, ndp["CABZ"]); if(cuse) { ndp["ZCAB"] += cuse; ndp["CABZ"] -= cuse; } } } } if(winner(ndp) == 'A') { cout << "1\n"; return 0; } } cout << "0\n"; }