cswork / OAOpen practice workspace

MicrosoftOA #33

Island Perimeter (Round 3, LC 463 :)

Estimated EasyJudge-ready

Topics (AI-labelled)Grid Search

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.

Input

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

Output a single integer, the answer.

Example 1

Input:

1 1
1

Output:

4

Example 2

Input:

2 2
1 1
1 1

Output:

8

Example 3

Input:

2 2
1 0
1 1

Output:

8

Example notes

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.

Editorial

Loading solution…