#P1454. 圣诞夜的极光

圣诞夜的极光

Description

Santa Claus has returned to the Arctic Christmas Zone; it is almost 12 o'clock, which means the aurora show is about to begin. Here the aurora is not the natural polar lights but a man-made display directed by Santa.

Boom... fireworks go off (from Liuyang, the hometown of fireworks in China). Then comes the aurora show.

The man-made aurora is actually a series of n×mn \times m dot-matrix images in the sky. Each point has only two states: lit or unlit. The lit points together form beautiful pictures. Each picture consists of several (exactly ss) “patterns”. Santa defines a “pattern” strictly as follows: view lit points as vertices of a graph where two lit points are adjacent if their Manhattan distance is at most 22. For A(x1,y1)A(x_1, y_1) and B(x2,y2)B(x_2, y_2), the Manhattan distance between AA and BB is x1x2+y1y2|x_1 - x_2| + |y_1 - y_2|. A “pattern” is a connected component of this graph. The elves watch the aurora and count the number of patterns in each image, singing and dancing through a beautiful Christmas night. ^_^

Input Format

The first line contains two integers nn and mm.

The next nn lines each contain mm characters. In the ii-th line, the jj-th character is - if the point is unlit, or # if the point is lit. No other characters appear.

Output Format

Output one integer ss on a single line.

19 48
------------------------------------------------
---####-----#-----#----------------------####---
--######----#-----#---------------------######--
-########--#-#---#-#####--#-##-##---#--########-
-###--###--#-#---#-#----#-##-##--#--#--###--###-
-###--###--#--#-#--######-#--#---#-#---###--###-
-########--#--#-#--#------#--#----##---########-
--######---#---#---######-#--#-----#----######--
---####----------------------------#-----####---
----------------------------------#-------------
------------------------------------------------
---###--#--------#------#-----------------------
--#---#-#---------------#-----------------------
-#------#-##--#-##--##-###-#-##-###--###-#--##--
-#------##--#-##-#-#----#--##--#---##---##-#----
-#------#---#-#--#--#---#--#---#---##----#--#---
--#---#-#---#-#--#---#--#--#---#---##---##---#--
---###--#---#-#--#-##---#--#---#---#-###-#-##---
------------------------------------------------
4

Hint

Constraints: 1n,m1001 \le n, m \le 100.

Hint: DFS.

Translated by ChatGPT 5