#include #include #include #include #include using namespace std; const short max_size = 1000; short n; vector words; unordered_map mapa; short cnt_words(const vector & c) { short res = 0; unordered_map cnt_map; for(short i = 0; i < n; i++) { string s = ""; for(short j = 0; j < n; j++) { s += c[i][j]; } cnt_map[s]++; } for(short j = 0; j < n; j++) { string s = ""; for(short i = 0; i < n; i++) { s += c[i][j]; } cnt_map[s]++; } for(pair p : mapa) { res += min(p.second, cnt_map[p.first]); } return res; } int main() { ios_base::sync_with_stdio(false); short k; cin >> n >> k; for(short i = 0; i < k; i++) { string s; cin >> s; words.push_back(s); mapa[s]++; } map, bool> map_visited; vector v(n); string S = ""; for(int i = 0; i < n; i++) { S += "."; } for(int i = 0; i < n; i++) { v[i] = S; } queue> q; queue q_vis; q_vis.push(0); q.push(v); short res = 0; vector tmp; while(!q.empty()) { vector c = q.front(); q.pop(); short visited = q_vis.front(); q_vis.pop(); short cnt = 0; for(short i = 0; i < n; i++) { for(short j = 0; j < n; j++) { if(c[i][j] == '.') { cnt++; break; } } if(cnt > 0) { break; } } if(cnt == 0 and cnt_words(c) >= 2 * n) { res++; // for(short i = 0;i < n; i++) { // for(short j = 0; j < n; j++) { // cout << c[i][j]; // } // cout << endl; // } // cout << endl; continue; } if(cnt == 0) { continue; } for(short w = 0; w < k; w++) { if(!(visited & (1 << w))) { for(short i = 0; i < n; i++) { bool check = true; for(short j = 0; j < n; j++) { if(c[i][j] != '.' and c[i][j] != words[w][j]) { check = false; break; } } if(check) { tmp = c; for(short j = 0; j < n; j++) { tmp[i][j] = words[w][j]; } if(!map_visited[tmp]) { q.push(tmp); q_vis.push(visited | (1 << w)); map_visited[tmp] = true; } } } for(short j = 0; j < n; j++) { bool check = true; for(short i = 0; i < n; i++) { if(c[i][j] != '.' and words[w][i] != c[i][j]) { check = false; break; } } if(check) { tmp = c; for(short i = 0; i < n; i++) { tmp[i][j] = words[w][i]; } if(!map_visited[tmp]) { map_visited[tmp] = true; q.push(tmp); q_vis.push(visited | (1 << w)); } } } } } } cout << res << endl; return 0; }