Language: C++

# include <bits/stdc++.h>

# define x first    
# define y second
# define mp make_pair
// everything go according to my plan      
# define pb push_back
# define sz(a) (int)(a.size())
# define vec vector         
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
# define y1    Y_U_NO_y1
# define left  Y_U_NO_left
# define right Y_U_NO_right  

using namespace std;

typedef pair <int, int> pii; 
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap () {
  ios_base :: sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
}
inline void randomizer3000 () {
  unsigned int seed;
  asm ("rdtsc" : "=A"(seed));
  srand (seed);
}
void files (string problem) {   
  if (fopen ((problem + ".in").c_str(),"r")) {
    freopen ((problem + ".in").c_str(),"r",stdin);
    freopen ((problem + ".out").c_str(),"w",stdout);
  }
}
void localInput(const char in[] = "s") { 
  if (fopen (in, "r")) {
    freopen (in, "r", stdin);
  }
  else
    cerr << "Warning: Input file not found" << endl;
}
const int N = 12 + 1;

int n, m;
               
string s[13];


struct matching { 
  vec<int> g[N];
  int n;
  bool used[N];
  void add_edge (int a, int b) {
    g[a].pb (b); g[b].pb (a);
  }
  bool solve (int p = 0, bool mid = 0) {      
    if (p == n) {
      return 1;
    }
    if (used[p])  return solve (p + 1, mid);
    
    if ((n & 1) && !mid) {
      used[p] = 1;
      if (solve (p + 1, 1)) 
        return 1;
      used[p] = 0;
    }   

    for (auto to : g[p]) {
      if (!used[to]) {
        used[to] = used[p] = 1;
        if (solve (p + 1, mid))
          return 1;
        used[to] = used[p] = 0;
      }
    }
    return 0;
  }     
} M[7484400];
  
int cnt = 0;
int getId (vec<int> &a) { 
  static map <vec<int>, int> id;
  int &x = id[a];
  cnt = sz(id);
  if (!x) 
    return x = sz(id);
  else
    return x;
}
vec<int> mt;
    
void getcol (int x, int y, int p = 0, bool mid = 0) {     

  if (~mt[p]) {
    getcol (x, y, p + 1, mid);
    return;
  }
  if (p == m) {
    int a; 
    M[a = getId (mt)].add_edge (x, y);
    return;
  }
  if ((m & 1) && !mid && s[x][p] == s[y][p]) {
    mt[p] = p;              
    getcol (x, y, p + 1, 1);
    mt[p] = -1;
  }                         
  for (int i = p+1; i < m; i++) {
    if (mt[i] == -1 && s[x][p] == s[y][i] && s[x][i] == s[y][p]) {       
      mt[p] = i;
      mt[i] = p; 
      getcol (x, y, p + 1, mid);
      mt[i] = mt[p] = -1;
    }
  }
}                               
int main()
{
  # ifdef Local
    //localInput();
  # endif                                      
  Read_rap();   
  cin >> n >> m;
  for (int i = 0; i < n; i++)   
    cin >> s[i];
  
  mt.resize (m + 1, -1);
           
  for (int i = 0; i < n; i++) {
    for (int j = i+1; j < n; j++) {
      getcol (i, j);
    }
  }

  for (int i = 1; i <= cnt; i++) {
    M[i].n = n;
    if (M[i].solve()) {
      cout << "YES";
      return 0;
    }
  }
  cout << "NO";


  return 0;
}






// Coded by Z..



Compiler output:
/tmp/ccomLPa4.o: in function `getId(std::vector<int, std::allocator<int> >&)':
a.cc:(.text+0x1e3): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x1f3): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x20b): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x224): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x23d): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x253): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x266): relocation truncated to fit: R_X86_64_PC32 against `.bss'
a.cc:(.text+0x274): relocation truncated to fit: R_X86_64_PC32 against symbol `cnt' defined in .bss section in /tmp/ccomLPa4.o
a.cc:(.text+0x285): relocation truncated to fit: R_X86_64_PC32 against `.bss'
/tmp/ccomLPa4.o: in function `getcol(int, int, int, bool)':
a.cc:(.text+0x2cb): relocation truncated to fit: R_X86_64_PC32 against symbol `mt' defined in .bss section in /tmp/ccomLPa4.o
a.cc:(.text+0x311): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status