Problem:
Score : 300 points

Problem Statement

We have a square grid with H rows and W columns.
Snuke wants to write 0 or 1 in each of the squares.
Here, all of the following conditions have to be satisfied:

For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)

For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.

Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints

1 \leq H,W \leq 1000

0 \leq A

2 \times A \leq W

0 \leq B

2 \times B \leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W A B

Output

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

s_{11}s_{12}\cdots s_{1W}
s_{21}s_{22}\cdots s_{2W}
\vdots
s_{H1}s_{H2}\cdots s_{HW}

Here s_{ij} is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.

If multiple solutions exist, printing any of them will be accepted.

Sample Input 1

3 3 1 1

Sample Output 1

100
010
001

Every row contains two 0s and one 1, so the first condition is satisfied.
Also, every column contains two 0s and one 1, so the second condition is satisfied.

Sample Input 2

1 5 2 0

Sample Output 2

01010

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

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

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}

#define ll long long int 
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (300006)
ll n, m, a, b;
set<pi> col;
bool ans[1001][1001];
vector<ll> v[1001];
int main()
{
	FAST
	cin>>n>>m>>a>>b;
	if(n==1 && m==1) {
		cout<<0;
		return 0;
	}
	if(n==1) {
		FOR(i,0,a-1) cout<<"1";
		FOR(i,a,m-1) cout<<"0";
		return 0;
	}
	if(m==1) {
		FOR(i,0,b-1) cout<<"1\n";
		FOR(i,b,n-1) cout<<"0\n";
		return 0;
	}
	FOR(j,0,m-1) col.ins(pi(0,j));
	FOR(i,0,n-1) {
		set<pi> col2;
		FOR(j,0,a-1) {
			if(col.begin()->f < b) {
				ans[i][col.begin()->s]=1;
				pi x=*col.begin(); col.erase(col.begin());
				++ x.f;
				col2.ins(x);
			} else {
				auto k=col.lower_bound(pi(n-b, -INF)); 
				if(k==col.begin()) { assert(0); cout<<"-1\n"; return 0; }
				-- k;
				pi x=*k; col.erase(k);
				++ x.f; ans[i][x.s]=1;
				col2.ins(x);
			}
		}
		for(auto i:col2) col.ins(i);
	}
	FOR(i,0,n-1) FOR(j,0,m-1) if(ans[i][j] == 0) v[j].eb(i);
	set<pi> help;
	for(auto i:col) {
		if(i.f < b) {
			help.ins(pi(b-i.f, i.s));
		} else if(i.f > b) {
			assert(i.f <= n-b);
			if(i.f == n-b) continue;
			help.ins(pi(n-b-i.f, i.s));
		}
	}
	if(help.size()) FOR(ii,0,n-1) {
		if((--help.end())->f == 0) break;
		// set<pi> col2;
		FOR(j,a,m-a-1) {
			// while(help.size() && ans[i][(--help.end())->s]) {
				// col2.ins(*(--help.end()));
				// help.erase((--help.end()));
			// }
			if(help.empty() || (--help.end())->f == 0) { assert(0); cout<<"-1\n"; return 0; }
			ans[v[(--help.end())->s].back()][(--help.end())->s]=1;
			v[(--help.end())->s].pop_back();
			// col2.ins(pi((--help.end())->f-1, (--help.end())->s));
			pi x=*(--help.end());
          	help.erase((--help.end()));
          	x.f --;
          	help.ins(x);
		}
		// for(auto i:col2) help.ins(i);
	}
	if(help.size() && (--help.end())->f > 0) { assert(0); cout<<"-1\n"; return 0; }
	FOR(i,0,n-1) { ll add=0; FOR(j,0,m-1) add+=ans[i][j]; if(add ^ a) { cout<<"-1\n"; return 0; } }
	FOR(j,0,m-1) { ll add=0; FOR(i,0,n-1) add+=ans[i][j]; if(add ^ b) { cout<<"-1\n"; return 0; } }
	FOR(i,0,n-1) FOR(j,0,m-1) {
		cout<<ans[i][j];
		if(j==m-1) cout<<"\n";
	}
}