LogiQuest

307. Island Perimeter

EasyGrid

The grid has exactly one island of 1s (connected horizontally/vertically). Print its perimeter.

Input format

Line 1: r c Next r lines: c values (0/1)

Output format

The perimeter.

Example 1

Input

4 4
0 1 0 0
1 1 1 0
0 1 0 0
1 1 0 0

Output

16

Example 2

Input

1 1
1

Output

4

Constraints

1 ≤ r, c ≤ 100

Read input from standard input and print the answer to standard output. Your code is judged against 3 test cases, most of them hidden.

Loading the MyLoginn IDE…