Fencing off the darkness

Given a grid of bulb strengths and a ceiling height, compute each square's light level, mark the dark ones, then find the cheapest set of interior squares that contains all dark squares and minimizes the perimeter cost.

Hard9GraphMinimum spanning treeMathImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

For health and safety reasons every night club now has to meet a minimum lighting standard. The city divides the club floor into squares of one metre by one metre and measures the light level with a probe at the centre of each square.

The club owner will not install new lighting. The owner will fence off the dark squares with transparent panels instead and deal with them later.

The lighting layout of the club is given as an R×CR \times C grid. Directly above the centre of square (r,c)(r, c) there is one bulb of strength ss, where ss is an integer from 0 to 9. If ss is 0 there is no bulb above that square. The ceiling is flat, so its height is HH everywhere.

Every bulb sends light to every square of the club. If the displacement from a bulb to a probe is (x,y,z)(x, y, z), the light level at that square rises by

sx2+y2+z2\frac{s}{x^2 + y^2 + z^2}

The light level of square (r,c)(r, c) is the sum of what every bulb adds to it. The displacement from the bulb above square (r,c)(r', c') to the probe of square (r,c)(r, c) is (rr,cc,H)(|r - r'|, |c - c'|, H). A square whose light level is at least BB meets the standard, and a square below BB is dark.

A panel of fencing is built on the shared edge of two adjacent squares. One panel costs 11 pounds when at least one of the two squares is dark. When both squares meet the standard the builders demand extra pay for working under the flashing club lights, so one panel costs 43 pounds.

You pick the set SS of squares to fence off. SS has to contain every dark square and cannot contain any border square. The border squares are the ones in the first row, the last row, the first column and the last column. One panel is built on every edge between a square in SS and an adjacent square outside SS. The outer wall of the club already exists and needs no panel. SS may come in several pieces, and it may enclose squares that meet the standard.

Find the smallest amount that fencing off every dark square can cost.

Input

  • The first line contains the minimum required light level BB. (0<B90 < B \le 9)
  • The second line contains the height of the ceiling HH. (0<H50 < H \le 5)
  • The third line contains the number of rows RR and the number of columns CC of the club, separated by a space. (0<R,C300 < R, C \le 30)
  • Each of the next RR lines contains a string of CC digits. The cc-th digit on the rr-th line is the strength of the bulb above square (r,c)(r, c).

Every value in the input is an integer. Every border square, that is every square in the first row, the last row, the first column and the last column, meets the standard.

Output

Print one integer, the smallest number of pounds that has to be spent on fencing.