Language: C++

#include<iostream>
#include<unordered_set>
#include<tuple>
#include<vector>

using namespace std;

typedef pair<char,char> PII;
typedef tuple<unordered_set<PII>,unordered_set<PII>,unordered_set<PII> > T;

bool neighbor(const unordered_set<PII> &s,PII p){
  for(int i=0;i<4;i++){
    static int d[]={0,1,0,-1,0};
    auto pp=make_pair(p.first+d[i],p.second+d[i+1]);
    if(s.count(pp))return true;
  }
  return false;
}

__int128 encode(const unordered_set<PII> &s){
  __int128 i=0;
  for(auto &e:s){
    i|=__int128(1)<<e.first*10+e.second;
  }
  return i;
}

tuple<__int128,__int128,__int128> encode(const T &t){
  return make_tuple(encode(get<0>(t)),encode(get<1>(t)),encode(get<2>(t)));
}

int main(){
  for(int w,h;cin>>w>>h,w;){
    unordered_set<PII> s;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	int r;
	cin>>r;
	if(r){
	  s.insert({i,j});
	}
      }
    }
    auto b=*s.begin();
    s.erase(s.begin());
    bool flag=false;
    if(s.size()%2){
      for(auto e:s){
	auto is=s;
	is.erase(e);
	for(int i=0;i<4;i++){
	  for(int j=0;j<2;j++){
	    unordered_set<tuple<__int128,__int128,__int128> > ss;
	    unordered_set<PII> bs,es;
	    bs.insert(b);
	    es.insert(e);
	    T ist{bs,es,is};
	    vector<T> v{ist};
	    while(!v.empty()){
	      auto cs=v.back();
	      if(get<2>(cs).empty())break;
	      v.pop_back();
	      if(!ss.insert(encode(cs)).second)continue;
	      //	      cout<<get<2>(cs).size()<<endl;
	      for(auto f:get<2>(cs)){
		if(!neighbor(get<0>(cs),f))continue;
		auto vec=make_pair(f.first-b.first,f.second-b.second);
		if(j){
		  swap(vec.first,vec.second);
		}
		for(int k=0;k<i;k++){
		  vec=make_pair(-vec.second,vec.first);
		}
		auto d=PII(e.first+vec.first,e.second+vec.second);
		const auto &rs=get<2>(cs);
		if(d!=f&&rs.count(d)&&neighbor(get<1>(cs),d)){
		  auto ns=cs;
		  get<0>(ns).insert(f);
		  get<1>(ns).insert(d);
		  get<2>(ns).erase(f);
		  get<2>(ns).erase(d);
		  if(ss.count(encode(ns))==0)
		    v.push_back(ns);
		}
	      }
	    }
	    flag|=!v.empty();
	  }
	}
      }
    }
    cout<<(flag?"YES":"NO")<<endl;
  }
}

		  
    

Compiler output:
a.cc: In function 'int main()':
a.cc:34:24: error: use of deleted function 'std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::pair<char, char>; _Hash = std::hash<std::pair<char, char> >; _Pred = std::equal_to<std::pair<char, char> >; _Alloc = std::allocator<std::pair<char, char> >]'
   34 |     unordered_set<PII> s;
      |                        ^
In file included from /usr/include/c++/14/unordered_set:41,
                 from a.cc:2:
/usr/include/c++/14/bits/unordered_set.h:142:7: note: 'std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::pair<char, char>; _Hash = std::hash<std::pair<char, char> >; _Pred = std::equal_to<std::pair<char, char> >; _Alloc = std::allocator<std::pair<char, char> >]' is implicitly deleted because the default definition would be ill-formed:
  142 |       unordered_set() = default;
      |       ^~~~~~~~~~~~~
/usr/include/c++/14/bits/unordered_set.h: At global scope:
/usr/include/c++/14/bits/unordered_set.h:142:7: error: use of deleted function 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Hashtable() [with _Key = std::pair<char, char>; _Value = std::pair<char, char>; _Alloc = std::allocator<std::pair<char, char> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<char, char> >; _Hash = std::hash<std::pair<char, char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]'
In file included from /usr/include/c++/14/bits/unordered_set.h:33:
/usr/include/c++/14/bits/hashtable.h:539:7: note: 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Hashtable() [with _Key = std::pair<char, char>; _Value = std::pair<char, char>; _Alloc = std::allocator<std::pair<char, char> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<char, char> >; _Hash = std::hash<std::pair<char, char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]' is implicitly deleted because the default definition would be ill-formed:
  539 |       _Hashtable() = default;
      |       ^~~~~~~~~~
/usr/include/c++/14/bits/hashtable.h:539:7: error: use of deleted function 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _Traits>::_Hashtable_base() [with _Key = std::pair<char, char>; _Value = std::pair<char, char>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<char, char> >; _Hash = std::hash<std::pair<char, char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, true, true>]'
In file included from /usr/include/c++/14/bits/hashtable.h:35:
/usr/include/c++/14/bits/hashtable_policy.h:1731:7: note: 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _Traits>::_Hashtable_base() [with _Key = std::pair<char, char>; _Value = std::pair<char, char>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<char, char> >; _Hash = std::hash<std::pair<char, char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, true, true>]' is implicitly deleted because the default definition would be ill-formed:
 1731 |       _Hashtable_base() = default;
      |       ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1731:7: error: use of deleted function 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, _Unused, __cache_hash_code>::_Hash_code_base() [with _Key = std::pair<char, char>; _Value = std::pair<char, char>; _ExtractKey = std::__detail::_Identity; _Hash = std::hash<std::pair<char, char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true]'
/usr/include/c++/14/bits/hashtable_policy.h: In instantiation of 'std::__detail::_Hashtable_ebo_helper<_Nm, _Tp, true>::_Hashtable_ebo_helper() [with int _Nm = 1; _Tp = std::hash<std::pair<char, char> >]':
/usr/include/c++/14/bits/hashtable_policy.h:1328:7:   required from here
 1328 |       _Hash_code_base() = default;
      |       ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1245:49: error: use of deleted function 'std::hash<std::pair<char, char> >::hash()'
 1245 |       _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { }
      |                                                 ^~~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/functional_hash.h:102:12: note: 'std::hash<std::pair<char, char> >::hash()' is implicitly deleted because the default definition would be ill-formed:
  102 |     struct hash : __hash_enum<_Tp>
      |            ^~~~
/usr/include/c++/14/bits/functional_hash.h:102:12: error: no matching function for call to 'std::__hash_enum<std::pair<char, char>, false>::__hash_enum()'
/usr/include/c++/14/bits/functional_hash.h:83:7: note: candidate: 'std::__hash_enum<_Tp, <anonymous> >::__hash_enum(std::__hash_enum<_Tp, <anonymous> >&&) [with _Tp = std::pair<char, char>; bool <anonymous> = false]'
   83 |       __hash_enum(__hash_enum&&);
      |       ^~~~~~~~~~~
/usr/include/c++/14/bits/functional_hash.h:83:7: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/14/bits/functional_hash.h:102:12: error: 'std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::pair<char, char>; bool <anonymous> = false]' is private within this context
  102 |     struct hash : __hash_enum<_Tp>
      |            ^~~~
/usr/include/c++/14/bits/functional_hash.h:84:7: note: declared private here
   84 |       ~__hash_enum();
      |       ^
/usr/include/c++/14/bits/hashtable_policy.h:1245:49: note: use '-fdiagnostics-all-candidates' to display considered candidates
 1245 |       _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { }
      |                                                 ^~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1328:7: note: 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, _Unused, __cache_hash_code>::_Hash_code_base() [with _Key = std::pair<char, char>; _Value = std::pair<char, char>; _ExtractKey = std::__detail::_Identity; _Hash = std::hash<std::pair<char, char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true]' is implicitly deleted because the default definition would be ill-formed:
 1328 |       _Hash_code_base() = default;
      |       ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1328:7: error: use of deleted function 'std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<char, char> >, true>::~_Hashtable_ebo_helper()'
/usr/include/c++/14/bits/hashtable_policy.h:1242:12: note: 'std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<char, char> >, true>::~_Hashtable_ebo_helper()' is implicitly deleted because the default definition would be ill-formed:
 1242 |     struct _Hashtable_ebo_helper<_Nm, _Tp, true>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1242:12: error: use of deleted function 'std::hash<std::pair<char, char> >::~hash()'
/usr/include/c++/14/bits/functional_hash.h:102:12: note: 'std::hash<std::pair<char, char> >::~hash()' is implicitly deleted because the default definition would be ill-formed:
  102 |     struct hash : __hash_enum<_Tp>
      |            ^~~~
/usr/include/c++/14/bits/functional_hash.h:102:12: error: 'std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::pair<char, char>; bool <anonymous> = false]' is private within this context
/usr/include/c++/14/bits/functional_hash.h:84:7: note: declared private here
   84 |       ~__hash_enum();
      |       ^
/usr/include/c++/14/bits/hashtable_policy.h:1731:7: note: use '-fdiagnostics-all-candidates' to display considered candidates
 1731 |       _Hashtable_base() = default;
      |       ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1731:7: error: use of deleted function 'std::__detail::_Hash_code_base<std::pair<char, char>, std::pair<char, char>, std::__detail::_Identity, std::hash<std::pair<char, char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()'
/usr/include/c++/14/bits/hashtable_policy.h:1306:12: note: 'std::__detail::_Hash_code_base<std::pair<char, char>, std::pair<char, char>, std::__detail::_Identity, std::hash<std::pair<char, char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()' is implicitly deleted because the default definition would be ill-formed:
 1306 |     struct _Hash_code_base
      |            ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1306:12: error: use of deleted function 'std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<char, char> >, true>::~_Hashtable_ebo_helper()'
/usr/include/c++/14/bits/hashtable.h:539:7: note: use '-fdiagnostics-all-candidates' to display considered candidates
  539 |       _Hashtable() = default;
      |       ^~~~~~~~~~
/usr/include/c++/14/bits/hashtable.h:539:7: error: use of deleted function 'std::__detail::_Hashtable_base<std::pair<char, char>, std::pair<char, char>, std::__detail::_Identity, std::equal_to<std::pair<char, char> >, std::hash<