luogu#P4961. 小埋与扫雷

小埋与扫雷

Background

Umaru is always playing games at home. One day, she suddenly wanted to play the Minesweeper that comes with Windows. Her older brother saw this and remembered the days when he played Minesweeper in the computer lab during IT class when he was young, so he happily started teaching Umaru how to play. However, Umaru still did not understand how to compute 3bv\mathrm{3bv} (Bechtel's Board Benchmark Value, the minimum number of left clicks needed to open all non-mine cells in one game; see the tutorial on saolei.net), so she came to you.

Problem Description

Umaru will tell you a Minesweeper board, represented by an n×mn\times m matrix, where 11 means a mine and 00 means not a mine. Please tell her the 3bv\mathrm{3bv} of this board.

A cell that is not a mine and has no mines in the eight surrounding cells is called an "empty cell". A cell that is not a mine and has at least one mine in the eight surrounding cells is called a "number cell". An 8-connected component consisting of "empty cells" is called an "empty area". 3bv=\mathrm{3bv}= (the number of "number cells" that have no "empty cells" in their eight surrounding cells) ++ (the number of "empty areas").

If you cannot understand the computation above, you can read the tutorial given in the Background, or see the sample explanation below.

Note: 8-connectivity

Input Format

The first line contains two integers nn and mm, meaning the Minesweeper board is an n×mn \times m matrix.

The next nn lines each contain mm integers, describing the matrix. Each number is either 00 or 11. 11 means the cell is a mine, and 00 means it is not a mine.

Output Format

Output one integer, representing the 3bv\mathrm{3bv} of this Minesweeper board.

8 8
0 0 0 1 1 0 0 0 
1 0 0 1 0 0 0 1 
1 0 0 1 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 
13

Hint

1n, m10001\le n,\ m\le 1000

Translated by ChatGPT 5