cswork / OAOpen practice workspace

AmazonOA #339

Minimum Rooks in a Peaceful State (SDE 2, Phone Interview)

Estimated MediumJudge-ready

Topics (AI-labelled)Union-Find

On the board, 1 is a rook and 0 is an empty cell. A rook can only move along its row or column to capture another rook it meets; it cannot jump over other rooks and cannot move to an empty cell. Keep capturing, and find the minimum number of rooks left once no two of them attack each other.

Input

The first line contains the number of rows n and the number of columns m, followed by n lines of m values, each 0 or 1. The original bounds are 1≤n,m≤1000, and a completely empty board is allowed.

Output

Output a single integer, the answer.

Example 1

Input:

2 3
1 0 1
1 0 0

Output:

1

Example 2

Input:

3 3
1 0 0
0 1 0
0 0 1

Output:

3

Example 3

Input:

1 1
0

Output:

0

Example notes

The outputs of the three public examples are 1; 3; and 0, checked by independent enumeration or direct simulation.

Editorial

Loading solution…