The Shortest Fence
Limits: 1 sec., 128 MiB
Equestrian competitions ended at the Olympic Games in Tokyo. And while the athletes are exchanging contacts on social networks, the main groom of the Olympics has an important task — to make sure that the horses do not run away from the competition venue all over the island of Honshu.
The field on which the competition is held has the shape of a
rectangle with the length of L jō (unit of length) and the
width of W jō. The groom has already divided the field into
1x1 jō cells, which are parallel to the sides of the field. Also, he
found locations of all the horses using a drone and marked which cells
are occupied by horses. Now he wants to build a solid closed fence that
would fence off all the horses in the field so that they do not run
away. The groom will build a fence on the sides of the cells of the
field. Help him to determine the minimal possible length of the fence.
Note that only one fence must be built which should not overlap and
should have no self-intersections.
It is guaranteed that at least one cell is occupied by horses.
Input
The first line contains two integers L and
W — the length and the width of the field.
The following L lines contain W characters
each. The i-th character of the j-th line is
# if cell (i,j) is occupied by horses, or
. if the cell is empty.
Output
Print one integer — the minimum length of the fence.
Constraints
\(1 \le L, W \le 100.\)
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 4 ..#. .##. .... .... | 8 |
| Input (stdin) | Output (stdout) |
|---|---|
| 4 3 .#. .#. ... ..# | 12 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|