#P2372. yyy2015c01挑战算周长

yyy2015c01挑战算周长

Description

The amusement park was crowded, and every dance machine was surrounded by people. What to do? With so many people, it would take a long time to play. yyy2015c01 looked around and suddenly found a dance machine with no one in front of it. “Haha, I found one with no queue, hurry up...”. But when she got there, she realized this new machine played differently: many X marks appeared at random positions on the floor grid. When you step on a cell, you must first compute the perimeter of the target it belongs to according to the rules, then input the correct perimeter into the machine. The final winner can receive a free game coupon from the amusement park. yyy2015c01 was tempted. Kids, can you help yyy2015c01 win the coupon?

Game rules:

There are many targets on the new dance machine’s rectangular grid. A dot . means an empty place; an uppercase X means part of a target.

Each X stands for one full grid-square region, including its boundary and the target itself. An X is adjacent to any X in any of the 8 directions (sharing an edge or a corner). Any two adjacent X squares have overlapping boundaries or corners, so their square regions are considered adjacent. A target is a connected component formed by a chain of adjacent X squares under 8-direction adjacency.

yyy2015c01 always steps on an X, selecting the target that contains that X. Record the coordinates where she steps. Row and column indices start from the top-left corner, numbered from 1 1 . A useful statistic is the target’s perimeter. Assume each X square has side length 1 1 .

The target will not contain any completely enclosed holes.

Description

Input Format

There are M+1 M+1 lines in total. The first line contains four positive integers M,N,X,Y M, N, X, Y (separated by spaces), meaning the grid has M M rows and N N columns, and the stepped-on cell is at row X X , column Y Y . The next M M lines consist of characters . and X.

Output Format

Output a single line with the perimeter of the selected target.

2 2 2 2 
XX
XX
8
6 4 2 3 
.XXX
.XXX
.XXX
...X
..X.
X...
18

Hint

1M20 1 \leq M \leq 20 , 1N20 1 \leq N \leq 20 , 1XM 1 \leq X \leq M , 1YN 1 \leq Y \leq N .

Translated by ChatGPT 5