CF1344B Monopole Magnets

题目描述

**单极磁铁**,顾名思义,就是只有一个磁极(N 或 S)的磁铁,~~他们在实际生活中是不存在的,不过……不要在意那些细节。~~ 我们有一个 $n$ 行 $m$ 列的方格图,要在里面放一些单极磁铁,你可以随便放置,甚至把多个磁铁放在同一个格子里。 单极磁铁的**吸引**是这么定义的:任选一个 N 极磁铁和一个 S 极磁铁,如果它们两个**恰好处于同一行或同一列中**,那 N 极磁铁会向**靠近 S 极磁铁**的方向**前进一格**。当然,如果它们两个处于同一个格子,什么也不会发生。注意 **S 极磁铁的位置是永远不会变化的**。 这个方格图里的每一个格子都涂成了**黑色**或**白色**。磁铁的放置必须要**遵守以下规则**: 1. 每行每列都必须至少有一个 S 极磁铁。 2. 对于每个黑色格子,必须有某个 N 极磁铁通过一系列的吸引操作经过它。 3. 对于每个白色格子,无论 N 极磁铁的位置怎样变化,都不能经过它。 现在我们想知道:要满足以上三条规则,至少要放几个 N 极磁铁。如果无论怎么放都不能满足规则,请输出 $-1$。

输入格式

输出格式

说明/提示

In the first test, here is an example placement of magnets: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/64081a77e42ae7a50ec16156938f7edaddb418dd.png)In the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule $ 3 $ since we can move the north magnet down onto a white square. The second example violates rule $ 2 $ since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule $ 1 $ since there is no south magnet in the first column. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/74342bffb2ed25274f81d33fb996a7cec774a68f.png)In the third test, here is an example placement of magnets. We can show that there is no required placement of magnets with fewer north magnets. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/0515bcb41bc9913bf5f95900134aa4f65b7e2af6.png)In the fourth test, we can show that no required placement of magnets exists. Here are two example placements that fail to meet the requirements. The first example violates rule $ 1 $ since there is no south magnet in the first row. The second example violates rules $ 1 $ and $ 3 $ since there is no south magnet in the second row and we can move the north magnet up one unit onto a white square. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/72d166e7967b6aa45c82ffda8c7e527d7a3ecbc3.png)In the fifth test, we can put the south magnet in each cell and no north magnets. Because there are no black cells, it will be a correct placement.