← 返回 bytedance 的题目列表Number of Islands and Island Perimeter
类型:online_judge
Problem: Number of Islands and Island Perimeter
Given an m x n 2D grid grid, where:
1 represents land;
0 represents water.
Two land cells belong to the same island if they are adjacent vertically or horizontally. The area outside the grid is considered water.
Compute:
The number of islands in the grid;
The total perimeter of all islands, i.e. the number of land edges adjacent to water or the grid boundary.
Input Format
The first line contains two integers m and n.
The next m lines describe the grid. Each row may be provided as either:
a continuous 0/1 string, such as 11000; or
space-separated digits, such as 1 1 0 0 0.
Output Format
Print two integers:
number_of_islands total_perimeter
Constraints
1 <= m, n <= 300
grid[i][j] is either 0 or 1
Example
Input:
4 5
11000
11000
00100
00011
Output:
3 16
Example
Input
4 5
11000
11000
00100
00011
Output
3 16