Language: C++

#include<iostream>
#include<set>
#include<tuple>
#include<vector>

using namespace std;

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

bool neighbor(const 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;
}

int main(){
  for(int w,h;cin>>w>>h,w;){
    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++){
	    set<set<PII> > ss;
	    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(get<2>(cs)).second)continue;
	      //	      cout<<get<2>(cs).size()<<endl;
	      for(auto f:get<2>(cs)){
		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<0>(cs),f)&&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(ns)==0)
		  v.push_back(ns);
		}
	      }
	    }
	    flag|=!v.empty();
	  }
	}
      }
    }
    cout<<(flag?"YES":"NO")<<endl;
  }
}

		  
    

Compiler output:
a.cc: In function 'int main()':
a.cc:69:30: error: no matching function for call to 'std::set<std::set<std::pair<char, char> > >::count(std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >&)'
   69 |                   if(ss.count(ns)==0)
      |                      ~~~~~~~~^~~~
In file included from /usr/include/c++/14/set:63,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_set.h:756:9: note: candidate: 'template<class _Kt> decltype (((const std::set<_Key, _Compare, _Alloc>*)this)->std::set<_Key, _Compare, _Alloc>::_M_t._M_count_tr(__x)) std::set<_Key, _Compare, _Alloc>::count(const _Kt&) const [with _Key = std::set<std::pair<char, char> >; _Compare = std::less<std::set<std::pair<char, char> > >; _Alloc = std::allocator<std::set<std::pair<char, char> > >]'
  756 |         count(const _Kt& __x) const
      |         ^~~~~
/usr/include/c++/14/bits/stl_set.h:756:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_set.h: In substitution of 'template<class _Kt> decltype (((const std::set<std::set<std::pair<char, char> > >*)this)->std::set<std::set<std::pair<char, char> > >::_M_t.std::_Rb_tree<std::set<std::pair<char, char> >, std::set<std::pair<char, char> >, std::_Identity<std::set<std::pair<char, char> > >, std::less<std::set<std::pair<char, char> > >, std::allocator<std::set<std::pair<char, char> > > >::_M_count_tr<_Kt, _Req>(__x)) std::set<std::set<std::pair<char, char> > >::count(const _Kt&) const [with _Kt = std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >]':
a.cc:69:16:   required from here
   69 |                   if(ss.count(ns)==0)
      |                      ~~~~~~~~^~~~
/usr/include/c++/14/bits/stl_set.h:757:37: error: no matching function for call to 'std::_Rb_tree<std::set<std::pair<char, char> >, std::set<std::pair<char, char> >, std::_Identity<std::set<std::pair<char, char> > >, std::less<std::set<std::pair<char, char> > >, std::allocator<std::set<std::pair<char, char> > > >::_M_count_tr(const std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >&) const'
  757 |         -> decltype(_M_t._M_count_tr(__x))
      |                     ~~~~~~~~~~~~~~~~^~~~~
In file included from /usr/include/c++/14/set:62:
/usr/include/c++/14/bits/stl_tree.h:1311:9: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_count_tr(const _Kt&) const [with _Req = _Kt; _Key = std::set<std::pair<char, char> >; _Val = std::set<std::pair<char, char> >; _KeyOfValue = std::_Identity<std::set<std::pair<char, char> > >; _Compare = std::less<std::set<std::pair<char, char> > >; _Alloc = std::allocator<std::set<std::pair<char, char> > >]'
 1311 |         _M_count_tr(const _Kt& __k) const
      |         ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1311:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/string:49,
                 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/stl_function.h: In substitution of 'template<class _Func, class _SfinaeType> using std::__has_is_transparent_t = typename std::__has_is_transparent<_Func, _SfinaeType>::type [with _Func = std::less<std::set<std::pair<char, char> > >; _SfinaeType = std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >]':
/usr/include/c++/14/bits/stl_tree.h:1309:9:   required by substitution of 'template<class _Kt> decltype (((const std::set<std::set<std::pair<char, char> > >*)this)->std::set<std::set<std::pair<char, char> > >::_M_t.std::_Rb_tree<std::set<std::pair<char, char> >, std::set<std::pair<char, char> >, std::_Identity<std::set<std::pair<char, char> > >, std::less<std::set<std::pair<char, char> > >, std::allocator<std::set<std::pair<char, char> > > >::_M_count_tr<_Kt, _Req>(__x)) std::set<std::set<std::pair<char, char> > >::count(const _Kt&) const [with _Kt = std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >]'
 1309 |                typename _Req = __has_is_transparent_t<_Compare, _Kt>>
      |                ^~~~~~~~
a.cc:69:16:   required from here
   69 |                   if(ss.count(ns)==0)
      |                      ~~~~~~~~^~~~
/usr/include/c++/14/bits/stl_function.h:1427:11: error: no type named 'type' in 'struct std::__has_is_transparent<std::less<std::set<std::pair<char, char> > >, std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >, void>'
 1427 |     using __has_is_transparent_t
      |           ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:750:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::count(const key_type&) const [with _Key = std::set<std::pair<char, char> >; _Compare = std::less<std::set<std::pair<char, char> > >; _Alloc = std::allocator<std::set<std::pair<char, char> > >; size_type = long unsigned int; key_type = std::set<std::pair<char, char> >]'
  750 |       count(const key_type& __x) const
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:750:29: note:   no known conversion for argument 1 from 'std::tuple<std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > >, std::set<std::pair<char, char>, std::less<std::pair<char, char> >, std::allocator<std::pair<char, char> > > >' to 'const std::set<std::set<std::pair<char, char> > >::key_type&' {aka 'const std::set<std::pair<char, char> >&'}
  750 |       count(const key_type& __x) const
      |             ~~~~~~~~~~~~~~~~^~~