#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
//#include <bits/stdc++.h>
#include <map>
using namespace std;


int main() {
    ios_base::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    
    unordered_map<string, bool> mapa;
    
    vector<string> v(k);
    for(int i = 0; i < k; i++) {
        cin >> v[i];
        mapa[v[i]] = true;
    }
    
    long long res = 0;
    if(n == 6) {
        for(int a = 0; a < k; a++) {
            for(int b = a + 1; b < k; b++) {
                for(int c = b + 1; c < k; c++) {
                    for(int d = c + 1; d < k; d++) {
                        for(int e = d + 1; e < k; e++) {
                            for(int f = e + 1; f < k; f++) {
                                vector<string> crossword(n);
                                crossword[0] = v[a];
                                crossword[1] = v[b];
                                crossword[2] = v[c];
                                crossword[3] = v[d];
                                crossword[4] = v[e];
                                crossword[5] = v[f];
                                
                                int cnt = 0;
                                for(int i = 0; i < n; i++) {
                                    string word = "";
                                    for(int j = 0; j < n; j++) {
                                        word += crossword[i][j];
                                    }
                                    if(mapa[word]) {
                                        cnt++;
                                    }
                                    if(cnt >= 2 * n) {
                                        res++;
                                        //                            for(int x = 0; x < n; x++) {
                                        ////                                for(int y = 0; y < n; y++) {
                                        ////                                    cout << crossword[x][y];
                                        ////                                }
                                        ////                                cout << endl;
                                        ////                            }
                                        break;
                                    }
                                    word = "";
                                    for(int j = n - 1; j >= 0; j--) {
                                        word += crossword[i][j];
                                        
                                    }
                                    if(mapa[word]) {
                                        cnt++;
                                    }
                                    if(cnt >= 2 * n) {
                                        res++;
                                        //                            for(int x = 0; x < n; x++) {
                                        //                                for(int y = 0; y < n; y++) {
                                        //                                    cout << crossword[x][y];
                                        //                                }
                                        //                                cout << endl;
                                        //                            }
                                        break;
                                    }
                                    word = "";
                                    for(int j = 0; j < n; j++) {
                                        word += crossword[j][i];
                                    }
                                    if(mapa[word]) {
                                        cnt++;
                                        
                                    }
                                    if(cnt >= 2 * n) {
                                        res++;
                                        //                            for(int x = 0; x < n; x++) {
                                        //                                for(int y = 0; y < n; y++) {
                                        //                                    cout << crossword[x][y];
                                        //                                }
                                        //                                cout << endl;
                                        //                            }
                                        break;
                                    }
                                    word = "";
                                    for(int j = n - 1; j >= 0; j--) {
                                        word += crossword[j][i];
                                    }
                                    if(mapa[word]) {
                                        cnt++;
                                    }
                                    if(cnt >= 2 * n) {
                                        res++;
                                        //                            for(int x = 0; x < n; x++) {
                                        //                                for(int y = 0; y < n; y++) {
                                        //                                    cout << crossword[x][y];
                                        //                                }
                                        //                                cout << endl;
                                        //                            }
                                        break;
                                    }
                                    
                                    
                                    
                                    
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    else if(n == 3) {
        for(int a = 0; a < k; a++) {
            for(int b = a + 1; b < k; b++) {
                for(int c = b + 1; c < k; c++) {
                    vector<string> crossword(n);
                    crossword[0] = v[a];
                    crossword[1] = v[b];
                    crossword[2] = v[c];
                    
                    int cnt = 0;
                    for(int i = 0; i < n; i++) {
                        string word = "";
                        for(int j = 0; j < n; j++) {
                            word += crossword[i][j];
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
////                                for(int y = 0; y < n; y++) {
////                                    cout << crossword[x][y];
////                                }
////                                cout << endl;
////                            }
                            break;
                        }
                        word = "";
                        for(int j = n - 1; j >= 0; j--) {
                            word += crossword[i][j];
                            
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        word = "";
                        for(int j = 0; j < n; j++) {
                            word += crossword[j][i];
                        }
                        if(mapa[word]) {
                            cnt++;
                            
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        word = "";
                        for(int j = n - 1; j >= 0; j--) {
                            word += crossword[j][i];
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        
                    }
                }
            }
        }
    }
    else if(n == 5) {
        for(int a = 0; a < k; a++) {
            for(int b = a + 1; b < k; b++) {
                for(int c = b + 1; c < k; c++) {
                    for(int d = c + 1; d < k; d++) {
                        for(int e = d + 1; e < k; e++) {
                            
                     
                    vector<string> crossword(n);
                    crossword[0] = v[a];
                    crossword[1] = v[b];
                    crossword[2] = v[c];
                            crossword[3] = v[d];
                            crossword[4] = v[e];
                    
                    int cnt = 0;
                    for(int i = 0; i < n; i++) {
                        string word = "";
                        for(int j = 0; j < n; j++) {
                            word += crossword[i][j];
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
////                                for(int y = 0; y < n; y++) {
////                                    cout << crossword[x][y];
////                                }
////                                cout << endl;
////                            }
                            break;
                        }
                        word = "";
                        for(int j = n - 1; j >= 0; j--) {
                            word += crossword[i][j];
                            
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        word = "";
                        for(int j = 0; j < n; j++) {
                            word += crossword[j][i];
                        }
                        if(mapa[word]) {
                            cnt++;
                            
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        word = "";
                        for(int j = n - 1; j >= 0; j--) {
                            word += crossword[j][i];
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                    }
                }
                        
                    }
                }
            }
        }
        for(int a = 0; a < k; a++) {
            for(int b = a + 1; b < k; b++) {
                for(int c = b + 1; c < k; c++) {
                    for(int d = c + 1; d < k; d++) {
                        for(int e = d + 1; e < k; e++) {
                            
                     
                    vector<string> crossword(n, "wwwww");
                            
                            for(int j = 0; j < n; j++) {
                                crossword[0][j] = v[a][j];
                            }
                            for(int j = 0; j < n; j++) {
                                crossword[1][j] = v[b][j];
                            }
                            for(int j = 0; j < n; j++) {
                                crossword[2][j] = v[c][j];
                            }
                            for(int j = 0; j < n; j++) {
                                crossword[3][j] = v[d][j];
                            }
                                          for(int j = 0; j < n; j++) {
                                              crossword[4][j] = v[e][j];

                                          }
                    
                    
                    int cnt = 0;
                    for(int i = 0; i < n; i++) {
                        string word = "";
                        for(int j = 0; j < n; j++) {
                            word += crossword[i][j];
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
////                                for(int y = 0; y < n; y++) {
////                                    cout << crossword[x][y];
////                                }
////                                cout << endl;
////                            }
                            break;
                        }
                        word = "";
                        for(int j = n - 1; j >= 0; j--) {
                            word += crossword[i][j];
                            
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        word = "";
                        for(int j = 0; j < n; j++) {
                            word += crossword[j][i];
                        }
                        if(mapa[word]) {
                            cnt++;
                            
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                        word = "";
                        for(int j = n - 1; j >= 0; j--) {
                            word += crossword[j][i];
                        }
                        if(mapa[word]) {
                            cnt++;
                        }
                        if(cnt >= 2 * n) {
                            res++;
//                            for(int x = 0; x < n; x++) {
//                                for(int y = 0; y < n; y++) {
//                                    cout << crossword[x][y];
//                                }
//                                cout << endl;
//                            }
                            break;
                        }
                    }
                }
                        
                    }
                }
            }
        }

        
    }
    cout << res << endl;
    
    return 0;
}
