Skip to main content
Back to problems
Codeforces
Medium
Arrays
Matrices
Greedy
OR in Matrix

Reconstruct a binary matrix from the matrix of OR-values over all rows and columns.

Acceptance 0%
Problem Statement

You are given an n×mn \times m matrix BB consisting of $0 and \1$.

Interpret BB as the result of the following process on an unknown binary matrix AA of the same size:

  • If B[i][j]=1B[i][j] = 1, then every cell in row ii and every cell in column jj of AA must be $1$.
  • If B[i][j]=0B[i][j] = 0, then there must be at least one cell equal to $0inrowin rowi and at least one cell equal to \0incolumnin columnj$.

Your task is to determine whether such a binary matrix AA exists. If it exists, output one valid matrix; otherwise, report that it is impossible.

This is the classic reconstruction task behind the "OR in Matrix" problem.

Input Format

  • The first line contains two integers nn and mm.
  • The next nn lines contain mm integers each, describing matrix BB.

The exact official limits are not repeated here; solve for a general n×mn \times m binary matrix.

Output Format

  • If no valid matrix exists, output NO.
  • Otherwise output YES followed by any valid n×mn \times m binary matrix $A`.

A valid matrix should satisfy all constraints implied by BB.

Constraints

  • 1n,m1 \le n, m
  • B[i][j]{0,1}B[i][j] \in \{0,1\}
  • Output any one valid solution if it exists.
Examples
Sample cases returned by the problem API.

Example 1

Input

2 2
1 1
1 1

Output

YES
1 1
1 1

Explanation

All entries in BB are 1, so the all-ones matrix is a valid reconstruction.

Example 2

Input

3 3
1 1 1
1 0 1
1 1 1

Output

NO

Explanation

The center cell is 0, so its row and column must each contain a 0. That would force additional zeros in BB, so this configuration cannot come from any valid binary matrix.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.