Sheba's Amoebas

Count the number of disjoint closed loops formed by black pixels on a grid, where each black pixel has exactly two black neighbors among its eight surrounding cells.

Medium4GraphDFSNo attempts yetTime limit2sMemory limit512 MB

Problem

After a successful crowdfunding campaign, Sheba Arriba has raised enough money for her mail-order biology supply company. Sheba's Amoebas ships Petri dishes already populated with a colony of those tiny one-celled organisms. Sheba needs to verify the number of amoebas her company sends out. For each dish she has a black-and-white image that has been pre-processed to show each amoeba as a simple closed loop of black pixels. A loop is a minimal set of black pixels in which each pixel is adjacent to exactly two other pixels in the set, where adjacent means sharing an edge or a corner of a pixel. All black pixels in the image belong to some loop.

Write a program that counts the closed loops in a rectangular array of black and white pixels. No two closed loops in the image touch or overlap. One cannibalistic species of amoeba surrounds and engulfs its neighbors, so there may be amoebas within amoebas. For instance, each of the images in Figure 1 contains four amoebas.

Figure 1: Two Petri dishes, each with four amoebas.

Input

The first line contains two integers mm and nn (1m,n1001 \le m, n \le 100).

Each of the next mm lines contains nn characters. A # denotes a black pixel and a . denotes a white pixel. For every black pixel, exactly two of its eight neighbors are also black.

Output

Print a single integer, the number of loops in the input.