Problems > Count the Lakes

Count the Lakes

Difficulty: 1400 4000 ms 128 MB

bfs graphs

The map has land (L) and water (W). Water cells touching side-by-side (not diagonally) form one lake. Count the lakes.

Input: the first line has N and M; the next N lines each have M characters.

Output: the number of lakes.

Constraints:

  • 1 ≤ N, M ≤ 500

Sample Tests

Input 1
3 4
LWWL
LLWL
WLLL
Output 1
2
Input 2
1 1
L
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.