This page is still under construction.

Parts of this page are still being built. What you see may change.

Illumination

Time limit1sMemory limit256 MB

Summary
Count the hexagon edges of occupied cells that border open space reachable from outside the map, excluding walls around enclosed courtyards.
Level

Medium6 of 10

Topics
BFS, Graph, Implementation, Simulation
Solved
No attempts yet

Problem

Sanggeun's house is a single building made of many regular hexagons, each with a side length of 1 metre, packed together like a honeycomb. For Christmas he wants to hang lights only on the walls that are visible from outside the building, because lighting walls that cannot be seen from the outside would be a waste.

The building sits on a hexagonal grid. Each cell is either occupied by the building or empty. A wall that can be lit is one of the six edges of an occupied cell that touches outside space. The area outside the map is open space you can move through freely, but you cannot pass between two occupied cells that share an edge. Consequently, walls that face an empty region fully enclosed by the building (a courtyard) and therefore not connected to the outside are NOT lit.

Given the building layout, write a program that computes the total length of the walls to be lit. Each edge of a hexagon is 1 metre long.

Input

The first line contains two integers WW and HH. (1≤W,H≤1001 \le W, H \le 100)

Each of the next HH lines describes the layout. The ii-th line from the top contains WW integers separated by spaces; the jj-th integer on that line describes the cell at coordinate (j,i)(j, i). A value of 11 means the cell is occupied by the building and 00 means it is empty. At least one cell is occupied.

Coordinates on the hexagonal grid follow these rules.

  • The top-left hexagon has coordinate (1,1)(1, 1).
  • The hexagon to the right of (x,y)(x, y) has coordinate (x+1,y)(x+1, y).
  • When yy is odd, the hexagon directly below (x,y)(x, y) has coordinate (x,y+1)(x, y+1).
  • When yy is even, the hexagon to the lower-right of (x,y)(x, y) has coordinate (x,y+1)(x, y+1).

Output

Print the total length of the walls to be lit on a single line. Since every edge is 1 metre long, the answer equals the number of walls that touch the outside.

Examples2

  1. Example 1

    Input
    8 4
    0 1 0 1 0 1 1 1
    0 1 1 0 0 1 0 0
    1 0 1 0 1 1 1 1
    0 1 1 0 1 0 1 0
    
    Expected output
    64
    
  2. Example 2

    Input
    8 5
    0 1 1 1 0 1 1 1
    0 1 0 0 1 1 0 0
    1 0 0 1 1 1 1 1
    0 1 0 1 1 0 1 0
    0 1 1 0 1 1 0 0
    
    Expected output
    56