MicrosoftOA #33
Island Perimeter (Round 3, LC 463 :)
估计简单可在线判题
考点(AI 标注)网格搜索
0 is water and 1 is land. Land cells connected 4-directionally form exactly one island with no lakes, surrounded by water. Each cell has side length 1. Find the perimeter of the island.
输入格式
The first line contains N M (1..100), followed by N lines each with M values 0/1. There is guaranteed to be exactly one island and no inner lakes.
输出格式
Output a single integer, the answer.
样例 1
输入:
1 1
1
输出:
4
样例 2
输入:
2 2
1 1
1 1
输出:
8
样例 3
输入:
2 2
1 0
1 1
输出:
8
样例说明
Example 1: a single cell has four sides, so the perimeter is 4.
Example 2: a solid 2×2 square has sides of length 2, so the perimeter is 8.
Example 3: three cells contribute 12, minus 4 for the two shared edges, giving a perimeter of 8.