← 返回 snapchat 的题目列表Maximum Island Perimeter (Max over components)
类型:online_judge
Problem: Maximum Island Perimeter (Maximum over all components)
You are given a 2D grid grid consisting of 0s and 1s where:
1 represents land
0 represents water
An island is a connected component of land cells connected 4-directionally (up, down, left, right).
Return the maximum perimeter among all islands in the grid.
The perimeter of an island is defined as the total number of edges of land cells that are adjacent to water or outside the grid boundary.
Input Format
First line: two integers m n (rows and columns).
Next m lines: n integers separated by spaces describing grid.
Output Format
Output one integer: the maximum island perimeter.
Constraints
1 <= m, n <= 2000
grid[i][j] ∈ {0,1}
4-directional adjacency.
Examples
Example 1
Input:
4 4
0 1 0 0
1 1 1 0
0 1 0 0
1 1 0 0
Output:
12
Example 2
Input:
2 3
0 0 0
0 0 0
Output:
0
Example 3
Input:
1 1
1
Output:
4
Example
Input
4 4
0 1 0 0
1 1 1 0
0 1 0 0
1 1 0 0
Output
12