PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000009. Cảm biến kết nối (Sensors)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cảm biến kết nối (Sensors)

Sensors

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a grid with hh rows and ww columns. A cell marked # contains a sensor; a cell marked . is empty. Two sensors are directly connected if their cells share a side or a corner. Connections may also pass through other sensors. Count the connected groups of sensors.

Input

The first line contains two integers h,wh,w. Each of the next hh lines is a string of ww characters, each either # or ., describing one row.

Output

Print the number of connected groups of sensors.

Subtasks

  • Subtask 1 (20 points): 1≤h≤121\le h\le 12; 1≤w≤121\le w\le 12; each cell contains # or ..
  • Subtask 2 (30 points): 1≤h≤1301\le h\le 130; 1≤w≤1301\le w\le 130; each cell contains # or ..
  • Subtask 3 (50 points): 1≤h≤10001\le h\le 1000; 1≤w≤10001\le w\le 1000; each cell contains # or ..

Examples

Input

5 6
.##...
...#..
....##
#.#...
..#...

Output

3

Explanation

The first group has cells (1,2),(1,3),(2,4),(3,5),(3,6); the second is (4,1), and the third is (4,3),(5,3). Hence 3 groups.