Language: C++

int same(int ii, int jj)
{
	int i;
	for(i=0; i<W; i++){
		if(S[ii][as1[i]] != S[jj][as1[W-1-i]])
			return 0;
	}
	return 1;
}
 
void srch2(int dep)
{
	int i;
	if(suc == 1)
		return ;
	if(dep==H){
		suc = 1;
		return ;
	}
	if(dep%2==0){
		if(H%2==1 && dep==H-1){
			if(same(as2[dep], as2[dep]) == 1)
				srch2(dep+1);
		}
		else{
for(i=dep; i<H; i++){
swap(as2[i], as2[dep]);
			srch2(dep+1);
swap(as2[i], as2[dep]);
}
}
		return ;
	}
	for(i=dep; i<H; i++){
		swap(as2[i], as2[dep]);
		if(sm_r[as2[dep]][as2[dep-1]] == 1){
			if(same(as2[dep], as2[dep-1]) == 1)
				srch2(dep+1);
		}
		if(suc == 1)
			return ;
		swap(as2[i], as2[dep]);
	}
}
 
void srch(int dep)
{
	int i;
	if(suc == 1)
		return ;
	if(dep==W){
		for(i=0; i<H; i++)
			as2[i] = i;
		for(i=0; i<W; i++){
			if(i%2==0){
				as1[i/2] = as[i];
			}
			else
				as1[W-1-i/2] = as[i];
		}
		srch2(0);
		return ;
	}
	if(dep%2==0){
		if(W%2==1 && dep==W-1){
			if(sm1[as[dep]]==1)
				srch(dep+1);
		}
		else{
for(i=dep; i<W; i++){
swap(as[i], as[dep]);
			srch(dep+1);
swap(as[i], as[dep]);
}
}
		return ;
	}
	for(i=dep; i<W; i++){
		swap(as[i], as[dep]);
		if(sm_c[as[dep]][as[dep-1]] == 1)
			srch(dep+1);
		if(suc == 1)
			return ;
		swap(as[i], as[dep]);
	}
}
 
void solve()
{
	int i, j, k;
	for(i=0; i<W; i++){
		for(j=0; j<W; j++){
			int a1[26], a2[26];
			for(k=0; k<26; k++)
				a1[k] = a2[k] = 0;
			for(k=0; k<H; k++){
				a1[S[k][i]-'a'] ++;
				a2[S[k][j]-'a'] ++;
			}
			for(k=0; k<26; k++){
				if(a1[k] != a2[k])
					break;
			}
			if(k==26)
				sm_c[i][j] = 1;
			else
				sm_c[i][j] = 0;
		}
	}
	for(i=0; i<H; i++){
		for(j=0; j<H; j++){
			int a1[26], a2[26];
			for(k=0; k<26; k++)
				a1[k] = a2[k] = 0;
			for(k=0; k<H; k++){
				a1[S[i][k]-'a'] ++;
				a2[S[j][k]-'a'] ++;
			}
			for(k=0; k<26; k++){
				if(a1[k] != a2[k])
					break;
			}
			if(k==26)
				sm_r[i][j] = 1;
			else
				sm_r[i][j] = 0;
		}
	}
	for(i=0; i<W; i++){
		int a1[26];
		for(k=0; k<26; k++)
			a1[k] = 0;
		for(k=0; k<H; k++){
			a1[S[i][k]-'a'] ++;
		}
		int tmp=0;
		for(k=0; k<26; k++){
			if(a1[k]%2==1)
				tmp ++;
		}
		if(tmp<=1)
			sm1[i] = 1;
		else
			sm1[i] = 0;
	}
	for(i=0; i<H; i++){
		int a1[26];
		for(k=0; k<26; k++)
			a1[k] = 0;
		for(k=0; k<W; k++){
			a1[S[i][k]-'a'] ++;
		}
		int tmp=0;
		for(k=0; k<26; k++){
			if(a1[k]%2==1)
				tmp ++;
		}
		if(tmp<=1)
			sm2[i] = 1;
		else
			sm2[i] = 0;
	}
	suc = 0;
	for(i=0; i<W; i++)
		as[i] = i;
	srch(0);
	if(suc)
		printf("YES\n");
	else
		printf("NO\n");
}
 
int main()
{
	//freopen("in.txt", "r", stdin);
	int i;
	scanf("%d%d", &H, &W);
	for(i=0; i<H; i++)
		scanf("%s", S[i]);
	solve();
	return 0;
}

Compiler output:
a.cc: In function 'int same(int, int)':
a.cc:4:20: error: 'W' was not declared in this scope
    4 |         for(i=0; i<W; i++){
      |                    ^
a.cc:5:20: error: 'S' was not declared in this scope
    5 |                 if(S[ii][as1[i]] != S[jj][as1[W-1-i]])
      |                    ^
a.cc:5:26: error: 'as1' was not declared in this scope
    5 |                 if(S[ii][as1[i]] != S[jj][as1[W-1-i]])
      |                          ^~~
a.cc: In function 'void srch2(int)':
a.cc:14:12: error: 'suc' was not declared in this scope
   14 |         if(suc == 1)
      |            ^~~
a.cc:16:17: error: 'H' was not declared in this scope
   16 |         if(dep==H){
      |                 ^
a.cc:17:17: error: 'suc' was not declared in this scope
   17 |                 suc = 1;
      |                 ^~~
a.cc:21:20: error: 'H' was not declared in this scope
   21 |                 if(H%2==1 && dep==H-1){
      |                    ^
a.cc:22:33: error: 'as2' was not declared in this scope
   22 |                         if(same(as2[dep], as2[dep]) == 1)
      |                                 ^~~
a.cc:27:6: error: 'as2' was not declared in this scope
   27 | swap(as2[i], as2[dep]);
      |      ^~~
a.cc:27:1: error: 'swap' was not declared in this scope
   27 | swap(as2[i], as2[dep]);
      | ^~~~
a.cc:34:22: error: 'H' was not declared in this scope
   34 |         for(i=dep; i<H; i++){
      |                      ^
a.cc:35:22: error: 'as2' was not declared in this scope
   35 |                 swap(as2[i], as2[dep]);
      |                      ^~~
a.cc:35:17: error: 'swap' was not declared in this scope
   35 |                 swap(as2[i], as2[dep]);
      |                 ^~~~
a.cc:36:20: error: 'sm_r' was not declared in this scope
   36 |                 if(sm_r[as2[dep]][as2[dep-1]] == 1){
      |                    ^~~~
a.cc:40:20: error: 'suc' was not declared in this scope
   40 |                 if(suc == 1)
      |                    ^~~
a.cc: In function 'void srch(int)':
a.cc:49:12: error: 'suc' was not declared in this scope
   49 |         if(suc == 1)
      |            ^~~
a.cc:51:17: error: 'W' was not declared in this scope
   51 |         if(dep==W){
      |                 ^
a.cc:52:28: error: 'H' was not declared in this scope
   52 |                 for(i=0; i<H; i++)
      |                            ^
a.cc:53:25: error: 'as2' was not declared in this scope
   53 |                         as2[i] = i;
      |                         ^~~
a.cc:56:33: error: 'as1' was not declared in this scope
   56 |                                 as1[i/2] = as[i];
      |                                 ^~~
a.cc:56:44: error: 'as' was not declared in this scope
   56 |                                 as1[i/2] = as[i];
      |                                            ^~
a.cc:59:33: error: 'as1' was not declared in this scope
   59 |                                 as1[W-1-i/2] = as[i];
      |                                 ^~~
a.cc:59:48: error: 'as' was not declared in this scope
   59 |                                 as1[W-1-i/2] = as[i];
      |                                                ^~
a.cc:65:20: error: 'W' was not declared in this scope
   65 |                 if(W%2==1 && dep==W-1){
      |                    ^
a.cc:66:28: error: 'sm1' was not declared in this scope
   66 |                         if(sm1[as[dep]]==1)
      |                            ^~~
a.cc:66:32: error: 'as' was not declared in this scope
   66 |                         if(sm1[as[dep]]==1)
      |                                ^~
a.cc:71:6: error: 'as' was not declared in this scope
   71 | swap(as[i], as[dep]);
      |      ^~
a.cc:71:1: error: 'swap' was not declared in this scope
   71 | swap(as[i], as[dep]);
      | ^~~~
a.cc:78:22: error: 'W' was not declared in this scope
   78 |         for(i=dep; i<W; i++){
      |                      ^
a.cc:79:22: error: 'as' was not declared in this scope
   79 |                 swap(as[i], as[dep]);
      |                      ^~
a.cc:79:17: error: 'swap' was not declared in this scope
   79 |                 swap(as[i], as[dep]);
      |                 ^~~~
a.cc:80:20: error: 'sm_c' was not declared in this scope
   80 |                 if(sm_c[as[dep]][as[dep-1]] == 1)
      |                    ^~~~
a.cc:82:20: error: 'suc' was not declared in this scope
   82 |                 if(suc == 1)
      |                    ^~~
a.cc: In function 'void solve()':
a.cc:91:20: error: 'W' was not declared in this scope
   91 |         for(i=0; i<W; i++){
      |                    ^
a.cc:96:36: error: 'H' was not declared in this scope
   96 |                         for(k=0; k<H; k++){
      |                                    ^
a.cc:97:36: error: 'S' was not declared in this scope
   97 |                                 a1[S[k][i]-'a'] ++;
      |                                    ^
a.cc:105:33: error: 'sm_c' was not declared in this scope
  105 |                                 sm_c[i][j] = 1;
      |                                 ^~~~
a.cc:107:33: error: 'sm_c' was not declared in this scope
  107 |                                 sm_c[i][j] = 0;
      |                                 ^~~~
a.cc:110:20: error: 'H' was not declared in this scope
  110 |         for(i=0; i<H; i++){
      |                    ^
a.cc:116:36: error: 'S' was not declared in this scope
  116 |                                 a1[S[i][k]-'a'] ++;
      |                                    ^
a.cc:124:33: error: 'sm_r' was not declared in this scope
  124 |                                 sm_r[i][j] = 1;
      |                                 ^~~~
a.cc:126:33: error: 'sm_r' was not declared in this scope
  126 |                                 sm_r[i][j] = 0;
      |                                 ^~~~
a.cc:129:20: error: 'W' was not declared in this scope
  129 |         for(i=0; i<W; i++){
      |                    ^
a.cc:133:28: error: 'H' was not declared in this scope
  133 |                 for(k=0; k<H; k++){
      |                            ^
a.cc:134:28: error: 'S' was not declared in this scope
  134 |                         a1[S[i][k]-'a'] ++;
      |                            ^
a.cc:142:25: error: 'sm1' was not declared in this scope
  142 |                         sm1[i] = 1;
      |                         ^~~
a.cc:144:25: error: 'sm1' was not declared in this scope
  144 |                         sm1[i] = 0;
      |                         ^~~
a.cc:146:20: error: 'H' was not declared in this scope
  146 |         for(i=0; i<H; i++){
      |                    ^
a.cc:150:28: error: 'W' was not declared in this scope
  150 |                 for(k=0; k<W; k++){
      |                            ^
a.cc:151:28: error: 'S' was not declared in this scope
  151 |                         a1[S[i][k]-'a'] ++;
      |                            ^
a.cc:159:25: error: 'sm2' was not declared in this scope
  159 |                         sm2[i] = 1;
      |                         ^~~
a.cc:161:25: error: 'sm2' was not declared in this scope
  161 |                         sm2[i] = 0;
      |                         ^~~
a.cc:163:9: error: 'suc' was not declared in this scope
  163 |         suc = 0;
      |         ^~~
a.cc:164:20: error: 'W' was not declared in this scope
  164 |         for(i=0; i<W; i++)
      |                    ^
a.cc:165:17: error: 'as' was not declared in this scope
  165 |                 as[i] = i;
      |                 ^~
a.cc:168:17: error: 'printf' was not declared in this scope
  168 |                 printf("YES\n");
      |                 ^~~~~~
a.cc:1:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | int same(int ii, int jj)
a.cc:170:17: error: 'printf' was not declared in this scope
  170 |                 printf("NO\n");
      |                 ^~~~~~
a.cc:170:17: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
a.cc: In function 'int main()':
a.cc:177:24: error: 'H' was not declared in this scope
  177 |         scanf("%d%d", &H, &W);
      |                        ^
a.cc:177:28: error: 'W' was not declared in this scope
  177 |         scanf("%d%d", &H, &W);
      |                            ^
a.cc:177:9: error: 'scanf' was not declared in this scope
  177 |         scanf("%d%d", &H, &W);
      |         ^~~~~
a.cc:179:29: error: 'S' was not declared in this scope
  179 |                 scanf("%s", S[i]);
      |                             ^