Language: C++

#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
int h,w,i,j,sss,ttt,k;
bool ss[12]={0,0,0,0,0,0,0,0,0,0,0,0};
bool tt[12]={0,0,0,0,0,0,0,0,0,0,0,0};
bool ssss[12]={0,0,0,0,0,0,0,0,0,0,0,0};
bool tttt[12]={0,0,0,0,0,0,0,0,0,0,0,0};
bool x,y,z;
int main()
{
  z=1;
  cin >> h >> w;
  string s[h];
  char t[w][h];
  for(i=0;i<h;i++){
    cin >> s[i];
  }
  for(i=0;i<w;i++){
    for(j=0;j<h;j++){
      t[i][j]=s[j][i];
    }
  }
  for(i=0;i<h;i++){
    sort(s[i].begin(),s[i].end());
  }
  for(i=0;i<w;i++){
    sort(t[i],t[i]+h);
  }
  for(i=0;i<h;i++){
    for(j=0;j<h;j++){
      if(s[i]==s[j]&&ss[i]!=1&&ss[j]!=1&&i!=j){ss[i]=1;
                                               ss[j]=1;
                                              }
    }
  }
  for(i=0;i<h;i++){sss+=ss[i];
                  }
  if(sss==h||sss==h-1)x=1;
  
  for(i=0;i<w;i++){
    for(j=0;j<w;j++){
      for(k=0;k<h;k++){if(t[i][k]!=t[j][k])goto P;
                      }
      if(tt[i]!=1&&tt[j]!=1&&i!=j){tt[i]=1;
                                   tt[j]=1;
                                  }
      P:;
    }
  }
  for(i=0;i<w;i++){ttt+=tt[i];
                  }
  if(ttt==w||ttt==w-1)y=1;
  if(x==1&&y==1){
    for(i=0;i<w;i++){
      if(tt[i]==0){
        for(j=0;j<h;j++){
          for(k=0;k<h;k++){if(t[i][j]==t[i][k]&&tttt[j]==0&&tttt[k]==0&&j!=k){tttt[j]=1;
                                                                              tttt[k]=1;
                                                                             }
                          }
        }
        ttt=0;
        for(j=0;j<h;j++){ttt+=tttt[j];
                  }
        if(ttt<h-1)z=0;
      }
    }
    for(i=0;i<h;i++){
      if(ss[i]==0){
        for(j=0;j<w;j++){
          for(k=0;k<w;k++){if(s[i][j]==s[i][k]&&ssss[j]==0&&ssss[k]==0&&j!=k){ssss[j]=1;
                                                                              ssss[k]=1;
                                                                             }
                          }
        }
        sss=0;
        for(j=0;j<w;j++){sss+=ssss[j];
                  }
        if(sss<w-1)z=0;
      }
    }
  }
  if(x==1&&y==1&&z==1)printf("YES");
  else printf("NO");
}

Compiler output:
a.cc: In function 'int main()':
a.cc:15:3: error: 'cin' was not declared in this scope
   15 |   cin >> h >> w;
      |   ^~~
a.cc:5:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    4 | #include<algorithm>
  +++ |+#include <iostream>
    5 | using namespace std;
a.cc:16:3: error: 'string' was not declared in this scope
   16 |   string s[h];
      |   ^~~~~~
a.cc:5:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
    4 | #include<algorithm>
  +++ |+#include <string>
    5 | using namespace std;
a.cc:19:12: error: 's' was not declared in this scope
   19 |     cin >> s[i];
      |            ^
a.cc:23:15: error: 's' was not declared in this scope
   23 |       t[i][j]=s[j][i];
      |               ^
a.cc:27:10: error: 's' was not declared in this scope
   27 |     sort(s[i].begin(),s[i].end());
      |          ^
a.cc:34:10: error: 's' was not declared in this scope
   34 |       if(s[i]==s[j]&&ss[i]!=1&&ss[j]!=1&&i!=j){ss[i]=1;
      |          ^
a.cc:74:31: error: 's' was not declared in this scope
   74 |           for(k=0;k<w;k++){if(s[i][j]==s[i][k]&&ssss[j]==0&&ssss[k]==0&&j!=k){ssss[j]=1;
      |                               ^
a.cc:86:23: error: 'printf' was not declared in this scope
   86 |   if(x==1&&y==1&&z==1)printf("YES");
      |                       ^~~~~~
a.cc:5:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    4 | #include<algorithm>
  +++ |+#include <cstdio>
    5 | using namespace std;
a.cc:87:8: error: 'printf' was not declared in this scope
   87 |   else printf("NO");
      |        ^~~~~~
a.cc:87:8: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'