#P2372. Dance Dance Revolution: Target Perimeter

    ID: 15644 Type: Default 1000ms 256MiB

Dance Dance Revolution: Target Perimeter

Dance Dance Revolution: Target Perimeter

In a crowded amusement park, the new dancing machine features a unique challenge. Its stepping pad is a rectangular grid with cells marked by either a dot ('.') representing an empty space or an uppercase X marking part of a target.

When a player steps on an X, that cell and any X that is connected to it (considering all 8 directions of connectivity) form a target. Each X represents a full square with 4 edges. The target's perimeter is defined as the sum of the lengths of all the sides of squares that do not border another square in the target. In other words, for each cell in the connected target, each of its 4 sides (up, down, left, right) contributes 1 to the perimeter if the adjacent cell is either out of bounds or not part of the target.

You are given the grid and the coordinates (row and column, 1-indexed) of the stepped cell (which is guaranteed to be an X). Your task is to compute the perimeter of the target that includes the stepped cell.

Note: The target does not contain any completely enclosed holes.

inputFormat

The input consists of:

  1. The first line contains two integers R and C, representing the number of rows and columns in the grid.
  2. The second line contains two integers r and c (1-indexed), the coordinates of the stepped cell.
  3. The next R lines each contain a string of C characters. Each character is either '.' (empty cell) or 'X' (a cell that is part of a target).

outputFormat

Output a single integer representing the perimeter of the target connected component that contains the stepped cell.

sample

4 4
1 1
XX..
XX..
....
....
8