Problem:
Alternate Escape
アリスハウス
AliceとBobはボードゲームで遊んでいる．
このボードゲームは，
H
行
W
列のマス目が書かれた盤面と1つのコマを使って遊ぶ．
このゲームでは，盤面の左上のマスを1行1列目として，下方向に行を，右方向に列を数える．
マス同士が隣接する辺と，マスが盤面の外側と接する辺には壁を置けるようになっていて，ゲームの開始時にはそれぞれの辺について壁の有無が指定されている．また，ゲームの開始時には，コマが盤面のマスのいずれか1箇所に置かれている．
AliceとBobは交互に手番をこなすことでゲームを進める．
ゲームはAliceの手番から始まる．
Aliceの目的は，コマを盤面の外まで動かして，迷路から脱出させることである．
Aliceが1手でできる行動は，コマを今ある位置のマスから，上下左右に隣接するマスのうち，間の辺に壁がない方向のいずれかに移動させることである．
コマの今あるマスが盤面の外側に接していて，間の辺に壁がない場合，そこからコマを脱出させることができる．
一方，Bobの目的は，コマの脱出を妨害することである．
Bobの手番では，壁の有無を反転させるか，何もせずに手番を終えるかを選ぶことができる．
壁の有無を反転させることを選んだ場合，盤面のすべてのマスの辺について，壁の有無が反転する．
盤面の初期状態と，コマの初期位置が与えられるので，AliceとBobの両者が最適な行動をとったときに，Aliceがコマを盤面から脱出させられるか判定せよ．
ただし，Aliceの手番においてコマが4方向とも壁で囲まれてしまった場合は脱出できないとみなす．
Input
入力は40個以下のデータセットからなる．
それぞれのデータセットは次の形式で与えられる．
H
W
R
C
Horz
1,1
Horz
1,2
...
Horz
1,
W
Vert
1,1
Vert
1,2
...
Vert
1,
W
+1
...
Vert
H
,1
Vert
H
,2
...
Vert
H
,
W
+1
Horz
H
+1,1
Horz
H
+1,2
...
Horz
H
+1,
W
1行目には4つの整数
H
,
W
(1 ≤
H
,
W
≤ 500),
R
,
C
(1 ≤
R
≤
H
, 1 ≤
C
≤
W
) が与えられる．
これらは盤面が
H
行
W
列のマス目からなり，コマの初期位置が
R
行
C
列目であることを表す．
続く 2
H
+ 1 行には盤面の初期状態が与えられる．
2
i
行目 (1 ≤
i
≤
H
+ 1) は，
W
個の整数
Horz
i
,1
,
Horz
i
,2
, ...,
Horz
i
,
W
を含む．
Horz
i
,
j
は，
i
行
j
列目のマスの上側の辺に壁が有るとき1で，無いとき0である．
ただし，
Horz
H
+1,
j
は，
H
行
j
列目のマスの下側の辺における壁の有無を表す．
2
i
+ 1 行目 (1 ≤
i
≤
H
) は，
W
+ 1 個の整数
Vert
i
,1
,
Vert
i
,2
, ...,
Vert
i
,
W
+1
を含む．
Vert
i
,
j
は，
i
行
j
列目のマスの左側の辺に壁が有るとき1で，無いとき0である．
ただし，
Vert
i
,
W
+1
は，
i
行
W
列目のマスの右側の辺における壁の有無を表す．
入力の終わりは，4つのゼロからなる1行で示される．
Output
それぞれのデータセットについて，Aliceがコマを盤面から脱出させられる場合は"
Yes
"，できない場合は"
No
"と1行に出力せよ．
Sample Input
3 3 2 2
 1 1 1
0 0 0 0
 1 1 1
0 0 0 0
 1 1 1
0 0 0 0
 1 1 1
3 3 2 2
 1 0 1
1 0 1 1
 1 0 0
0 0 0 0
 0 0 1
1 1 0 1
 1 0 1
1 3 1 1
 1 1 1
1 0 0 1
 1 0 1
2 2 1 1
 1 0
1 0 0
 0 0
0 0 0
 0 0
0 0 0 0
Output for Sample Input
Yes
No
Yes
No
Hint
1つ目のデータセットではAliceは次のように動くことでコマを脱出させられる。
初期状態
Aliceがコマを左に動かす
Bobは脱出を阻止するために壁を反転させる
Aliceがコマを上に動かす
Bobが次の手番で壁の有無を反転させてもさせなくても、
Aliceは次の手番でコマを脱出させられる。

Submitted solutions:

code_snippet: #include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>

using namespace std;

int n, m, row, col;

const int N = 600;

int ver[N][N];
int hor[N][N];
int visit[N][N][2][2];
int deg[N][N][2][2];

struct Node {
	int r, c, who, rev;
	Node(int r, int c, int who, int rev) : r(r), c(c), who(who), rev(rev) {}
	Node() {}
};

vector<Node> q;
		
void push(Node t) {
	if (t.r < 0 || t.r >= n || t.c < 0 || t.c >= m) return ;
	if (visit[t.r][t.c][t.who][t.rev]) return ;
	visit[t.r][t.c][t.who][t.rev] = 1;
	q.push_back(t);
}

int main() {
	while (scanf("%d %d %d %d", &n, &m, &row, &col) == 4 && (n || m || row || col)) {
		for (int i = 0; i < n * 2 + 1; i++) {
			if (i % 2 == 0) {
				for (int j = 0; j < m; j++) scanf("%d", &ver[i >> 1][j]), ver[i >> 1][j] ^= 1;
			} else {
				for (int j = 0; j < m + 1; j++) scanf("%d", &hor[i >> 1][j]), hor[i >> 1][j] ^= 1;
			}
		}
		row--; col--;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				for (int who = 0; who < 2; who++) {
					for (int rev = 0; rev < 2; rev++) {
						visit[i][j][who][rev] = deg[i][j][who][rev] = 0;
					}
				}
			}
		}
		q.clear();
		for (int r = 0; r < n; r++) {
			for (int c = 0; c < m; c++) {
				for (int rev = 0; rev < 2; rev++) {
					bool up = ver[r][c] ^ rev;
					bool down = ver[r + 1][c] ^ rev;
					bool left = hor[r][c] ^ rev;
					bool right = hor[r][c + 1] ^ rev;
					if (
					(r == 0 && up)
					|| (r == n - 1 && down)
					|| (c == 0 && left)
					|| (c == m - 1 && right)) {
						q.push_back(Node(r, c, 0, rev));
						visit[r][c][0][rev] = 1;
					}
				}
			}
		}
		
		bool flag = false;
		for (int head = 0; head < q.size(); head++) {
			Node t = q[head];
			//printf("r = %d c = %d who = %d rev = %d\n", t.r, t.c, t.who, t.rev);
			if (t.r == row && t.c == col && t.who == 0 && t.rev == 0) {
				puts("Yes");
				flag = true;
				break;
			}
			if (t.who == 0) {
				t.who = 1;
				for (t.rev = 0; t.rev < 2; t.rev++) {
					if (++deg[t.r][t.c][t.who][t.rev] == 2) {
						q.push_back(t);
					}
				}
			} else {
				bool up = ver[t.r][t.c] ^ t.rev;
				bool down = ver[t.r + 1][t.c] ^ t.rev;
				bool left = hor[t.r][t.c] ^ t.rev;
				bool right = hor[t.r][t.c + 1] ^ t.rev;
				if (up) push(Node(t.r - 1, t.c, 0, t.rev));
				if (down) push(Node(t.r + 1, t.c, 0, t.rev));
				if (left) push(Node(t.r, t.c - 1, 0, t.rev));
				if (right) push(Node(t.r, t.c + 1, 0, t.rev));
			}
		}
		
		if (!flag) puts("No");
	}
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 34924
final_rank: 16

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var) do { cout << #var << " :\n"; view(var); } while(0)
template<typename T>void view(const T& e) {cout << e;}
template<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << "{" << p.first << ", " << p.second << "}";}
template<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << " ";} cout << endl;}
template<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << " ";} cout << endl;}

int H, W, r, c;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void solve(){
    vvi horz(H + 1, vi(W));
    vvi vert(H, vi(W + 1));
    rep(i, H){
        rep(j, W) cin >> horz[i][j];
        rep(j, W + 1) cin >> vert[i][j];
    }
    rep(j, W) cin >> horz[H][j];

    vvi A(H + 2, vi(W + 2, 0));
    rep(i, H + 2){
        A[i][0] = 1;
        A[i][W + 1] = 1;
    }
    rep(j, W + 2){
        A[0][j] = 1;
        A[H + 1][j] = 1;
    }
    auto ok = [&](int i, int j){
        bool a = false, b = false;
        if (!horz[i - 1][j - 1] && A[i - 1][j]) a = true;
        if (!horz[i][j - 1] && A[i + 1][j]) a = true;
        if (!vert[i - 1][j - 1] && A[i][j - 1]) a = true;
        if (!vert[i - 1][j] && A[i][j + 1]) a = true;

        if (horz[i - 1][j - 1] && A[i - 1][j]) b = true;
        if (horz[i][j - 1] && A[i + 1][j]) b = true;
        if (vert[i - 1][j - 1] && A[i][j - 1]) b = true;
        if (vert[i - 1][j] && A[i][j + 1]) b = true;
        return a && b;
    };
    auto ok_ = [&](int i, int j){
        bool a = false;
        if (!horz[i - 1][j - 1] && A[i - 1][j]) a = true;
        if (!horz[i][j - 1] && A[i + 1][j]) a = true;
        if (!vert[i - 1][j - 1] && A[i][j - 1]) a = true;
        if (!vert[i - 1][j] && A[i][j + 1]) a = true;
        return a;
    };
    deque<pair<int, int>> q;
    for (auto i : {1, H}) srep(j, 1, W + 1) if (!A[i][j] && ok(i, j)){
        q.push_back({i, j});
        A[i][j] = 1;
    }
    srep(i, 1, H + 1) for (auto j : {1, W}) if (!A[i][j] && ok(i, j)){
        q.push_back({i, j});
        A[i][j] = 1;
    }
    while (!q.empty()){
        auto [i, j] = q.front(); q.pop_front();
        rep(k, 4){
            int ni = i + dx[k], nj = j + dy[k];
            if (!A[ni][nj] && ok(ni, nj)){
                A[ni][nj] = 1;
                q.push_back({ni, nj});
            }
        }
    }
    cout << (ok_(r, c) ? "Yes" : "No") << endl;
}

int main(){
    while (true){
        cin >> H >> W >> r >> c;
        if (H == 0) break;
        solve();
    }
}
accuracy: 1
time_ms: 130
memory_kb: 6412
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
ll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}
ll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}

ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;

vector<ll> _ta = {0,1,0,-1,1,1,-1,-1};
vector<ll> _yo = {1,0,-1,0,1,-1,1,-1};


// for AOJ or ICPC or etc..
template<class Tp>
bool zero (const Tp &x) {
    return x == 0;
}

template<class Tp, class... Args>
bool zero (const Tp &x, const Args& ...args) {
    return zero(x) and zero(args...);
}


void solve(ll h,ll w,ll r,ll c){
  r--;c--;
  vector<vvll> g(h,vvll(w,vll(4)));
  {
    vll yoko(w);
    cin >> yoko;
    rep(j,w){
      g[0][j][3] = yoko[j];
    }
  }
  rep(i,h){
    vll tate(w+1);
    cin>> tate;
    vll yoko(w);
    cin >> yoko;
    rep(j,w){
      g[i][j][2] = tate[j];
      g[i][j][0] = tate[j+1];
      g[i][j][1] =  yoko[j];
      if(i < h-1){
        g[i+1][j][3] = yoko[j];
      }
    }
  }
  vvll find(h,vll(w,INF));
  queue<pll> que;
  rep(i,h)rep(j,w){
     vll bi;
    rep(k,4){
      ll nny = i + _ta[k];
      ll nnx = j + _yo[k];
      if(!isin(nny,nnx,h,w) or find[nny][nnx] < INF){
        bi.push_back(g[i][j][k]);
      }
    }
    if(!bi.empty() &&  *min_element(all(bi)) == 0 && *max_element(all(bi)) == 1){
      find[i][j] = 0;
      que.push({i,j});
    }
  }
  while(!que.empty()){
    auto[nowy,nowx] = que.front();
    que.pop();
    rep(p,4){
      ll ny = nowy + _ta[p];
      ll nx = nowx + _yo[p];
      if(isin(ny,nx,h,w) && find[ny][nx] ==  INF){
        vll bi;
        rep(k,4){
          ll nny = ny + _ta[k];
          ll nnx = nx + _yo[k];
          if(!isin(nny,nnx,h,w) or find[nny][nnx] < INF){
            bi.push_back(g[ny][nx][k]);
          }
        }
        if(*min_element(all(bi)) == 0 && *max_element(all(bi)) == 1){
          find[ny][nx] = find[nowy][nowx] + 1;
          que.push({ny,nx});
        }
      }
    }
  }
  //そもそも1手で出れる
  if(r== 0 && g[r][c][3] == 0){
    print("Yes");return ;
  }
  if(r==h-1&&g[r][c][1]==0){
    print("Yes");return ;
  }
  if(c==0&&g[r][c][2]==0){
    print("Yes");return ;
  }
  if(c==w-1&&g[r][c][0]==0){
    print("Yes");return ;
  }
  Yes(find[r][c]  < INF);
}

int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  while(true){
    LL(h,w,r,c);
    if(zero(h,w,r,c))break;
    solve(h,w,r,c);
  }
}
accuracy: 1
time_ms: 110
memory_kb: 23108
final_rank: 13

code_snippet: #ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
// #include <boost/rational.hpp>
// using namespace boost;
// using rat = rational<long long int>;
using mint = modint998244353;
// using mint = modint1000000007;
// using mint = mint;
using ll = long long;
using ld = long double;
using ull = uint64_t;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vm = vector<mint>;
using vvm = vector<vm>;
using vvvm = vector<vvm>;
using vstr = vector<string>;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
#define vvv(T) vector<vector<vector<T>>>
#define vvvv(T) vector<vector<vector<vector<T>>>>

istream &operator>>(istream &is, mint &a){ll tmp; is >> tmp; a = tmp; return is;}
ostream &operator<<(ostream &os, const mint &a){ os << a.val(); return os; }
string to_string(const __int128_t &a) { if (a == 0) return "0"; string s = ""; __int128_t num = a; bool is_negative = false; if (num < 0) { is_negative = true; num = -num; } while (num > 0) { s += '0' + (num % 10); num /= 10; } if (is_negative) s += '-'; reverse(s.begin(), s.end()); return s; }
istream &operator>>(istream &is, __int128_t &a){ string s; is >> s; a = 0; for(char c : s) { if(isdigit(c)) {a = a*10 + (c - '0'); }  } if(s[0]=='-'){ a *= -1; } return is; }
ostream &operator<<(ostream &os, const __int128_t &a){ os << to_string(a); return os; }
template<class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<class T> istream &operator>>(istream &is, vector<T> &vec){ for(T &e : vec){is >> e;} return is; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for(int i = 0; i < (int)vec.size(); i++) { os << vec[i] << (i + 1 != (int)vec.size() ? " " : ""); } return os; }

template <class... T> constexpr auto min (T... a) { return min(initializer_list<common_type_t<T...>>{a...}); }
template <class... T> constexpr auto max (T... a) { return max(initializer_list<common_type_t<T...>>{a...}); }
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template<class T> T opmin(T x, T y) { return min(x, y); }
template<class T> T einf() { return numeric_limits<T>::max(); }
template<class T> T opmax(T x, T y) { return max(x, y); }
template<class T> T eminf() { return numeric_limits<T>::min(); }
template<class T> T opsum(T x, T y) { return x + y; }
template<class T> T ezero() { return (T)0; }
// #define maxseg(T) segtree<T, [](T x, T y){return max(x, y);}, [](){return (T)(-(1LL << 60));}>
// #define minseg(T) segtree<T, [](T x, T y){return min(x, y);}, [](){return (T)((1LL << 60));}>
// #define sumseg(T) segtree<T, [](T x, T y){return x + y;}, [](){return (T)(0);}>
template<class T> using minseg = segtree<T, opmin<T>, einf<T>>;
template<class T> using maxseg = segtree<T, opmax<T>, eminf<T>>;
template<class T> using sumseg = segtree<T, opsum<T>, ezero<T>>;
// template<class T> struct v : vector<T> { using vector<T> :: vector; };
// template<class T> struct vv : vector<v<T>> { using vector<v<T>> :: vector; };
// template<class T> struct vvv : vector<vv<T>> { using vector<vv<T>> :: vector; };
template<class T> inline bool chmin(T& a, T b) {if(a > b){a = b; return true;} else {return false;}};
template<class T> inline bool chmax(T& a, T b) {if(a < b){a = b; return true;} else {return false;}};
#define rep(i,n) for(ll i = 0; i < (ll)(n); i++)
#define repr(i,n) for(ll i = (ll)(n) - 1; i >= 0; i--)
#define REP(i, l, r) for(ll i = (ll)l; i <= (ll)(r); i++)
#define REPR(i, l, r) for(ll i = (ll)r; i >= (ll)(l); i--)
const ll inf = (1 << 30);
const ll INF = (1LL << 60);
const vector<pair<ll, ll>> DIJ = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
void out(){cout<<'\n';}
template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout<<a; (cout<<... << (cout << ' ', b)); cout << '\n';}
void outf(){cout<<endl;}
template<class T, class... Ts> void outf(const T& a, const Ts&... b){ cout<<a; (cout<<... << (cout << ' ', b)); cout << endl;}
template<class T, class U> void outp(pair<T, U> a){ out((a).first, (a).second); }
template<class T, class U> void outpf(pair<T, U> a){ outf((a).first, (a).second); }
template<class T> void outv(T a){rep(i, (a).size()){ cout << (a)[i] << " "; } cout << endl;}
template<class T> void outvL(T a){rep(i, (a).size()){out((a)[i]);} cout << flush; }
// template<class T> void outvv(T a){rep(i, a.size()){ rep(j, a.at(i).size()){cout << a.at(i).at(j) << " "; } cout << endl; }}
// template<class T> void outvp(T a){rep(i, a.size()){ out2(a.at(i).first, a.at(i).second); }}
void setpre(int a){cout << fixed << setprecision(a);}
#define outN out("No")
#define outY out("Yes")
#define outYN(flag) out(flag ? "Yes" : "No")
#define dame(...) {outf(__VA_ARGS__);return 0;}

template<class T> void read(vector<T>& vec){ for(int i = 0; i < (int)vec.size(); i++) { cin >> vec[i]; } }
template<class... T> void read(T&... a){(cin >> ... >> a);}
#define readll(...) ll __VA_ARGS__; read(__VA_ARGS__)
#define readvll(a, n) vector<ll> a(n); read(a)
#define readvt(type, a, n) vector<type> a(n); read(a)
#define readvll2(a, b, n) vector<ll> a(n), b(n); for(int lopi = 0; lopi < (int)(n); lopi++) cin >> (a)[lopi] >> (b)[lopi]
#define readvll3(a, b, c, n) vector<ll> a(n), b(n), c(n); for(int lopi = 0; lopi < (int)(n); lopi++) cin >> (a)[lopi] >> (b)[lopi] >> (c)[lopi]
#define readstr(...) string __VA_ARGS__; read(__VA_ARGS__)
#define readundirG(G, N, M) G = vvll(N); rep(lopi, M) {ll a, b; cin >> a >> b; G[a-1].push_back(b-1); G[b-1].push_back(a-1);}
#define readdirG(G, N, M) G = vvll(N); rep(lopi, M) {ll a, b; cin >> a >> b; G[a-1].push_back(b-1);}
#define readundirwghG(G, N, M) G = vv(pll)(N); rep(lopi, M) {ll a, b, c; cin >> a >> b >> c; G[a-1].emplace_back(b-1,c); G[b-1].emplace_back(a-1, c);}
#define readdirwghG (G, N, M) G = vv(pll)(N); rep(lopi, M) {ll a, b, c; cin >> a >> b >> c; G[a-1].emplace_back(b-1, c);}

#define All(a) (a).begin(), (a).end()
template<class T> inline void sortr(T& a){ sort((a).rbegin(), (a).rend()); }
template<class T> inline vector<int> argsort(T V, bool rev = false){vector<int> res(V.size()); iota(res.begin(), res.end(), 0); sort(res.begin(), res.end(), [&](int x, int y){if(!rev){return V[x] < V[y];}else{return V[x] > V[y];}}); return res;}
template<class T, class U> inline void sort_by_idx(T& V, vector<U>& I){assert(V.size() == I.size()); T tmpv = V; for(int loopi = 0; loopi < (int)I.size(); loopi++){V[loopi] = tmpv[I.at(loopi)];}}
template<class T, class U> inline void sortp(vector<T>& v1, vector<U>& v2, bool rev1 = false, int rev2 = false){assert(v1.size() == v2.size()); vector<int> I(v1.size()); iota(I.begin(), I.end(), 0); sort(I.begin(), I.end(), [&](const int x, const int y){if(v1[x] != v1[y]){return (bool)(rev1 ^ (v1[x] < v1[y]));}else{if(v2[x]==v2[y]){return false;} return (bool)(rev2 ^ (v2[x] < v2[y]));}}); sort_by_idx(v1, I); sort_by_idx(v2, I);}
template<class T> T POW(T x, ll n) {T ret = 1; while(n > 0){if(n & 1) ret *= x; x *= x; n >>= 1;} return ret;}
ll powll(ll x, ll n){ll ret = 1; while(n > 0){if(n & 1) ret *= x; x *= x; n >>= 1;} return ret;}
inline ll divceil(ll x, ll y) { if(x >= 0) {return(x / y + (ll)(x % y != 0)); } else { return -((-x) / y); } }
inline ll divfloor(ll x, ll y) { if(x >= 0) { return x/y; } else { return -((-x)/y + (ll)((-x) % y != 0)); } }
inline bool inLR(ll x, ll L, ll R){ return (L <= x && x < R); }
inline bool inRect(ll pos_x, ll pos_y, ll rect_H, ll rect_W, ll rect_h = 0, ll rect_w = 0){ return (rect_h <= pos_x && pos_x < rect_H && rect_w <= pos_y && pos_y < rect_W); }

template<class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v){e++;} return v;}
template<class T> vector<T> operator++(vector<T> &v, signed) {auto res=v; for(auto &e : v){e++;} return res;}
template<class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v){e--;} return v;}
template<class T> vector<T> operator--(vector<T> &v, signed) {auto res=v; for(auto &e : v){e--;} return res;}
template<class T> vector<T> operator+(const vector<T> &x, const vector<T> &y) { assert(x.size() == y.size()); vector<T> ret(x.size()); for(int i = 0; i < (int)x.size(); i++) {ret[i] = x[i] + y[i];} return ret; }
template<class T> vector<T> operator-(const vector<T> &x, const vector<T> &y) { assert(x.size() == y.size()); vector<T> ret(x.size()); for(int i = 0; i < (int)x.size(); i++) {ret[i] = x[i] - y[i];} return ret; } 

template<class T, class U> pair<T, U> operator+(const pair<T, U> &x, const pair<T, U> &y) { return make_pair(x.first + y.first, x.second + y.second); }
template<class T, class U> pair<T, U> operator-(const pair<T, U> &x, const pair<T, U> &y) { return make_pair(x.first - y.first, x.second - y.second); }
template<class T, class U> void operator+=(pair<T, U> &x, pair<T, U> &y) { x = x + y; }
template<class T, class U> void operator-=(pair<T, U> &x, pair<T, U> &y) { x = x - y; }

template<class T> size_t HashCombine(const size_t seed,const T &v){ return seed^(std::hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2)); }
template<class T,class S> struct std::hash<std::pair<T,S>>{ size_t operator()(const std::pair<T,S> &keyval) const noexcept { return HashCombine(std::hash<T>()(keyval.first), keyval.second); } };
template<class T> struct std::hash<std::vector<T>>{ size_t operator()(const std::vector<T> &keyval) const noexcept { size_t s=0; for (auto&& v: keyval) s=HashCombine(s,v); return s; } };
template<int N> struct HashTupleCore{ template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ size_t s=HashTupleCore<N-1>()(keyval); return HashCombine(s,std::get<N-1>(keyval)); } };
template <> struct HashTupleCore<0>{ template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ return 0; } };
template<class... Args> struct std::hash<std::tuple<Args...>>{ size_t operator()(const tuple<Args...> &keyval) const noexcept { return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval); } };

int main()
{
    std::cin.tie(nullptr), std::ios_base::sync_with_stdio(false);
    while(true)
    {
        readll(H, W, R, C);
        if(!H) break;
        R--; C--;
        vvll G0(H*W + 1), G1(H*W + 1);
        rep(i, 2*H + 1)
        {
            if(i % 2 == 0)
            {
                readvll(X, W);
                rep(j, W)
                {
                    ll a, b;
                    if(i == 0)
                    {
                        a = j;
                        b = H*W;
                    }
                    else if(i == 2*H)
                    {
                        a = (H - 1)*W + j;
                        b = H*W;
                    }
                    else
                    {
                        a = (i/2 - 1)*W + j;
                        b = (i/2)*W + j;
                    }
                    if(!X[j])
                    {
                        G0[a].emplace_back(b);
                        G0[b].emplace_back(a);
                    }
                    else
                    {
                        G1[a].emplace_back(b);
                        G1[b].emplace_back(a);
                    }
                }
            }
            else
            {
                readvll(X, W + 1);
                rep(j, W + 1)
                {
                    ll a, b;
                    if(j == 0)
                    {
                        a = H*W;
                        b = ((i-1)/2)*W + j;
                    }
                    else if(j == W)
                    {
                        a = ((i-1)/2)*W + j - 1;
                        b = H*W;
                    }
                    else
                    {
                        a = ((i-1)/2)*W + j - 1;
                        b = ((i-1)/2)*W + j;
                    }
                    if(!X[j])
                    {
                        G0[a].emplace_back(b);
                        G0[b].emplace_back(a);
                    }
                    else
                    {
                        G1[a].emplace_back(b);
                        G1[b].emplace_back(a);
                    }
                }
            }
        }
        rep(i, H*W + 1)
        {
            sort(All(G0[i]));
            G0[i].erase(unique(All(G0[i])), G0[i].end());
        }
        rep(i, H*W + 1)
        {
            sort(All(G1[i]));
            G1[i].erase(unique(All(G1[i])), G1[i].end());
        }
        queue<ll> q;
        q.emplace(H*W);
        v(bool) is_win(H*W + 1, false);
        is_win[H*W] = true;
        while(!q.empty())
        {
            auto now = q.front(); q.pop();
            vll cand;
            for(auto e : G0[now]) if(!is_win[e]) cand.emplace_back(e);
            for(auto e : G1[now]) if(!is_win[e]) cand.emplace_back(e);
            sort(All(cand));
            cand.erase(unique(All(cand)), cand.end());
            for(auto nxt : cand)
            {
                bool ok0 = false, ok1 = false;
                for(auto e : G0[nxt])
                {
                    if(is_win[e])
                    {
                        ok0 = true;
                        break;
                    }
                }
                if(!ok0) continue;
                for(auto e : G1[nxt])
                {
                    if(is_win[e])
                    {
                        ok1 = true;
                        break;
                    }
                }
                if(!ok1) continue;
                is_win[nxt] = true;
                q.emplace(nxt);
            }
        }
        bool ok = is_win[R*W + C];
        for(auto e : G0[R*W + C])
        {
            if(is_win[e]) ok = true;
        }
        outYN(ok);
    }
}
accuracy: 1
time_ms: 2640
memory_kb: 48272
final_rank: 20

code_snippet: # include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double pi = acos(-1);
template<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T hinf() { return inf<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }
template<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }
template<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };
ll MOD(ll x, ll m){return (x%m+m)%m; }
ll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }
template<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;
# define all(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(all((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(all((rprpr)),(rprpr).begin(),TU<char>)
# define rep(i,upupu)         for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)
# define reps(i,opopo)        for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)
# define len(x)                ((ll)(x).size())
# define bit(n)               (1LL << (n))
# define pb push_back
# define eb emplace_back
# define exists(c, e)         ((c).find(e) != (c).end())

struct INIT{
	INIT(){
		std::ios::sync_with_stdio(false);
		std::cin.tie(0);
		cout << fixed << setprecision(20);
	}
}INIT;

namespace mmrz {
	void solve();
}

int main(){
	mmrz::solve();
}
#define debug(...) (static_cast<void>(0))

using namespace mmrz;

using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using ull = unsigned long long;
using lll = __int128;
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

// [BEGIN] template の include

vll dx={1,0,-1,0},dy={0,-1,0,1};
int SOLVE(){
	ll H,W,si,sj;cin>>H>>W>>si>>sj;
	si--,sj--;
	if(H==0&&W==0) return 1;
	vector<vvll> can(H,vvll(W,vll(4)));
	rep(s,2*H+1){
		if(s%2==0&&s<2*H){
			ll i=s/2;
			rep(j,W){
				ll f;cin>>f;
				if(f){
					can[i][j][2]=1;
					if(i>0) can[i-1][j][0]=1;	
				}
			}
		}else if(s%2==1){
			ll i=s/2;
			vll A(W+1);rep(i,W+1) cin>>A[i];
			rep(j,W){
				if(A[j]==1) can[i][j][1]=1;
				if(A[j+1]==1) can[i][j][3]=1;
			}
		}else{
			rep(j,W){
				ll f;cin>>f;
				if(f) can[H-1][j][0]=1;
			}
		}
	}



	vector<vll> dp0(H,vll(W));
	vvll dp1=dp0;
	rep(i,H) rep(j,W){ 
		if(i==si&&sj==j) continue;
		ll a1=0,a2=0;
		rep(k,4){
			ull toi=i+dx[k],toj=j+dy[k];
			if(!(toi<H&&toj<W)){
				if(can[i][j][k]==0) dp0[i][j]=1;
				else dp1[i][j]=1;
			}
		}
	}
	queue<pair<ll,ll>>q;
	rep(i,H) rep(j,W) if(dp1[i][j]==1&&dp0[i][j]) q.push({i,j});
	while(q.size()){
		auto [nowi,nowj]=q.front();q.pop();
		if(dp1[nowi][nowj]==1&&dp0[nowi][nowj]==1){
			rep(k,4){
				ull toi=nowi+dx[k],toj=nowj+dy[k];
				if(!(toi<H&&toj<W)) continue;
				ll do2=0;
				if(can[nowi][nowj][k]==0) do2=!dp0[toi][toj], dp0[toi][toj]=1;
				else do2=!dp1[toi][toj],dp1[toi][toj]=1;
				if(do2&&dp0[toi][toj]==1&&dp1[toi][toj]==1) q.push({toi,toj});
			}
		}
	}
	bool ans=0;
	rep(k,4){
		ull toi=si+dx[k],toj=sj+dy[k];
		if(!(toi<H&&toj<W)){if(can[si][sj][k]==0)ans=1;continue;}
		if(can[si][sj][k]==0&&(dp0[toi][toj]==1&&dp1[toi][toj]==1)) ans=1;
	}
	cout<<(ans?"Yes":"No")<<endl;
	return 0;

	
}

void mmrz::solve(){
	while(!SOLVE());
}
accuracy: 1
time_ms: 130
memory_kb: 24824
final_rank: 15

code_snippet: # include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double pi = acos(-1);
template<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T hinf() { return inf<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }
template<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }
template<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };
ll MOD(ll x, ll m){return (x%m+m)%m; }
ll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }
template<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;
# define all(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(all((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(all((rprpr)),(rprpr).begin(),TU<char>)
# define rep(i,upupu)         for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)
# define reps(i,opopo)        for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)
# define len(x)                ((ll)(x).size())
# define bit(n)               (1LL << (n))
# define pb push_back
# define eb emplace_back
# define exists(c, e)         ((c).find(e) != (c).end())

struct INIT{
	INIT(){
		std::ios::sync_with_stdio(false);
		std::cin.tie(0);
		cout << fixed << setprecision(20);
	}
}INIT;

namespace mmrz {
	void solve();
}

int main(){
	mmrz::solve();
}
#define debug(...) (static_cast<void>(0))

using namespace mmrz;

using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using ull = unsigned long long;
using lll = __int128;
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

// [BEGIN] template の include

vll dx={1,0,-1,0},dy={0,-1,0,1};
int SOLVE(){
	ll H,W,si,sj;cin>>H>>W>>si>>sj;
	si--,sj--;
	if(H==0&&W==0) return 1;
	vector<vvll> can(H,vvll(W,vll(4)));
	rep(s,2*H+1){
		if(s%2==0&&s<2*H){
			ll i=s/2;
			rep(j,W){
				ll f;cin>>f;
				if(f){
					can[i][j][2]=1;
					if(i>0) can[i-1][j][0]=1;	
				}
			}
		}else if(s%2==1){
			ll i=s/2;
			vll A(W+1);rep(i,W+1) cin>>A[i];
			rep(j,W){
				if(A[j]==1) can[i][j][1]=1;
				if(A[j+1]==1) can[i][j][3]=1;
			}
		}else{
			rep(j,W){
				ll f;cin>>f;
				if(f) can[H-1][j][0]=1;
			}
		}
	}



	vector<vll> dp0(H,vll(W));
	vvll dp1=dp0;
	rep(i,H) rep(j,W){ 
		if(i==si&&sj==j) continue;
		ll a1=0,a2=0;
		rep(k,4){
			ull toi=i+dx[k],toj=j+dy[k];
			if(!(toi<H&&toj<W)){
				if(can[i][j][k]==0) dp0[i][j]=1;
				else dp1[i][j]=1;
			}
		}
	}
	queue<pair<ll,ll>>q;
	rep(i,H) rep(j,W) if(dp1[i][j]==1&&dp0[i][j]) q.push({i,j});
	while(q.size()){
		auto [nowi,nowj]=q.front();q.pop();
		if(dp1[nowi][nowj]==1&&dp0[nowi][nowj]==1){
			rep(k,4){
				ull toi=nowi+dx[k],toj=nowj+dy[k];
				if(!(toi<H&&toj<W)) continue;
				ll do2=0;
				if(can[nowi][nowj][k]==0) do2=!dp0[toi][toj], dp0[toi][toj]=1;
				else do2=!dp1[toi][toj],dp1[toi][toj]=1;
				if(do2&&dp0[toi][toj]==1&&dp1[toi][toj]==1) q.push({toi,toj});
			}
		}
	}
	bool ans=0;
	rep(k,4){
		ull toi=si+dx[k],toj=sj+dy[k];
		if(!(toi<H&&toj<W)){if(can[si][sj][k]==0)ans=1;continue;}
		if(can[si][sj][k]==0&&(dp0[toi][toj]==1&&dp1[toi][toj]==1)) ans=1;
	}
	cout<<(ans?"Yes":"No")<<endl;
	return 0;

	
}

void mmrz::solve(){
	while(!SOLVE());
}
accuracy: 1
time_ms: 90
memory_kb: 25040
final_rank: 14

code_snippet: // AOJ 2702 Alternate Escape
// 2025.5.16

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int c, n = 0;
    do c = gc(); while (c < '0');
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Couts(string s) {
    for (auto c: s) pc(c);
    pc('\n');
}

int main(){
    while (true) {
        int H = Cin(), W = Cin(), R = Cin(), C = Cin();
        if (H==0) break;

        vector horz(H+2, vector(W+2, array<int,2>{}));
        vector vert(H+2, vector(W+2, array<int,2>{}));

        for (int i = 1; i <= H+1; i++) {
            for (int j = 1; j <= W; j++) {
                horz[i][j][0] = Cin();
            }
            if (i == H+1) break;
            for (int j = 1; j <= W+1; j++) {
                vert[i][j][0] = Cin();
            }
        }
        for (int i = 1; i <= H+1; i++) {
            for (int j = 1; j <= W; j++) horz[i][j][1] = 1 - horz[i][j][0];
        }
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W+1; j++) vert[i][j][1] = 1 - vert[i][j][0];
        }

        int N = H * W * 2;
        vector<char> inA_A(N, 0), inA_B(N, 0);
        vector<int> cnt_B(N, 2);

        auto encode = [&](int r, int c, int f){
            return (((r-1)*W + (c-1))<<1) | f;
        };

        deque<pair<int,int>> q;

        for (int f = 0; f < 2; f++) {
            for (int i = 1; i <= H; i++) {
                for (int j = 1; j <= W; j++) {
                    bool canExit = false;
                    if (i==1 && horz[1][j][f]  ==0) canExit = true;
                    if (i==H && horz[H+1][j][f]==0) canExit = true;
                    if (j==1 && vert[i][1][f]  ==0) canExit = true;
                    if (j==W && vert[i][W+1][f]==0) canExit = true;
                    if (canExit) {
                        int id = encode(i,j,f);
                        if (!inA_A[id]) {
                            inA_A[id] = 1;
                            q.emplace_back(id, 0);
                        }
                    }
                }
            }
        }

        int dr[4] = {-1,1,0,0}, dc[4] = {0,0,-1,1};

        while (!q.empty()) {
            auto [u, turn] = q.front(); q.pop_front();
            if (turn == 0) {
                if (--cnt_B[u] == 0 && !inA_B[u]) {
                    inA_B[u] = 1;
                    q.emplace_back(u, 1);
                }
                int v = u ^ 1;
                if (--cnt_B[v] == 0 && !inA_B[v]) {
                    inA_B[v] = 1;
                    q.emplace_back(v, 1);
                }
            } else {
                int id = u;
                int f  = id & 1;
                int idx = id >> 1;
                int r = idx / W + 1;
                int c = idx % W + 1;

                for (int d = 0; d < 4; d++) {
                    int pr = r + dr[d], pc = c + dc[d];
                    if (pr < 1 || pr > H || pc < 1 || pc > W) continue;
                    bool ok = false;
                    if (d==0 && horz[r][c][f]  ==0) ok = true;
                    if (d==1 && horz[r+1][c][f]==0) ok = true;
                    if (d==2 && vert[r][c][f]  ==0) ok = true;
                    if (d==3 && vert[r][c+1][f]==0) ok = true;
                    if (!ok) continue;
                    int pid = encode(pr,pc,f);
                    if (!inA_A[pid]) {
                        inA_A[pid] = 1;
                        q.emplace_back(pid, 0);
                    }
                }
            }
        }
        int start = encode(R, C, 0);
        Couts(inA_A[start] ? "Yes" : "No");
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10408
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; 0 <= i; --i)
template <typename T> bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void solve();
int H,W,R,C;
bool horz[1<<10][1<<10],vert[1<<10][1<<10];
int main(){
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int t = 1<<30;
  //cin >> t;
  while (t--) {
    rep(i,1<<10)rep(j,1<<10){
      horz[i][j]=vert[i][j]=0;
    }
    cin>>H>>W>>R>>C;
    if(H==0)break;
    for(int i=1;i<=H+1;++i){
      for(int j=1;j<=W;++j)cin>>horz[i][j];
      if(i!=H+1){
        for(int j=1;j<=W+1;++j)cin>>vert[i][j];
      }
    }
    solve();
  }
}
bool in(int x,int y){
  return 0<=x&&x<=H+1&&0<=y&&y<=W+1;
}
void solve() {
  vector flag(H+2,vector<bool>(W+2,1));
  vector vis(H+2,vector<bool>(W+2));
  queue<pair<int,int>>que;
  for(int i=1;i<=H;++i)for(int j=1;j<=W;++j)flag[i][j]=0;
  rep(i,H+2)rep(j,W+2){
    if(flag[i][j]){
      vis[i][j]=1;
      que.push({i,j});
    }
  }
  while(!que.empty()){
    auto[cx,cy]=que.front();
    que.pop();
    int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
    rep(dir,4){
      int x=cx+dx[dir],y=cy+dy[dir];
      if(!in(x,y))continue;
      if(vis[x][y])continue;
      vector<int>cnt(2);
      if(in(x-1,y)&&flag[x-1][y]){ // ^
        ++cnt[horz[x][y]];
      }
      if(in(x+1,y)&&flag[x+1][y]){ // v
        ++cnt[horz[x+1][y]];
      }
      if(in(x,y-1)&&flag[x][y-1]){ // <
        ++cnt[vert[x][y]];
      }
      if(in(x,y+1)&&flag[x][y+1]){ // >
        ++cnt[vert[x][y+1]];
      }
      if(1<=cnt[0]&&1<=cnt[1]){
        flag[x][y]=1;
        vis[x][y]=1;
        que.push({x,y});
      }
    }
  }
  bool ans=0;
  if(in(R-1,C)&&!horz[R][C]&&flag[R-1][C]){ // ^
    ans=1;
  }else if(in(R+1,C)&&!horz[R+1][C]&&flag[R+1][C]){ // v
    ans=1;
  }else if(in(R,C-1)&&!vert[R][C]&&flag[R][C-1]){ // <
    ans=1;
  }else if(in(R,C+1)&&!vert[R][C+1]&&flag[R][C+1]){ // >
    ans=1;
  }
  cout<<(ans?"Yes":"No")<<'\n';
}
accuracy: 1
time_ms: 70
memory_kb: 5632
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; 0 <= i; --i)
template <typename T> bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
bool solve();
int H,W,R,C;
bool horz[1<<10][1<<10],vert[1<<10][1<<10];
int main(){
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int t = 1;
  //cin >> t;
  while (1) {
    cin>>H>>W>>R>>C;
    if(H==0){
      break;
    }
    rep(i,1<<10)rep(j,1<<10)horz[i][j]=vert[i][j]=0;
    for(int i=1;i<=H+1;++i){
      for(int j=1;j<=W;++j)cin>>horz[i][j];
      if(i!=H+1){
        for(int j=1;j<=W+1;++j)cin>>vert[i][j];
      }
    }
    cout<<(solve()?"Yes":"No")<<'\n';
  }
}
bool solve() {
  vector<vector<bool>>flag(H+2,vector<bool>(W+2));
  vector<vector<bool>>vis(H+2,vector<bool>(W+2));

  queue<pair<int,int>>que;

  for(int i=0;i<W+2;++i){
    flag[0][i]=flag[H+1][i]=1;
    vis[0][i]=vis[H+1][i]=1;
    que.push({0,i});
    que.push({H+1,i});
  }
  for(int i=0;i<H+2;++i){
    flag[i][0]=flag[i][W+1]=1;
    vis[i][0]=vis[i][W+1]=1;
    que.push({i,0});
    que.push({i,W+1});
  }
  int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
  while(!que.empty()){
    auto[wx,wy]=que.front();
    que.pop();
    rep(dir,4){
      int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
      vector<int>cnt(2);
      int x=wx+dx[dir],y=wy+dy[dir];
      if(!(0<=x&&x<=H+1&&0<=y&&y<=W+1))continue;
      if(vis[x][y])continue;
      // U
      {
        int xx=x-1,yy=y;
        if(0<=xx&&xx<=H+1&&0<=yy&&yy<=W+1){
          if(flag[xx][yy])++cnt[horz[x][y]];
        }
      }
      // D
      {
        int xx=x+1,yy=y;
        if(0<=xx&&xx<=H+1&&0<=yy&&yy<=W+1){
          if(flag[xx][yy])++cnt[horz[x+1][y]];
        }
      }
      // L
      {
        int xx=x,yy=y-1;
        if(0<=xx&&xx<=H+1&&0<=yy&&yy<=W+1){
          if(flag[xx][yy])++cnt[vert[x][y]];
        }
      }
      // R
      {
        int xx=x,yy=y+1;
        if(0<=xx&&xx<=H+1&&0<=yy&&yy<=W+1){
          if(flag[xx][yy])++cnt[vert[x][y+1]];
        }
      }
      if(1<=cnt[0]&&1<=cnt[1]){
        que.push({x,y});
        vis[x][y]=1;
        flag[x][y]=1;
      }
    }
  }
  /*
  cout<<H<<','<<W<<'\n';
  rep(i,H+2){
    rep(j,W+2)cout<<flag[i][j];
    cout<<'\n';
  }
    */
  if(!horz[R][C]&&flag[R-1][C]){ // U
    return 1;
  }else if(!horz[R+1][C]&&flag[R+1][C]){ // D
    return 1;
  }else if(!vert[R][C]&&flag[R][C-1]){ // L
    return 1;
  }else if(!vert[R][C+1]&&flag[R][C+1]){ // R
    return 1;
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 5632
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int H, W, R, C;
    cin >> H >> W >> R >> C;
    if (H == 0 && W == 0) return 0;
    R--, C--;
    vector<vector<int>> A(H+1,vector<int>(W));
    vector<vector<int>> B(H,vector<int>(W+1));
    rep(i,0,H) {
        rep(j,0,W) cin >> A[i][j];
        rep(j,0,W+1) cin >> B[i][j];
    }
    rep(j,0,W) cin >> A[H][j];
    int S = R * W + C;
    int N = H * W + 1;
    vector<vector<pair<int,int>>> G(N);
    rep(j,0,W) {
        G[N-1].push_back({j,A[0][j]});
        G[j].push_back({N-1,A[0][j]});
        rep(i,1,H) {
            G[(i-1)*W+j].push_back({i*W+j,A[i][j]});
            G[i*W+j].push_back({(i-1)*W+j,A[i][j]});
        }
        G[N-1].push_back({(H-1)*W+j,A[H][j]});
        G[(H-1)*W+j].push_back({N-1,A[H][j]});
    }
    rep(i,0,H) {
        G[N-1].push_back({i*W,B[i][0]});
        G[i*W].push_back({N-1,B[i][0]});
        rep(j,1,W) {
            G[i*W+j-1].push_back({i*W+j,B[i][j]});
            G[i*W+j].push_back({i*W+j-1,B[i][j]});
        }
        G[N-1].push_back({i*W+W-1,B[i][W]});
        G[i*W+W-1].push_back({N-1,B[i][W]});
    }
    vector<vector<bool>> DP(N,vector<bool>(2,false));
    queue<int> Q;
    DP[N-1][0] = DP[N-1][1] = true;
    Q.push(N-1);
    while(!Q.empty()) {
        int P = Q.front();
        Q.pop();
        for (pair<int,int> NP : G[P]) {
            if (!DP[NP.first][NP.second]) {
                DP[NP.first][NP.second] = true;
                if (DP[NP.first][0] && DP[NP.first][1]) Q.push(NP.first);
            }
        }
    }
    cout << (DP[S][0] ? "Yes" : "No") << endl;
}
}
accuracy: 1
time_ms: 330
memory_kb: 40796
final_rank: 18

code_snippet: #if 1
// clang-format off
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using lf = long double;
using pll = pair<ll, ll>;
#define vec vector
template <class T> using v = vector<T>;
template <class T> using vv = v<v<T>>;
template <class T> using vvv = v<vv<T>>;
using vl = v<ll>;
using vvl = vv<ll>;
using vvvl = vvv<ll>;
using vpl = v<pll>;
using vs = v<string>;
using vb = v<bool>;
using vvb = v<vb>;
using vvvb = v<vvb>;
template<class T> using PQ = priority_queue<T, v<T>, greater<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)
#define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++)
#define rrep(i, N) for (ll i = N - 1; i >= 0; i--)
#define rrep1(i, N) for (ll i = N; i > 0; i--)
#define fore(i, a) for (auto &i : a)
#define fs first
#define sc second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define UNIQUE(x) (x).erase(unique((x).begin(), (x).end()), (x).end());
#define YES(x) cout << ((x) ? "YES\n" : "NO\n");
#define Yes(x) cout << ((x) ? "Yes\n" : "No\n");
#define yes(x) cout << ((x) ? "yes\n" : "no\n");
template <class T, class U> void chmin(T &t, const U &u) { if (t > u) t = u; }
template <class T, class U> void chmax(T &t, const U &u) { if (t < u) t = u; }
template <class T> T min(const v<T> &lis) { return *min_element(all(lis)); }
template <class T> T max(v<T> &lis) { return *max_element(all(lis)); }
const int inf = (1 << 30);
const ll infl = (1LL << 60);
const ll mod93 = 998244353;
const ll mod17 = 1000000007;
int popcnt(uint x) { return __builtin_popcount(x); }
int popcnt(ull x) { return __builtin_popcountll(x); }
// 桁数
int bsr(uint x) { return 31 - __builtin_clz(x); }
int bsr(ull x) { return 63 - __builtin_clzll(x); }
// 2で割れる回数
int bsf(uint x) { return __builtin_ctz(x); }
int bsf(ull x) { return __builtin_ctzll(x); }

template <class T, class S> istream &operator>>(istream &is, pair<T, S> &x) { return is >> x.first >> x.second; }
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &x) { return os << x.first << " " << x.second; }
template <class T> istream &operator>>(istream &is, vector<T> &x) { for (auto &y : x) is >> y; return is; }
template <class T> ostream &operator<<(ostream &os, const vector<T> &x) {
  for (size_t i = 0, size = x.size(); i < size; i++)
    os << x[i] << (i == size - 1 ? "" : " ");
  return os;
}

ll rand_int(ll l, ll r) {  // [l, r]
  static random_device rd;
  static mt19937 gen(rd());
  return uniform_int_distribution<ll>(l, r)(gen);
}

// #include <boost/multiprecision/cpp_int.hpp>
// using cpp_int = boost::multiprecision::cpp_int;

// clang-format on
#endif

// #define _GLIBCXX_DEBUG

struct Solver {
  ll h, w;
  vvl orz, ert;
  vl dx = {-1, 0, 1, 0};
  vl dy = {0, -1, 0, 1};
  Solver(ll h, ll w) : h(h), w(w){};
  void solve(ll r, ll c) {
    r--, c--;
    orz = vvl(h + 1, vl(w));
    ert = vvl(h, vl(w + 1));
    rep(i, h) cin >> orz[i] >> ert[i];
    cin >> orz[h];

    vvl dp(h, vl(w));
    vvb used(h, vb(w));
    queue<pll> q;

    rep(i, h) {
      rep(b, 2) {
        if (check(i, 0, b, 1)) dp[i][0] |= 1 << b;
        if (check(i, w - 1, b, 3)) dp[i][w - 1] |= 1 << b;
      }
    }
    rep(i, w) {
      rep(b, 2) {
        if (check(0, i, b, 0)) dp[0][i] |= 1 << b;
        if (check(h - 1, i, b, 2)) dp[h - 1][i] |= 1 << b;
      }
      if (dp[0][i] == 3) q.push({0, i});
      if (dp[h - 1][i] == 3) q.push({h - 1, i});
    }

    while(q.size()) {
      auto [x, y] = q.front();
      q.pop();
      if (used[x][y]) continue;
      used[x][y] = true;

      rep(d, 4) {
        ll nx = x + dx[d];
        ll ny = y + dy[d];
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          continue;
        if (dp[nx][ny] == 3) continue;
        rep(b, 2) {
          if (check(x, y, b, d))
            dp[nx][ny] |= 1 << b;
        }
        if (dp[nx][ny] == 3) q.push({nx, ny});
      }
    }

    Yes(dp[r][c] & 1);
  }
  bool check(ll x, ll y, ll b, ll d) {
    if (d == 0) {
      return orz[x][y] == b;
    } else if (d == 1) {
      return ert[x][y] == b;
    } else if (d == 2) {
      return orz[x + 1][y] == b;
    } else if (d == 3) {
      return ert[x][y + 1] == b;
    }
    cerr << "Solver.check: Error d is " << d << endl;
    exit(0);
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll t = 1;
  // cin >> t;
  while (t) {
    ll h, w, r, c;
    cin >> h >> w >> r >> c;
    if (h == 0 && w == 0) break;
    Solver solver(h, w);
    solver.solve(r, c);
  }
}
accuracy: 1
time_ms: 70
memory_kb: 9388
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(A) A.begin(),A.end()


vll dx = { 1,0,-1,0 };
vll dy = { 0,-1,0,1 };

void solve(ll H, ll W, ll R, ll C) {
    vector<vll> G(H + 2, vll(W + 2, 0));
    rep(i, 2 * H + 1) {
        if (i % 2 == 0) {
            rep(w, W) {
                ll a;
                cin >> a;
                if (a == 0) {
                    G[i / 2][w + 1] += 8;
                    G[i / 2 + 1][w + 1] += 2;
                }
            }
        }
        else {
            rep(w, W + 1) {
                ll a;
                cin >> a;
                if (a == 0) {
                    G[i / 2 + 1][w] += 1;
                    G[i / 2 + 1][w + 1] += 4;
                }
            }
        }
    }
    queue<pair<ll, ll>> Q;
    vector<vector<bool>> seen(H + 2, vector<bool>(W + 2, 0));
    rep(h, H + 2) {
        Q.push({ h,0 });
        Q.push({ h,W + 1 });
        seen[h][0] = seen[h][W + 1] = 1;
    }
    rep(w, W + 2) {
        Q.push({ 0,w });
        Q.push({ H + 1,w });
        seen[0][w] = seen[H+1][w] = 1;
    }
    while (!Q.empty()) {
        ll y = Q.front().first;
        ll x = Q.front().second;
        Q.pop();
        rep(d, 4) {
            ll ny = y + dy[d];
            ll nx = x + dx[d];
            if (ny < 0 || nx < 0 || ny >= H+2 || nx >= W+2)continue;
            if (seen[ny][nx])continue;
            ll cn = 0;
            rep(e, 4) {
                ll py = ny + dy[e];
                ll px = nx + dx[e];
                if (py < 0 || px < 0 || py >= H+2 || px >= W+2)continue;
                if (!seen[py][px])continue;
                if (G[ny][nx] & (1 << e))cn |= 1;
                else cn |= 2;
            }
            if (cn == 3) {
                Q.push({ ny,nx });
                seen[ny][nx] = 1;
            }
        }
    }
    rep(d, 4) {
        ll y = R + dy[d];
        ll x = C + dx[d];
        if (seen[y][x] && (G[R][C] & (1 << d)) > 0) {
            cout << "Yes" << endl;
            return;
        }
    }
    cout << "No" << endl;

}

int main() {

    ll H, W, R, C;
    while (cin >> H >> W >> R >> C) {
        if (H == 0)return 0;
        solve(H, W, R, C);
    }
}
accuracy: 1
time_ms: 170
memory_kb: 5484
final_rank: 5

code_snippet: #ifdef RELEASE
#pragma GCC target("arch=x86-64-v3")
#pragma GCC optimize("Ofast")
#endif

#include <bits/stdc++.h>

//#include <atcoder/all>


using namespace std;

using ll = long long;
using i64 = long long;
using pll = pair<ll,ll>;
using plll = pair<pll,ll>;

constexpr ll mod = 998244353;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};



int is_ok(int x,int y,int f, vector<vector<vector<int>>>& F,vector<vector<vector<int>>>& dp){
  int H=F.size(), W=F[0].size();

  int ret=0;

  if (x==0 && f*(1-F[x][y][2])+(1-f)*F[x][y][2]==0)ret=1;
  if (x==H-1 && f*(1-F[x][y][0])+(1-f)*F[x][y][0]==0)ret=1;
  if (y==0 && f*(1-F[x][y][3])+(1-f)*F[x][y][3]==0)ret=1;
  if (y==W-1 && f*(1-F[x][y][1])+(1-f)*F[x][y][1]==0)ret=1;

  for (int d=0; d<4; d++){
    int nx=x+dx[d],ny=y+dy[d];
    if (f*(1-F[x][y][d])+(1-f)*F[x][y][d]==1)continue; //道が空いていないとき
    if (nx<0 || nx>=H || ny<0 || ny>=W)continue; //場外は上で処理した

    if (dp[nx][ny][0]==1 && dp[nx][ny][1]==1) {
      ret=1;

    }
  }
  dp[x][y][f]=ret;
  return ret;
}


int main(){
  while (true){
    int H,W,R,C;
    cin>>H>>W>>R>>C;
    R--;
    C--;
    if (H==0)break;
    vector<vector<vector<int>>> F(H,vector<vector<int>>(W,vector<int>(4)));
    for (int i=0; i<=2*H; i++){
      if (i%2==0){
        if (i==0){
          for (int j=0; j<W; j++){
            cin>>F[i/2][j][2];
          }
        }
        else if (i==2*H){
          for (int j=0; j<W; j++){
            cin>>F[(i/2)-1][j][0];
          }

        }
        else{
          for (int j=0; j<W; j++){
            cin>>F[i/2][j][2];
            F[(i/2)-1][j][0]=F[i/2][j][2];
          }
        }
      }
      else{
        for (int j=0; j<W+1; j++){
          if (j==0){
            cin>>F[(i/2)][j][3];
          }
          else if (j==W){
            cin>>F[(i/2)][j-1][1];
          }
          else{
            cin>>F[(i/2)][j][3];
            F[(i/2)][j-1][1]=F[(i/2)][j][3];
          }
        }


      }
    }
    vector<vector<vector<int>>> dp(H,vector<vector<int>>(W,vector<int>(2)));

    vector<tuple<int,int,int>> que;
    for (int i=0; i<H; i++){
      que.emplace_back(i,0,0);
      que.emplace_back(i,0,1);
      que.emplace_back(i,W-1,0);
      que.emplace_back(i,W-1,1);
    }
    for (int j=1; j<W-1; j++){
      que.emplace_back(0,j,0);
      que.emplace_back(0,j,1);
      que.emplace_back(H-1,j,0);
      que.emplace_back(H-1,j,1);

    }

    int ind=0;
    while (que.size()>ind){
      auto [x, y, f] = que[ind];


      if (dp[x][y][f]==1){
        ind++;
        continue;
      }

      if (is_ok(x,y,f,F,dp)==1){
        for (int d=0; d<4; d++){
          int nx=x+dx[d],ny=y+dy[d];
          if (nx<0 || nx>=H || ny<0 || ny>=W){
            continue;
          }
          if (dp[nx][ny][0]==0) que.emplace_back(nx,ny,0);
          if (dp[nx][ny][1]==0) que.emplace_back(nx,ny,1);
        }
      }

      ind++;
    }


    string ans;
    if (dp[R][C][0]==1)ans="Yes";
    else ans="No";

    cout<<ans<<endl;





  }
}
accuracy: 1
time_ms: 300
memory_kb: 78468
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
int H,W,R,C;
int Hor[505][505],Ver[505][505];
bool alice[505][505];
bool solve()
{
    cin>>H>>W>>R>>C;
    if(H==0&&W==0&&R==0&&C==0)return 0;
    for(int j=1;j<=W;j++)cin>>Hor[0][j];
    for(int i=1;i<=H;i++)
    {
        for(int j=0;j<=W;j++)cin>>Ver[i][j];
        for(int j=1;j<=W;j++)cin>>Hor[i][j];
    }
    for(int i=0;i<=H+1;i++)for(int j=0;j<=W+1;j++)alice[i][j]=0;
    queue<pair<int,int>>q;
    for(int i=0;i<=H+1;i++)alice[i][0]=alice[i][W+1]=1;
    for(int j=0;j<=W+1;j++)alice[0][j]=alice[H+1][j]=1;
    for(int i=1;i<=H;i++)q.push(make_pair(i,1)),q.push(make_pair(i,W));
    for(int j=1;j<=W;j++)q.push(make_pair(1,j)),q.push(make_pair(H,j));
    while(!q.empty())
    {
        auto[r,c]=q.front();
        q.pop();
        if(alice[r][c])continue;
        assert(1<=r&&r<=H&&1<=c&&c<=W);
        int tr=0;
        if(alice[r-1][c])tr|=1<<Hor[r-1][c];
        if(alice[r][c-1])tr|=1<<Ver[r][c-1];
        if(alice[r+1][c])tr|=1<<Hor[r][c];
        if(alice[r][c+1])tr|=1<<Ver[r][c];
        if(tr==3)
        {
            alice[r][c]=1;
            q.push(make_pair(r-1,c));
            q.push(make_pair(r,c-1));
            q.push(make_pair(r+1,c));
            q.push(make_pair(r,c+1));
        }
    }
    cout<<(
        (alice[R-1][C]&&!Hor[R-1][C])||
        (alice[R][C-1]&&!Ver[R][C-1])||
        (alice[R+1][C]&&!Hor[R][C])||
        (alice[R][C+1]&&!Ver[R][C])
        ?"Yes":"No")<<'\n';
    return 1;
}
int main(){while(solve()){}}
accuracy: 1
time_ms: 170
memory_kb: 5564
final_rank: 8

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

template < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }
template < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }
template < class T > vector< T >  operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }
template < class T > vector< T >  operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

bool solve(int H, int W, int R, int C) {
    R--, C--;
    vector Horz(H + 1, vector(W, int(-1)));
    vector Vert(H, vector(W + 1, int(-1)));
    for(int i : rep(H)) {
        Horz[i] = in(W);
        Vert[i] = in(W + 1);
    }
    Horz[H] = in(W);

    vector can(H, vector(W, false));
    queue<pair<int,int>> q;
    for(int i : rep(H)) for(int j : rep(W)) {
        int cnt = 0;
        for(auto [di, dj] : dir4) {
            int ni = i + di, nj = j + dj;
            if(not(0 <= ni and ni < H and 0 <= nj and nj < W)) cnt++;
        }
        if(cnt >= 2) q.push({i, j});
    }

    while(not q.empty()) {
        auto [i, j] = q.front(); q.pop();
        if(can[i][j]) continue;
        int cnt[2] = {};
        if(not(0 <= i - 1) or can[i - 1][j]) cnt[Horz[i][j]]++;
        if(not(i + 1 <  H) or can[i + 1][j]) cnt[Horz[i + 1][j]]++;
        if(not(0 <= j - 1) or can[i][j - 1]) cnt[Vert[i][j]]++;
        if(not(j + 1 <  W) or can[i][j + 1]) cnt[Vert[i][j + 1]]++;
        if(1 <= cnt[0] and 1 <= cnt[1]) {
            can[i][j] = true;
            if(0 <= i - 1 and not can[i - 1][j]) q.push({i - 1, j});
            if(i + 1 <  H and not can[i + 1][j]) q.push({i + 1, j});
            if(0 <= j - 1 and not can[i][j - 1]) q.push({i, j - 1});
            if(j + 1 <  W and not can[i][j + 1]) q.push({i, j + 1});
        }
    }

    bool win = [&] {
        auto [i, j] = make_pair(R, C);
        if(can[i][j]) return true;
        if((not(0 <= i - 1) or can[i - 1][j]) and Horz[i][j] == 0) return true;
        if((not(i + 1 <  H) or can[i + 1][j]) and Horz[i + 1][j] == 0) return true;
        if((not(0 <= j - 1) or can[i][j - 1]) and Vert[i][j] == 0) return true;
        if((not(j + 1 <  W) or can[i][j + 1]) and Vert[i][j + 1] == 0) return true;
        return false;
    }();

    return win;
}

int main() {
    while(true) {
        int H = in(), W = in(), R = in(), C = in();
        if(make_tuple(H, W, R, C) == make_tuple(0, 0, 0, 0)) return 0;
        print(solve(H, W, R, C) ? "Yes" : "No");
    }
}
accuracy: 1
time_ms: 80
memory_kb: 5480
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;

void solve(int h, int w, int r, int c) {
    vector<vector<int>> g (h,vector<int> (w,0));
    vector<vector<int>> wall (2*h+1,vector<int> (w+1));

        rep(i,2*h+1) {
            if (i%2 == 0) {
                rep(j,w) {
                    cin >> wall[i][j];
                }
            } 
            else {
                rep(j,w+1) {
                    cin >> wall[i][j];
                }
            }
        }

        if (r-1 == 0 && !wall[0][c-1]) {
            cout << "Yes" << endl;
            return;
        }
        if (r-1 == h-1 && !wall[2*h][c-1]) {
            cout << "Yes" << endl;
            return;
        }
        if (c-1 == 0 && !wall[2*r-1][0]) {
            cout << "Yes" << endl;
            return;
        }
        if (c-1 == w-1 && !wall[2*r-1][w]) {
            cout << "Yes" << endl;
            return;
        }

        queue<pair<int,int>> que;

        que.push({0,0});
        que.push({0,w-1});
        que.push({h-1,0});
        que.push({h-1,w-1});

        if (h == 1) {
            rep(i,w) {
                que.push({0,i});
            }
        }
        else if (w == 1) {
            rep(i,h) {
                que.push({i,0});
            }
        }

        while (!que.empty()) {
            pair<int,int> p = que.front();
            que.pop();
            bool open = false, close = false;
            if (p.first == 0 || g[max(0,p.first-1)][p.second]) {
                if (wall[2*p.first][p.second]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (p.first == h-1 || g[min(h-1,p.first+1)][p.second]) {
                if (wall[2*p.first+2][p.second]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (p.second == 0 || g[p.first][max(p.second-1,0)]) {
                if (wall[2*p.first+1][p.second]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (p.second == w-1 || g[p.first][min(w-1,p.second+1)]) {
                if (wall[2*p.first+1][p.second+1]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (open && close) {
                g[p.first][p.second] = 1;
                if (p.first == r-1 && p.second == c-1) {
                    cout << "Yes" << endl;
                    return;
                }
                /*if (p.first == r-2 && p.second == c-1 && wall[2*p.first+2][p.second] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r && p.second == c-1 && wall[2*p.first][p.second] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r-1 && p.second == c-2 && wall[2*p.first+1][p.second+1] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r-1 && p.second == c && wall[2*p.first+1][p.second] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                */
                if (p.first != 0) {
                    if (!g[p.first-1][p.second]) {
                        que.push({p.first-1,p.second});
                    }
                }
                if (p.first != h-1) {
                    if (!g[p.first+1][p.second]) {
                        que.push({p.first+1,p.second});
                    }
                }
                if (p.second != 0) {
                    if (!g[p.first][p.second-1]) {
                        que.push({p.first,p.second-1});
                    }
                }
                if (p.second != w-1) {
                    if (!g[p.first][p.second+1]) {
                        que.push({p.first,p.second+1});
                    }
                }
            }
        }
        cout << "No" << endl;
        return;
}


int main() {
    int h,w,r,c;
    while (true) {
        cin >> h >> w >> r >> c;
        if (!h) {
            return 0;
        }
        solve(h,w,r,c);
    }
}
accuracy: 1
time_ms: 160
memory_kb: 5884
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;

void solve(int h, int w, int r, int c) {
    vector<vector<int>> g (h,vector<int> (w,0));
    vector<vector<int>> wall (2*h+1,vector<int> (w+1));

        rep(i,2*h+1) {
            if (i%2 == 0) {
                rep(j,w) {
                    cin >> wall[i][j];
                }
            } 
            else {
                rep(j,w+1) {
                    cin >> wall[i][j];
                }
            }
        }

        if (r-1 == 0 && !wall[0][c-1]) {
            cout << "Yes" << endl;
            return;
        }
        if (r-1 == h-1 && !wall[2*h][c-1]) {
            cout << "Yes" << endl;
            return;
        }
        if (c-1 == 0 && !wall[2*r-1][0]) {
            cout << "Yes" << endl;
            return;
        }
        if (c-1 == w-1 && !wall[2*r-1][w]) {
            cout << "Yes" << endl;
            return;
        }

        queue<pair<int,int>> que;

        que.push({0,0});
        que.push({0,w-1});
        que.push({h-1,0});
        que.push({h-1,w-1});

        if (h == 1) {
            rep(i,w) {
                que.push({0,i});
            }
        }
        else if (w == 1) {
            rep(i,h) {
                que.push({i,0});
            }
        }

        while (!que.empty()) {
            pair<int,int> p = que.front();
            que.pop();
            bool open = false, close = false;
            if (p.first == 0 || g[max(0,p.first-1)][p.second]) {
                if (wall[2*p.first][p.second]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (p.first == h-1 || g[min(h-1,p.first+1)][p.second]) {
                if (wall[2*p.first+2][p.second]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (p.second == 0 || g[p.first][max(p.second-1,0)]) {
                if (wall[2*p.first+1][p.second]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (p.second == w-1 || g[p.first][min(w-1,p.second+1)]) {
                if (wall[2*p.first+1][p.second+1]) {
                    close = true;
                }
                else {
                    open = true;
                }
            }
            if (open && close) {
                g[p.first][p.second] = 1;
                if (p.first == r-1 && p.second == c-1) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r-2 && p.second == c-1 && wall[2*p.first+2][p.second] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r && p.second == c-1 && wall[2*p.first][p.second] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r-1 && p.second == c-2 && wall[2*p.first+1][p.second+1] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first == r-1 && p.second == c && wall[2*p.first+1][p.second] == 0) {
                    cout << "Yes" << endl;
                    return;
                }
                if (p.first != 0) {
                    if (!g[p.first-1][p.second]) {
                        que.push({p.first-1,p.second});
                    }
                }
                if (p.first != h-1) {
                    if (!g[p.first+1][p.second]) {
                        que.push({p.first+1,p.second});
                    }
                }
                if (p.second != 0) {
                    if (!g[p.first][p.second-1]) {
                        que.push({p.first,p.second-1});
                    }
                }
                if (p.second != w-1) {
                    if (!g[p.first][p.second+1]) {
                        que.push({p.first,p.second+1});
                    }
                }
            }
        }
        cout << "No" << endl;
        return;
}


int main() {
    int h,w,r,c;
    while (true) {
        cin >> h >> w >> r >> c;
        if (!h) {
            return 0;
        }
        solve(h,w,r,c);
    }
}
accuracy: 1
time_ms: 160
memory_kb: 5804
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;

pair<vector<vector<ll>>, vector<vector<ll>>> rot(vector<vector<ll>> Ho, vector<vector<ll>> Ve) {
    ll H = (ll)Ho.size() - 2;
    ll W = (ll)Ho[0].size() - 1;
    vector<vector<ll>> newHo(W+2, vector<ll>(H+1, 0));
    vector<vector<ll>> newVe(W+1, vector<ll>(H+2, 0));
    rep(i,1,W+2){
        rep(j,1,H+1){
            newHo[i][j] = Ve[j][W+2 - i];
        }
    }
    rep(i,1,W+1){
        rep(j,1,H+2){
            newVe[i][j] = Ho[j][W+1 - i];
        }
    }

    return make_pair(newHo, newVe);
}

void solve(ll H, ll W, ll R, ll C) {
    vector<vector<ll>> Ho(H+2, vector<ll>(W+1, 0));
    vector<vector<ll>> Ve(H+1, vector<ll>(W+2, 0));
    rep(i,1,H+2) {
        if (i == H + 1){
            rep(j,1,W+1){
                cin>>Ho[i][j];
            }
        } else {
            rep(j,1,W+1){
                cin>>Ho[i][j];
            }
            rep(j,1,W+2){
                cin>>Ve[i][j];
            }
        }
    }

    // ///// rot test

    // cerr << "\n";
    // rep(i,1,H+2){
    //     rep(j,1,W+1){
    //         cerr << Ho[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // cerr << "\n";
    // rep(i,1,H+1) {
    //     rep(j,1,W+2){
    //         cerr << Ve[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // cerr << "\n";

    // pair<vector<vector<ll>>, vector<vector<ll>>> p = rot(Ho, Ve);
    // Ho = p.first;
    // Ve = p.second;

    // cerr << "\n";
    // rep(i,1,H+2){
    //     rep(j,1,W+1){
    //         cerr << Ho[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // cerr << "\n";
    // rep(i,1,H+1) {
    //     rep(j,1,W+2){
    //         cerr << Ve[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // cerr << "\n";

    // p = rot(Ho, Ve);
    // Ho = p.first;
    // Ve = p.second;

    // cerr << "\n";
    // rep(i,1,H+2){
    //     rep(j,1,W+1){
    //         cerr << Ho[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // cerr << "\n";
    // rep(i,1,H+1) {
    //     rep(j,1,W+2){
    //         cerr << Ve[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // cerr << "\n";
    
    if (R == 1 && Ho[1][C] == 0) {
        cout << "Yes" << "\n";
        return;
    }
    if (R == H && Ho[H+1][C] == 0) {
        cout << "Yes" << "\n";
        return;
    }
    if (C == 1 && Ve[R][1] == 0) {
        cout << "Yes" << "\n";
        return;
    }
    if (C == W && Ve[R][W+1] == 0) {
        cout << "Yes" << "\n";
        return;
    }

    rep(r_, 0, 4) {
        pair<vector<vector<ll>>, vector<vector<ll>>> p = rot(Ho, Ve);
        ll newW = H;
        ll newH = W;
        ll newR = W+1-C;
        ll newC = R;

        H = newH;
        W = newW;
        R = newR;
        C = newC;

        Ho = p.first;
        Ve = p.second;

        // if (Ho[1][1] == Ve[1][1])continue;

        vector<vector<ll>> dp(H+2, vector<ll>(W+2, -1));
        rep(i,0,H+2){
            dp[i][0] = 0;
            dp[i][W+1] = 0;
        }
        rep(j,0,W+2){
            dp[0][j] = 0;
            dp[H+1][j] = 0;
        }
        queue<pair<ll, ll>> que;
        // dp[1][1] = 0;
        // que.push(make_pair(0, 1));
        // que.push(make_pair(1, 0));
        // que.push(make_pair(1, 2));
        // que.push(make_pair(2, 1));

        rep(i,1,H+1){
            que.push(make_pair(i, 1));
            que.push(make_pair(i, W));
        }
        rep(j,1,W+1){
            que.push(make_pair(1, j));
            que.push(make_pair(H, j));
        }

        while(!que.empty()) {
            pair<ll,ll> p = que.front();que.pop();
            ll x = p.first;
            ll y = p.second;

            // cerr << "x:" << x<< " y:" << y << endl;

            if (dp[x][y] == 0)continue;

            vector<ll> dx = {-1, 0, 1, 0};
            vector<ll> dy = {0, -1, 0, 1};
            set<ll> st;
            rep(k,0,4){
                ll nx = x + dx[k];
                ll ny = y + dy[k];
                if(dp[nx][ny] == 0) {
                    ll kabe;
                    if (k == 0) {
                        kabe = Ho[x][y];
                    } else if (k == 1) {
                        kabe = Ve[x][y];
                    } else if (k == 2) {
                        kabe = Ho[x + 1][y];
                    } else {
                        kabe = Ve[x][y + 1];
                    }
                    st.insert(kabe);
                    // cerr << x << ' ' << y << ' ' << nx << ' ' << ny << ' ' << kabe << "\n";
                }
            }
            // cerr << "====" << endl;
            // cerr << x << ' ' << y << ' ' << st.size() << endl;

            if(st.size() >= 2) {
                dp[x][y] = 0;
                rep(k,0,4){
                    ll nx = x + dx[k];
                    ll ny = y + dy[k];
                    if(dp[nx][ny] == -1){
                        que.push(make_pair(nx, ny));
                    }
                }
            }
        }
        // cerr << "----" << endl;
        // rep(i,1,H+1){
        //     rep(j,1,W+1){
        //         cerr << dp[i][j] << ' ';
        //     }
        //     cerr << endl;
        // }
        // cerr << "----" << endl;
        if (dp[R][C] == 0) {
            cout << "Yes" << "\n";
            return;
        }
    }
    cout << "No" << "\n";
    return;
}
int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(true){
        ll H, W, R, C;cin>>H>>W>>R>>C;
        if(H == 0)break;
        solve(H, W, R, C);
    }
}
accuracy: 1
time_ms: 150
memory_kb: 18900
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int H, W, sx, sy;
int dp[509][509][2];
int Hori[509][509];
int Vert[509][509];
vector<pair<int, int>> G[2][509][509];
queue<pair<int, int>> Q;

void Initialize() {
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 509; j++) {
            for (int k = 0; k < 509; k++) G[i][j][k].clear();
            for (int k = 0; k < 509; k++) dp[j][k][i] = 0;
            for (int k = 0; k < 509; k++) Hori[j][k] = 0;
            for (int k = 0; k < 509; k++) Vert[j][k] = 0;
        }
    }
    while (!Q.empty()) Q.pop();
}

void AddQue(int px, int py) {
    for (int i = 0; i < 4; i++) {
        int sx = px + dx[i];
        int sy = py + dy[i];
        if (sx <= 0 || sx > H || sy <= 0 || sy > W) continue;
        if (dp[sx][sy][0] == 1 && dp[sx][sy][1] == 1) continue;
        Q.push(make_pair(sx, sy));
    }
}

string Solve() {
    // Step 2. Make Graph (Part 1)
    for (int i = 1; i <= H + 1; i++) {
        for (int j = 1; j <= W + 0; j++) {
            if (Hori[i][j] == 0) {
                G[0][i][j].push_back(make_pair(i - 1, j));
                G[0][i - 1][j].push_back(make_pair(i, j));
            }
            if (Hori[i][j] == 1) {
                G[1][i][j].push_back(make_pair(i - 1, j));
                G[1][i - 1][j].push_back(make_pair(i, j));
            }
        }
    }

    // Step 3. Make Graph (Part 2)
    for (int i = 1; i <= H + 0; i++) {
        for (int j = 1; j <= W + 1; j++) {
            if (Vert[i][j] == 0) {
                G[0][i][j].push_back(make_pair(i, j - 1));
                G[0][i][j - 1].push_back(make_pair(i, j));
            }
            if (Vert[i][j] == 1) {
                G[1][i][j].push_back(make_pair(i, j - 1));
                G[1][i][j - 1].push_back(make_pair(i, j));
            }
        }
    }

    // Step 4. Starting Position
    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            for (int k = 0; k < 2; k++) {
                bool flag = false;
                for (pair<int, int> l : G[k][i][j]) {
                    if (l.first == 0 || l.second == 0 || l.first == H + 1 || l.second == W + 1) flag = true;
                }
                if (flag == false) continue;
                dp[i][j][k] = 1;
                AddQue(i, j);
            }
        }
    }

    // Step 5. BFS
    while (!Q.empty()) {
        int px = Q.front().first;
        int py = Q.front().second; Q.pop();
        for (int i = 0; i < 2; i++) {
            if (dp[px][py][i] == 1) continue;
            bool flag = false;
            for (pair<int, int> l : G[i][px][py]) {
                if (dp[l.first][l.second][0] == 1 && dp[l.first][l.second][1] == 1) flag = true;
            }
            if (flag == true) {
                dp[px][py][i] = 1;
                AddQue(px, py);
            }
        }
    }

    // Step 6. Return
    if (dp[sx][sy][0] == 1) return "Yes";
    return "No";
}

int main() {
    while (true) {
        Initialize();
        
        // Step 1. Input
        cin >> H >> W >> sx >> sy; if (H + W + sx + sy == 0) break;
        for (int i = 1; i <= H + 1; i++) {
            for (int j = 1; j <= W + 0; j++) cin >> Hori[i][j];
            if (i == H + 1) break;
            for (int j = 1; j <= W + 1; j++) cin >> Vert[i][j];
        }

        // Step 2. Output
        cout << Solve() << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 41716
final_rank: 17

code_snippet: #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using lint = ll;
using ulint = ull;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, n) For(i, 0, n)
template <class T, class U>
bool chmin(T &a, U &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
bool chmax(T &a, U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

constexpr int dx[4] = {0, 1, 0, -1};
constexpr int dy[4] = {1, 0, -1, 0};

int main() {
    while (true) {
        int h, w, r, c;
        cin >> h >> w >> r >> c;
        if (h == 0) break;

        vector<vector<int>> hor(h + 1, vector<int>(w));
        vector<vector<int>> ver(h, vector<int>(w + 1));
        rep(i, h) {
            rep(j, w) cin >> hor[i][j];
            rep(j, w + 1) cin >> ver[i][j];
        }
        rep(j, w) cin >> hor[h][j];

        vector<vector<bool>> isa(h + 2, vector<bool>(w + 2, false));
        queue<pii> que;
        rep(j, w + 2) {
            que.emplace(0, j);
            que.emplace(h + 1, j);
        }
        rep(i, h + 2) {
            que.emplace(i, 0);
            que.emplace(i, w + 1);
        }
        while (!que.empty()) {
            auto [x, y] = que.front();
            que.pop();
            if (isa[x][y]) continue;

            if (x == 0 || x == h + 1 || y == 0 || y == w + 1) {
                isa[x][y] = true;
            } else {
                vector<int> cnt(2, 0);
                if (isa[x - 1][y]) {
                    ++cnt[hor[x - 1][y - 1]];
                }
                if (isa[x + 1][y]) {
                    ++cnt[hor[x][y - 1]];
                }
                if (isa[x][y - 1]) {
                    ++cnt[ver[x - 1][y - 1]];
                }
                if (isa[x][y + 1]) {
                    ++cnt[ver[x - 1][y]];
                }

                if (cnt[0] >= 1 && cnt[1] >= 1) {
                    isa[x][y] = true;
                }
            }

            if (!isa[x][y]) continue;
            rep(i, 4) {
                int nx = x + dx[i], ny = y + dy[i];
                if (nx < 1 || nx > h || ny < 1 || ny > w) continue;
                que.emplace(nx, ny);
            }
        }

        int x = r, y = c;
        vector<int> cnt(2, 0);
        if (isa[x - 1][y]) {
            ++cnt[hor[x - 1][y - 1]];
        }
        if (isa[x + 1][y]) {
            ++cnt[hor[x][y - 1]];
        }
        if (isa[x][y - 1]) {
            ++cnt[ver[x - 1][y - 1]];
        }
        if (isa[x][y + 1]) {
            ++cnt[ver[x - 1][y]];
        }
        puts(cnt[0] ? "Yes" : "No");
    }
}
accuracy: 1
time_ms: 170
memory_kb: 5552
final_rank: 7