1046 - BnB   

Description

Here is a well-known game, ‘ 彈水阿給’(=瘋狂阿給=爆爆王). This is very similar to Bomber Man. The range of explosion of each water-ball is a cross shape on the map, and it will trigger any water-ball on the path of that cross.

 

Illustrated by following pictures:

 

pastedGraphic.pdf, pastedGraphic_1.pdf

 

Now you are in this intense “Rising Green Pepper Contest”, trying to play this exciting game. We redefined the rule as following:

0. You are playing on an N * M map. There’s no obstacles on that map, only several  placed water-balls.

1. Assume that every water-ball have infinite explosion range, that is to say, it would explode until the bound of the map.

2. The horizontal explosion of the water-ball will trigger water-balls on the same row, while doing no damage.

3. The vertical explosion of the water-ball will NOT trigger water-ball on the same column, while bringing damage.

 

Now things you are trying to do is:

0. Choose several water-balls to make them explode, and the number of choosing water-balls should be a minimum.

1. The total DAMAGE range should cover the whole map.

2. We want to be eco-friendly, so the DAMAGE range cannot be overlapped, or we would waste water.

 

Input

There are several testcases.

The first line of each testcases indicates N, M, which represents an N * M map.

Following will be N lines, each consists of M number(0 or 1), representing the state of the map. 0 means null, while 1 means water-ball.

1 <= N,M <= 100

Output

Print a positive integer, the minimum number of your choose.

If the goal cannot be achieved, print -1.

Sample Input  Download

Sample Output  Download

Tags




Discuss