P28285 [洛谷] [SDOI2009] Cell Exploration
提高+
题目描述

Problem Description

In biology class, the teacher introduces cells to the students. To deepen their impression, the teacher defines a cell in an N×M matrix, which contains only the characters "#" and ".":

A cell consists of a nucleus, cytoplasm, and cell membrane. The nucleus is a 4-connected (up, down, left, right) block of "#" that must be solid, meaning it cannot contain a 4-connected block of "." completely surrounded by "#" (complete包围 means this "." block cannot be adjacent to the matrix boundary, and all its 4-adjacent cells are part of the "#" block containing it). The cell membrane is an 8-connected (up, down, left, right, and four diagonal directions) non-solid block of "#". The cell membrane surrounds only one 4-connected region, which contains exactly one nucleus, and the rest of the region is filled with ".".

All connected blocks must be maximized, meaning an 8-connected block cannot find a "#" that is 8-connected to any "#" in the block; similarly, a 4-connected block cannot find a "#" that is 4-connected to any "#" in the block.

Now, the teacher has drawn a picture and asks Xiao E to answer how many cells are in the picture and to change all "#" that do not belong to any cell into ".".

Input Format

The first line of the input file explore.in contains two positive integers N and M separated by spaces, representing the height and width of the matrix.

The following is an N-row and M-column matrix, containing only the characters "#" and ".", with no extra characters.

Output Format

The first line of the output file explore.out contains an integer, representing the number of cells in the input matrix.

The following is an N-row and M-column matrix, containing only the characters "#" and ".", representing the modified picture.

Sample Input and Output

Input Sample #1

12 13
.###..#####..
#...#.#....#.
#.#.#.#..#.#.
#...#..#...#.
.###.#..###..
....#..##...#
..........###
##########..#
#...........#
#.###...###.#
#...........#
#############

Output Sample #1

1
......#####..
......#....#.
......#..#.#.
.......#...#.
........###..
.......##....
.............
.............
.............
.............
.............
.............

Input Sample #2

9 14
#########.....
#.......#....#
#.#####.#...#.
#.#...#.#..#..
#.#.#.#.#.#..#
#.#...#.#..#..
#.#####.#...#.
#.......#....#
#########.....

Output Sample #2

1
..............
..............
..#####.......
..#...#.......
..#.#.#.......
..#...#.......
..#####.......
..............
..............

Input Sample #3

7 15
#######.#######
#.....#.#.....#
#.###.#.#.###.#
#.#.#.#.#.#...#
#.###.#.#.###.#
#.....#.#.....#
#######.#######

Output Sample #3

1
........#######
........#.....#
........#.###.#
........#.#...#
........#.###.#
........#.....#
........#######

Notes/Hints

For 20% of the data, it satisfies 1 ≤ N, M ≤ 20.

For another 20% of the data, all "#" belong to a correct cell.

For 100% of the data, it satisfies 1 ≤ N, M ≤ 1,000.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
12 13 .###..#####.. #...#.#....#. #.#.#.#..#.#. #...#..#...#. .###.#..###.. ....#..##...# ..........### ##########..# #...........# #.###...###.# #...........# #############
样例输出
1 ......#####.. ......#....#. ......#..#.#. .......#...#. ........###.. .......##.... ............. ............. ............. ............. ............. .............