#P4961. 小埋与扫雷

小埋与扫雷

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