Given three numbers $n, a, b$. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to $a$, and the number of components in its complement is $b$. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.

In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.

The adjacency matrix of an undirected graph is a square matrix of size $n$ consisting only of "0" and "1", where $n$ is the number of vertices of the graph and the $i$-th row and the $i$-th column correspond to the $i$-th vertex of the graph. The cell $(i,j)$ of the adjacency matrix contains $1$ if and only if the $i$-th and $j$-th vertices in the graph are connected by an edge.

A connected component is a set of vertices $X$ such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to $X$ violates this rule.

The complement or inverse of a graph $G$ is a graph $H$ on the same vertices such that two distinct vertices of $H$ are adjacent if and only if they are not adjacent in $G$.


-----Input-----

In a single line, three numbers are given $n, a, b \,(1 \le n \le 1000, 1 \le a, b \le n)$: is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement. 


-----Output-----

If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).

Otherwise, on the first line, print "YES"(without quotes). In each of the next $n$ lines, output $n$ digits such that $j$-th digit of $i$-th line must be $1$ if and only if there is an edge between vertices $i$ and $j$ in $G$ (and $0$ otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes. 

If there are several matrices that satisfy the conditions — output any of them.


-----Examples-----
Input
3 1 2

Output
YES
001
001
110

Input
3 3 3

Output
NO

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Graph algorithms

Implementation

Constructive algorithms


Solutions:
import sys
(n, a, b) = list(map(int, input().split()))
if a > 1 and b > 1:
	print('NO')
	sys.exit(0)
if n == 3 and a == 1 and (b == 1):
	print('NO')
	sys.exit(0)
if n == 2 and a == 1 and (b == 1):
	print('NO')
	sys.exit(0)
t = [[0 for i in range(n)] for j in range(n)]
comp = max(a, b)
for i in range(comp - 1, n - 1):
	t[i][i + 1] = 1
	t[i + 1][i] = 1
if b > 1:
	for i in range(n):
		for j in range(n):
			if i != j:
				t[i][j] = 1 - t[i][j]
print('YES')
for i in range(n):
	print(''.join(map(str, t[i])))


def generate_matrix(n):
	result = [[0 for x in range(n)] for y in range(n)]
	return result

def generate_m_component_matrix(n, comp):
	result = generate_matrix(n)
	for i in range(0, n - comp + 1):
		for j in range(0, n - comp + 1):
			result[i][j] = 0 if i == j else 1
	return result

def invert_matrix(m, result):
	for i in range(0, m):
		for j in range(0, m):
			result[i][j] = 1 if result[i][j] == 0 and i != j else 0
	return result

def generate_one_component_graph(n):
	result = generate_matrix(n)
	for i in range(1, n):
		result[i][i - 1] = result[i - 1][i] = 1
	return result

def print_matrix(n, matrix):
	for i in range(0, n):
		s = ''.join((str(e) for e in matrix[i]))
		print(s)
(n, a, b) = map(int, input().split())
if a > 1 and b > 1:
	print('NO')
elif a > 1:
	matrix = generate_m_component_matrix(n, a)
	print('YES')
	print_matrix(n, matrix)
elif b > 1:
	matrix = generate_m_component_matrix(n, b)
	matrix = invert_matrix(n, matrix)
	print('YES')
	print_matrix(n, matrix)
elif n > 3 or n == 1:
	matrix = generate_one_component_graph(n)
	print('YES')
	print_matrix(n, matrix)
else:
	print('NO')


(n, a, b) = map(int, input().split())
if min(a, b) > 1 or (1 < n < 4 and max(a, b) == 1):
	print('NO')
	exit()
print('YES')
f = int(a == 1)
g = [a, b][f]
r = [[f] * n for i in range(n)]
for i in range(n):
	r[i][i] = 0
for i in range(n - g):
	r[i][i + 1] ^= 1
	r[i + 1][i] ^= 1
print('\n'.join(map(lambda x: ''.join(map(str, x)), r)))


import collections
from functools import cmp_to_key
import sys

def getIntList():
	return list(map(int, input().split()))
(n, a, b) = getIntList()
a0 = a
b0 = b
if a0 > b0:
	(a0, b0) = (b, a)
if n == 2 and (a0, b0) == (1, 1):
	print('NO')
	return
if n == 3 and (a0, b0) == (1, 1):
	print('NO')
	return
if a > 1 and b > 1:
	print('NO')
	return
mat = [['0' for y in range(n)] for x in range(n)]
mat1 = [['1' for y in range(n)] for x in range(n)]
if b == 1:
	for x in range(n - a):
		mat[x][x + 1] = '1'
		mat[x + 1][x] = '1'
else:
	mat = mat1
	for x in range(n):
		mat[x][x] = '0'
	for x in range(n - b):
		mat[x][x + 1] = '0'
		mat[x + 1][x] = '0'
print('YES')
for x in range(n):
	print(''.join(mat[x]))


(n, a, b) = map(int, input().split())
if min(a, b) > 1 or (n, a, b) in ((2, 1, 1), (3, 1, 1)):
	print('NO')
	exit()
res = [[0] * n for _ in range(n)]
for i in range(0, n - max(a, b)):
	res[i][i + 1] = res[i + 1][i] = 1
if a == 1:
	res = [[e ^ 1 for e in l] for l in res]
print('YES')
for i in range(n):
	res[i][i] = 0
	print(*res[i], sep='')


(n, a, b) = map(int, input().split())
if a > 1 and b > 1 or (a * b == 1 and 1 < n < 4):
	print('NO')
else:
	M = [[str(int(a < b))] * n for _ in range(n)]
	for i in range(n):
		M[i][i] = '0'
	for i in range(n - a * b):
		M[i][i + 1] = M[i + 1][i] = str(int(a >= b))
	print('YES\n' + '\n'.join(map(''.join, M)))


(n, a, b) = map(int, input().split())
if a > 1 and b > 1:
	print('NO')
	exit()
if 2 <= n <= 3 and a == b == 1:
	print('NO')
	exit()
print('YES')
if b == 1:
	adj = [[0] * n for i in range(n)]
	conn = n
	for i in range(n):
		if conn == a:
			break
		adj[i][i + 1] = adj[i + 1][i] = 1
		conn -= 1
		if conn == a:
			break
elif a == 1:
	adj = [[1] * n for i in range(n)]
	conn = n
	for i in range(n):
		adj[i][i] = 0
	for i in range(n):
		if conn == b:
			break
		adj[i][i + 1] = adj[i + 1][i] = 0
		conn -= 1
		if conn == b:
			break
for row in adj:
	print(*row, sep='')


(n, a, b) = [int(x) for x in input().split()]
if min(a, b) > 1 or (n, a, b) in ((2, 1, 1), (3, 1, 1)):
	print('NO')
else:
	print('YES')
	matrix = [[0] * n for i in range(n)]
	for i in range(0, n - max(a, b)):
		matrix[i][i + 1] = 1
		matrix[i + 1][i] = 1
	if a == 1:
		for i in range(n):
			for j in range(n):
				if i != j:
					if matrix[i][j] == 1:
						matrix[i][j] = 0
					else:
						matrix[i][j] = 1
	for i in range(n):
		print(*matrix[i], sep='')


def solve(n, a, b):
	if min(a, b) != 1 or (a == b == 1 and (n == 2 or n == 3)):
		print('NO')
		return
	print('YES')
	edges = 0
	if a > 1:
		X = '1'
		O = '0'
		edges = n - a
	else:
		X = '0'
		O = '1'
		edges = n - b
	line = '0' + (O, X)[edges > 0] * (n > 1) + O * (n - 2)
	print(line)
	for y in range(1, n):
		line = O * (y - 1) + (O, X)[y <= edges] + '0'
		if y < n - 1:
			line += (O, X)[y < edges] + O * (n - y - 2)
		print(line)
(n, a, b) = map(int, input().split())
solve(n, a, b)


(n, a, b) = map(int, input().strip().split())
if min(a, b) > 1:
	print('NO')
	exit(0)
M = [[0] * n for _ in range(n)]
if a == 1 and b == 1:
	if n == 1:
		print('YES')
		print('0')
		exit(0)
	if n == 2 or n == 3:
		print('NO')
		exit(0)
	for i in range(1, n):
		M[i - 1][i] = 1
		M[i][i - 1] = 1
else:
	s = n - max(a, b) + 1
	for i in range(s):
		for j in range(s):
			if i != j:
				M[i][j] = 1
	if a == 1:
		for i in range(n):
			for j in range(n):
				if i != j:
					M[i][j] = 1 - M[i][j]
print('YES')
for i in range(n):
	print(''.join(map(str, M[i])))


(n, a, b) = map(int, input().split())
if min(a, b) > 1 or (1 < n < 4 and max(a, b) == 1):
	print('NO')
	exit()
print('YES')
f = int(a == 1)
g = [a, b][f]
r = [[f] * n for i in range(n)]
for i in range(n):
	r[i][i] = 0
for i in range(n - g):
	r[i][i + 1] ^= 1
	r[i + 1][i] ^= 1
for x in r:
	print(*x, sep='')
johnny = 0


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(a, b, c) = map(int, input().split())
n = a
adj = [[0 for i in range(n)] for j in range(n)]
if a == 2 and b == 1 and (c == 1):
	print('NO')
elif a == 3 and b == 1 and (c == 1):
	print('NO')
elif a == 1 and b == 1 and (c == 1):
	print('YES')
	print(0)
elif b == 1 and c == 1:
	for i in range(n - 1):
		adj[i][i + 1] = 1
		adj[i + 1][i] = 1
	print('YES')
	for i in range(n):
		print(*adj[i], sep='')
elif b == 1:
	for i in range(1, c):
		for j in range(i + 1, n + 1):
			adj[i - 1][j - 1] = 1
			adj[j - 1][i - 1] = 1
	print('YES')
	for i in range(n):
		print(*adj[i], sep='')
else:
	if c != 1:
		print('NO')
		sys.exit(0)
	for i in range(b, n + 1):
		for j in range(b, n + 1):
			if i != j:
				adj[i - 1][j - 1] = 1
				adj[j - 1][i - 1] = 1
	print('YES')
	for i in range(n):
		print(*adj[i], sep='')


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, a, b) = mi()
c = max(a, b)
if a != 1 and b != 1:
	print('NO')
elif n == 2 and c == 1:
	print('NO')
elif n == 3 and c == 1:
	print('NO')
else:
	if a == 1:
		g = [[1] * n for i in range(n)]
		for i in range(n):
			g[i][i] = 0
		for i in range(c - 1, n - 1):
			g[i][i + 1] = g[i + 1][i] = 0
	else:
		g = [[0] * n for i in range(n)]
		for i in range(c - 1, n - 1):
			g[i][i + 1] = g[i + 1][i] = 1
	print('YES')
	for r in g:
		print(''.join((str(x) for x in r)))


(n, a, b) = map(int, input().split())
if a != 1 and b != 1:
	print('NO')
else:
	con_char = '1'
	discon_char = '0'
	if a == 1:
		con_char = '0'
		discon_char = '1'
		t = a
		a = b
		b = t
	if a > 1:
		print('YES')
		n_con = n - a + 1
		for i in range(n):
			res = []
			for j in range(n):
				if i == j:
					res.append('0')
				elif i < n_con and j < n_con:
					res.append(con_char)
				else:
					res.append(discon_char)
			print(''.join(res))
	elif n == 1 or n > 3:
		print('YES')
		for i in range(n):
			res = []
			for j in range(n):
				if i == j:
					res.append('0')
				elif abs(i - j) == 1:
					res.append('1')
				else:
					res.append('0')
			print(''.join(res))
	else:
		print('NO')


(n, a, b) = map(int, input().split())
(z, o) = ('01', '10')[a < b]
n *= not (a > 1 < b or 1 < n * a * b < 4)
l = [[z] * n for _ in range(n)]
for i in range(n):
	l[i][i] = '0'
for i in range(n - a * b):
	l[i][i + 1] = l[i + 1][i] = o
print(('YES', 'NO')[not n])
print('\n'.join(map(''.join, l)))


(n, a, b) = list(map(int, input().split()))
if min(a, b) > 1:
	print('NO')
	exit()
m = max(a, b)
if m == 1:
	if n == 1:
		print('YES')
		print(0)
		exit()
	elif n < 4:
		print('NO')
		exit()
	else:
		print('YES')
		for row in range(n):
			line = ['0'] * n
			if row > 0:
				line[row - 1] = '1'
			if row < n - 1:
				line[row + 1] = '1'
			print(''.join(line))
	exit()
print('YES')
if a == 1:
	c = '1'
	d = '0'
else:
	c = '0'
	d = '1'
for row in range(n):
	if row < m - 1:
		line = [c] * n
	else:
		line = [c] * (m - 1) + [d] * (n - m + 1)
	line[row] = '0'
	print(''.join(line))


(n, a, b) = map(int, input().split())
if (n == 3 or n == 2) and (a == 1 and b == 1):
	print('NO')
	exit(0)
g = [[0 for i in range(n)] for j in range(n)]
if a > 1 and b == 1:
	for i in range(n - a - 1, -1, -1):
		g[i][i + 1] = g[i + 1][i] = 1
elif b > 1 and a == 1:
	(a, b) = (b, a)
	for i in range(n - a - 1, -1, -1):
		g[i][i + 1] = g[i + 1][i] = 1
	for i in range(n):
		for j in range(n):
			if g[i][j] == 0:
				g[i][j] = 1
			elif g[i][j] == 1:
				g[i][j] = 0
	for i in range(n):
		g[i][i] = 0
elif a == 1 and b == 1:
	for i in range(n - 1):
		g[i][i + 1] = g[i + 1][i] = 1
elif a > 1 and b > 1:
	print('NO')
	exit(0)
print('YES')
for i in range(n):
	for j in range(n):
		print(g[i][j], end='')
	print()


import math

def getIntList():
	return list(map(int, input().split()))

def getTransIntList(n):
	first = getIntList()
	m = len(first)
	result = [[0] * n for _ in range(m)]
	for i in range(m):
		result[i][0] = first[i]
	for j in range(1, n):
		curr = getIntList()
		for i in range(m):
			result[i][j] = curr[i]
	return result
(n, a, b) = getIntList()
if a > 1 and b > 1:
	print('NO')
elif a == b == 1 and (n == 2 or n == 3):
	print('NO')
else:
	c = max(a, b)
	m = [[0] * n for _ in range(n)]
	for i in range(n - c):
		m[i][i + 1] = 1
		m[i + 1][i] = 1
	if b > 1:
		for i in range(n):
			for j in range(n):
				if i != j:
					m[i][j] = 1 - m[i][j]
	print('YES')
	for i in range(n):
		print(''.join(map(str, m[i])))


def i_ints():
	return list(map(int, input().split()))
(n, a, b) = i_ints()

def calc():
	if min(a, b) != 1:
		print('NO')
		return
	if a == b == 1 and n in (2, 3):
		print('NO')
		return
	print('YES')
	(ONE, ZERO) = '10' if a > 1 else '01'
	edges = n - max(a, b)
	line = '0' + (ZERO, ONE)[edges > 0] * (n > 1) + ZERO * (n - 2)
	print(line)
	for y in range(1, n):
		line = ZERO * (y - 1) + (ZERO, ONE)[y <= edges] + '0'
		if y < n - 1:
			line += (ZERO, ONE)[y < edges] + ZERO * (n - y - 2)
		print(line)
calc()


(n, a, b) = list(map(int, input().split()))
d = []
for i in range(n):
	d.append(['1'] * n)
	d[i][i] = '0'
if [n, a, b] == [2, 1, 1]:
	print('NO')
elif [n, a, b] == [3, 1, 1]:
	print('NO')
elif a == 1:
	c = n - b
	for i in range(c):
		d[i][i + 1] = '0'
		d[i + 1][i] = '0'
	print('YES')
	for i in range(n):
		print(''.join(d[i]))
elif a != 1 and b != 1:
	print('NO')
else:
	print('YES')
	for i in range(a - 1):
		for j in range(n):
			d[i][j] = '0'
		for j in range(n):
			d[j][i] = '0'
	for i in range(n):
		print(''.join(d[i]))


import sys
from array import array
(n, a, b) = map(int, input().split())
if a != 1 and b != 1 or (2 <= n <= 3 and a == 1 and (b == 1)):
	print('NO')
	exit()
if a == 1:
	matrix = [[1] * n for _ in range(n)]
	for i in range(n):
		matrix[i][i] = 0
	x = n
	for i in range(n):
		if x == b:
			break
		x -= 1
		matrix[i][i + 1] = matrix[i + 1][i] = 0
else:
	matrix = [[0] * n for _ in range(n)]
	x = n
	for i in range(n):
		if x == a:
			break
		x -= 1
		matrix[i][i + 1] = matrix[i + 1][i] = 1
ans = 'YES\n' + '\n'.join((''.join(map(str, row)) for row in matrix))
sys.stdout.buffer.write(ans.encode('utf-8'))


def readline():
	return map(int, input().split())

def main():
	(n, a, b) = readline()
	if a > 1 and b > 1:
		print('NO')
		return
	if n in [2, 3] and a == 1 and (b == 1):
		print('NO')
		return
	matrix = [[i in [j + 1, j - 1] for i in range(n)] for j in range(n)]
	(a, b) = (n + 1 - a, n + 1 - b)
	if a != n:
		matrix = [[False for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				if i < a and j < a and (i != j):
					matrix[i][j] = True
	elif b != n:
		matrix = [[False for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				if i >= b or j >= b:
					matrix[i][j] = True
				if i == j:
					matrix[i][j] = False
	print('YES')
	for row in matrix:
		print(''.join(map(lambda x: '1' if x else '0', row)), flush=False)
main()


[n, a, b] = map(int, input().strip().split())

def show(n, a, tr=False):
	s0 = '0' * n
	if a == n:
		for i in range(n):
			if tr:
				print('1' * i + '0' + '1' * (n - i - 1))
			else:
				print(s0)
	else:
		for i in range(a - 1):
			if tr:
				print('1' * i + '0' + '1' * (n - i - 1))
			else:
				print(s0)
		if tr:
			print('1' * (a - 1) + '0' * 2 + '1' * (n - a - 1))
		else:
			print('0' * a + '1' + '0' * (n - a - 1))
		for i in range(n - a - 1):
			if tr:
				print('1' * (a - 1 + i) + '000' + '1' * (n - a - 2 - i))
			else:
				print('0' * (a - 1 + i) + '101' + '0' * (n - a - 2 - i))
		if tr:
			print('1' * (n - 2) + '00')
		else:
			print('0' * (n - 2) + '10')
if a > 1 and b > 1 or (n, a, b) in [(2, 1, 1), (3, 1, 1)]:
	print('NO')
else:
	print('YES')
	if n == 1:
		print('0')
	elif n == 2:
		if a == 1:
			print('01\n10')
		else:
			print('00\n00')
	elif a == 1:
		show(n, b, True)
	else:
		show(n, a)


(n, a, b) = map(int, input().split())
if a > n:
	print('NO')
	exit()
if b > n:
	print('NO')
	exit()
if a == 1 and b == 1:
	if n == 2 or n == 3:
		print('NO')
		exit()
if n == 1 and a > 1 or (n == 1 and b > 1):
	print('NO')
	exit()
if min(a, b) > 1:
	print('NO')
	exit()

def check(mat):
	vis = [0] * n
	cnt = 0
	for i in range(n):
		if vis[i] == 0:
			q = [i]
			cnt += 1
			vis[i] = 1
			while q:
				t = q.pop(0)
				for j in range(n):
					if mat[t][j] == 1 and vis[j] == 0:
						vis[j] = 1
						q.append(j)
		return cnt
mat = [[0 for i in range(n)] for j in range(n)]
m = max(a, b)
j = 1
for i in range(n):
	if j < n:
		mat[i][j] = 1
		mat[j][i] = 1
	j += 1
for i in range(m - 1):
	curr = n - i - 1
	for j in range(n):
		if mat[curr][j] == 1:
			mat[curr][j] = 0
			mat[j][curr] = 0
if b == 1:
	print('YES')
	for i in range(n):
		print(*mat[i], sep='')
else:
	print('YES')
	for i in range(n):
		for j in range(n):
			mat[i][j] = 1 - mat[i][j]
	for i in range(n):
		mat[i][i] = 0
	for i in range(n):
		print(*mat[i], sep='')


str = input().split()
(n, a, b) = [int(str[x]) for x in range(0, 3)]
inv = 0
if b != 1:
	t = a
	a = b
	b = t
	inv = 1
if b != 1 or (n > 1 and n < 4 and (a == 1) and (b == 1)):
	print('NO')
	exit()
print('YES')
f = [[0] * n for i in range(0, n)]
for i in range(0, n - a):
	f[i][i + 1] = f[i + 1][i] = 1
for i in range(0, n):
	cur = [chr(((f[i][j] ^ inv) & (i != j)) + 48) for j in range(0, n)]
	print(''.join(cur))


from collections import defaultdict
(n, a, b) = map(int, input().split())
hash = defaultdict(list)
if a == 1 and b == 1:
	if n == 2 or n == 3:
		print('NO')
		exit()
if a == 1 or b == 1:
	bool = [False] * (n + 1)
	if a > n or b > n:
		print('NO')
		exit()
	print('YES')
	l = []
	for i in range(n):
		z = ['0'] * n
		l.append(z)
	ans = []
	for i in range(n):
		z = ['0'] * n
		ans.append(z)
	if b == 1:
		for i in range(a - 1, n - 1):
			l[i][i + 1] = '1'
			l[i + 1][i] = '1'
		for i in l:
			print(''.join(i))
	else:
		ans = []
		for i in range(n):
			z = ['0'] * n
			ans.append(z)
		for i in range(b - 1, n - 1):
			l[i][i + 1] = '1'
			l[i + 1][i] = '1'
		for i in range(n):
			for j in range(n):
				if i != j:
					if l[i][j] == '1':
						ans[i][j] = '0'
					if l[i][j] == '0':
						ans[i][j] = '1'
		for i in ans:
			print(''.join(i))
else:
	print('NO')


def read():
	return int(input())

def readlist():
	return list(map(int, input().split()))

def readmap():
	return map(int, input().split())
(N, A, B) = readmap()
if N == 1:
	print('YES')
	print(0)
elif N == 2:
	if A == 1 and B == 2:
		print('YES')
		print('01')
		print('10')
	elif A == 2 and B == 1:
		print('YES')
		print('00')
		print('00')
	else:
		print('NO')
elif N == 3:
	if A == 1 and B == 2:
		print('YES')
		print('011')
		print('100')
		print('100')
	elif A == 2 and B == 1:
		print('YES')
		print('001')
		print('000')
		print('100')
	elif A == 1 and B == 3:
		print('YES')
		print('011')
		print('101')
		print('110')
	elif A == 3 and B == 1:
		print('YES')
		print('000')
		print('000')
		print('000')
	else:
		print('NO')
elif A != 1 and B != 1:
	print('NO')
else:
	print('YES')
	if B == 1 and A != 1:
		mat = []
		for i in range(N):
			vec = []
			if i == 0:
				for j in range(N):
					if j >= A:
						vec.append(1)
					else:
						vec.append(0)
				mat.append(vec)
			else:
				vec = [0] * N
				if i >= A:
					vec[0] = 1
				mat.append(vec)
		for n in range(N):
			print(''.join(list(map(str, mat[n]))))
	elif A == 1 and B != 1:
		mat = []
		for i in range(N):
			vec = []
			if i == 0:
				for j in range(N):
					if j >= B:
						vec.append(0)
					else:
						vec.append(1)
				vec[i] = 0
				mat.append(vec)
			else:
				vec = [1] * N
				if i >= B:
					vec[0] = 0
				vec[i] = 0
				mat.append(vec)
		for n in range(N):
			print(''.join(list(map(str, mat[n]))))
	else:
		mat = []
		for i in range(N):
			vec = []
			if i == 0:
				for j in range(N):
					if j >= 2:
						vec.append(1)
					else:
						vec.append(0)
				mat.append(vec)
			else:
				vec = [0] * N
				if i >= 2:
					vec[0] = 1
				mat.append(vec)
		mat[1][2] = 1
		mat[2][1] = 1
		for n in range(N):
			print(''.join(list(map(str, mat[n]))))


def get_sign_1(fo):

	def res(s, f=fo):
		if f ** 2 + s ** 2 == 2 * f * s + 1:
			return '1'
		else:
			return '0'
	return res

def get_signs_2(cf, rev):
	cf -= 1
	if rev:

		def res(fo, cff=cf):
			if fo >= cff:

				def res2(s, f=fo):
					if s == f:
						return '0'
					elif s >= cff:
						return '0'
					else:
						return '1'
			else:

				def res2(s, f=fo):
					if s == f:
						return '0'
					else:
						return '1'
			return res2
	else:

		def res(fo, cff=cf):
			if fo >= cff:

				def res2(s, f=fo):
					if s == f:
						return '0'
					elif s >= cff:
						return '1'
					else:
						return '0'
			else:

				def res2(s):
					return '0'
			return res2
	return res
(n, a, b) = map(int, input().split())
c = a * b
if a + b == c + 1 and (c > 1 or n == 1 or n > 3):
	print('YES')
	if c == 1:
		get_sign_f = get_sign_1
	else:
		get_sign_f = get_signs_2(c, c == b)
	for foo in range(n):
		print(''.join(map(get_sign_f(foo), range(n))))
else:
	print('NO')