Language: C++

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i,j) for(int i=0;i<j;i++)
#define FORN(i,j,k) for(int i=j;i<k;i++)
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define vpii vector<pii >
#define all(a) a.begin(),a.end()
using namespace std;
const int INF=1e9+7;
int n,m;
string s[15];
int cnt[15][30]={0}; 
int kinds=0;
int exm[15][30]={0};
vi sum[15];
int next[15];
bool check1(int a,int b){
	int A[30][30]={0},B[30][30]={0};
	REP(i,m)A[s[a][i]-'a'][s[b][i]-'a']++,B[s[a][i]-'a'][s[b][i]-'a']++;
//	REP(i,26){
//		REP(j,26)cout<<A[i][j]<<' ';
//		cout<<endl;
//	}
	int _cnt=-m%2;
	REP(i,26){
		REP(j,26){
			if(B[i][j]!=0){
				if(B[i][j]!=B[j][i]){
//					cout<<"re:"<<i<<' '<<j<<endl;
					return 0;
				}
				if(i==j&&B[i][j]%2==1)_cnt++;
				B[i][j]=B[j][i]=0;
			}
		}
	}
//	cout<<_cnt<<endl;
	if(_cnt!=0)return 0;
	string str=s[0];
//	cout<<str.size()<<endl;
	_cnt=0;
	REP(i,26){
		REP(j,26){
			if(i==j&&A[i][j]%2==1){
				str[m/2]=(char)(i+'a');
				A[i][j]--;
			}
			while(A[i][j]>0){
				str[_cnt]=(char)(i+'a');
				str[m-_cnt-1]=(char)(j+'a');
				_cnt++;
				A[i][j]--;
				A[j][i]--;
			}
		}
	}
//	cout<<str<<endl;
	memset(next,(int)0,sizeof(next));
	string str1=str;
	reverse(all(str1));
	bool used[15]={0};
	REP(i,m){
		REP(j,m)if((!used[j])&&str[j]==s[a][i]&&str1[j]==s[b][i]){
			next[i]=j;
			used[j]=1;
		}
	}
//	REP(i,m)cout<<next[i]<<' ';
//	cout<<endl;
	return 1;
}
bool check(int pos){
	string str[15];
	bool used[15]={0};
	REP(i,n)str[i]=s[i];
	REP(i,n){
		REP(j,m)str[i][next[j]]=s[i][j]; 
	}
	REP(i,kinds){
		if((sum[i].size())%2==1){
			used[pos]=1;
		}
		REP(j,sum[i].size()){
			if(used[sum[i][j]])continue;
			used[j]=1;
			bool flag=0;
			REP(k,sum[i].size()){
				if(used[sum[i][k]])continue;
				string ss=str[sum[i][k]];
				reverse(all(ss));
				if(ss==str[sum[i][j]]){
					flag=1;
					used[sum[i][k]]=1;
					break;
				}
			}
			if(!flag){
				return 0;
			}
		}
	}
	return 1;
}
int main(void){
	cin>>n>>m;
	REP(i,n)cin>>s[i];
	if(n==1&&m==1){
		cout<<"YES";
		return 0;
	}
	REP(i,n)REP(j,m)cnt[i][s[i][j]-'a']++;
	REP(i,n){
		bool ok=0;
		REP(j,kinds){
			bool flag=1;
			REP(k,26)if(cnt[i][k]!=exm[j][k]){
				flag=0;
				break;
			}
			if(flag){
				ok=1;
				sum[j].pb(i);
				break;
			}
		}
		if(!ok){
			REP(j,26)exm[kinds][j]=cnt[i][j];
			sum[kinds].pb(i);
			kinds++;
		}
	}
	int _cnt=-n%2;
	REP(i,kinds)_cnt+=(sum[i].size())%2;
//	cout<<_cnt<<endl;
	if(_cnt!=0){
		cout<<"NO";
		return 0;
	}
	/*REP(i,kinds){
		_cnt=-m%2;
		REP(j,26)_cnt+=exm[i][j]%2;
		cout<<_cnt<<endl;
		if(_cnt!=0){
			cout<<"NO";
			return 0;
		}
	}*/
//	cout<<1<<endl;
//	cout<<kinds<<endl;
//	REP(i,kinds){
//		REP(j,sum[i].size())cout<<sum[i][j]<<' ';
//		cout<<endl;
//	}
	int it=-1,k=INF;
	REP(i,kinds){
		if((sum[i].size())%2==1){
			it=i;
			k=sum[i].size();
			break;
		}
		if(sum[i].size()<k){
			k=sum[i].size();
			it=i;
		}
	} 
//	cout<<it<<' '<<k<<endl;
	if(k%2==1){
		bool ok=0;
		REP(i,k){
			if(check1(sum[it][i],sum[it][i]))if(check(sum[it][i])){
				ok=1;
				break;
			}
		}
		if(ok){
			cout<<"YES";
		}
		else{
			cout<<"NO";
		}
	}
	else{
		bool ok=0;
		FORN(i,1,k){
			if(check1(sum[it][0],sum[it][i])){
//				cout<<0<<' '<<i<<endl;
				if(check(-1)){
					ok=1;
					break;
				}
			}
		}
		if(ok){
			cout<<"YES";
		}
		else{
			cout<<"NO";
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'bool check1(int, int)':
a.cc:65:16: error: reference to 'next' is ambiguous
   65 |         memset(next,(int)0,sizeof(next));
      |                ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:5: note:                 'int next [15]'
   23 | int next[15];
      |     ^~~~
a.cc:65:35: error: reference to 'next' is ambiguous
   65 |         memset(next,(int)0,sizeof(next));
      |                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:5: note:                 'int next [15]'
   23 | int next[15];
      |     ^~~~
a.cc:71:25: error: reference to 'next' is ambiguous
   71 |                         next[i]=j;
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:5: note:                 'int next [15]'
   23 | int next[15];
      |     ^~~~
a.cc: In function 'bool check(int)':
a.cc:84:32: error: reference to 'next' is ambiguous
   84 |                 REP(j,m)str[i][next[j]]=s[i][j];
      |                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:5: note:                 'int next [15]'
   23 | int next[15];
      |     ^~~~