Coast Length
InterviewTime limit1sMemory limit256 MB
Count the total length of borders between land and sea connected to the outside of the grid, excluding enclosed lakes.
Problem
Soteholm is an island municipality that has to write an action plan for its greenhouse gas emissions. The residents read the IPCC report on climate change and decided that rising sea level is the effect that reaches their municipality hardest. They value their coast, so before they take a position they want to know whether the total length of that coast grows or shrinks. Height maps already told them which squares go under water, and what is left is measuring the coast.
The map of Soteholm is an grid. Each square has side length 1 km and is either water or land. Two squares are connected when they share an edge. Everything outside the map is sea. Sea is any water that reaches the outside of the map through water only. The coast is every border between land and sea, and you have to report its total length. The shore of a lake enclosed by land, and any island inside such a lake, is not part of the coast.

In the figure, gray squares are land and white squares are water, and the thick black line is the coast. The figure corresponds to the first example input.
Input
The first line contains two integers and separated by a space ().
Each of the next lines contains a string of length made of zeros and ones. A zero is water and a one is land.
Output
Print the total length of the coast in km as one integer on a single line.