Height Map

Count the faces of the solid formed by grid columns of given heights, merging edge-adjacent unit squares on the same plane and direction into one face.

Medium7BFSSortingIntervalsGeometryNo attempts yetTime limit3sMemory limit256 MB

Problem

A height map is a two dimensional matrix of positive integers that describes a polyhedron. A cell holding the value VV carries a 1×1×V1 \times 1 \times V rectangular column that rests on one of its 1×11 \times 1 faces. All the downward facing 1×11 \times 1 faces together make the single bottom face of the solid, and further faces appear on the top and on the sides.

For instance, a 2×22 \times 2 matrix whose values are all 2 is a cube of side 2. If one of the values is 1, the solid is that same cube with one corner cut off. The picture below shows both shapes.

Not every polyhedron can be described this way, but many can. Here are some other examples.

Given a height map, count the faces of the solid it describes. Every height is an integer, so the surface of the solid splits into axis parallel unit squares. Two unit squares belong to the same face when they lie on the same plane, point outward in the same direction, and share a side. A face is a maximal set of unit squares linked by that relation. A face with a hole inside it still counts as one face. As the examples show, two different faces on the same plane may share a vertex, a whole side, or part of a side.

Input

The first line contains two integers RR and CC, the number of rows and the number of columns of the height map (1R,C1001 \le R, C \le 100). Each of the next RR lines contains CC integers. The jj-th integer on the ii-th line is the value Vi,jV_{i,j} in row ii and column jj (1Vi,j1091 \le V_{i,j} \le 10^9).

Output

Print one line with the number of faces of the polyhedron described by the height map.