All problems

Problem 422

Island Perimeter

Easy · 10 pts · Matrix, Simulation

Problem

A grid contains land ('1') and water ('0'). Each cell is a 1 x 1 square. Compute the total perimeter of all the land: count every side of a land cell that touches water or the outside of the grid. Sides shared by two land cells are inside the land and do not count. Each land cell contributes 4 sides minus one for every land neighbour.

Input

First line: R and C. Next R lines: C characters, each '0' or '1'.

Output

Print the total perimeter of the land.

Constraints

1 <= R, C <= 100

Example input

4 4
0100
1110
0100
1100

Example output

16

Explanation

The island 0100 / 1110 / 0100 / 1100 has 7 land cells, and 16 of their sides touch water or the edge of the grid, so the perimeter is 16.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Enclaves Next: Shortest Bridge