#K73727. Island Perimeter Calculation

    ID: 34040 Type: Default 1000ms 256MiB

Island Perimeter Calculation

Island Perimeter Calculation

You are given a 2D grid of size n × m where each cell is either 1 (land) or 0 (water). There is exactly one island (or possibly no land cells, in which case the perimeter is 0) in the grid, and the island does not contain any lakes (water inside that isn’t connected to the water surrounding the island). The island is formed by connecting adjacent lands horizontally or vertically.

Your task is to compute the perimeter of the island. The perimeter is defined as the total length of the island's boundary. Mathematically, if we denote by L the number of land cells and by A the number of adjacent pairs of land cells (each pair sharing a common edge), then the perimeter P is computed as:

$$P = 4\times L - 2\times A$$

Read the grid from standard input and output the computed perimeter to standard output.

inputFormat

The first line contains two integers, n and m, representing the number of rows and columns in the grid respectively. This is followed by n lines, each containing m space-separated integers (each either 0 or 1) representing the grid.

outputFormat

Output a single integer which is the perimeter of the island.

## sample
4 4
0 1 0 0
1 1 1 0
0 1 0 0
1 1 0 0
16