問題一覧 > 通常問題

No.348 カゴメカゴメ

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 40
作問者 : moti
お気に入りにしたユーザー ProblemId : 795 / 自分の提出
問題文最終更新日: 2026-06-25 00:53:05
Kagome Kagome
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
yukicoder contest 132 (順位表) の他の問題:

Problem Statement

There is an $N×M$ grid. Each cell is either '.' or 'x'. When two 'x' cells share a vertex of a cell, they are said to be "linked".
Also, when a cycle of 3 or more cells can be formed by connecting 'x' cells through their 8-neighborhoods, it is called a ring.

Every 'x' is always linked with exactly 2 other 'x' cells, forming a ring. Each ring has a strength, defined as the number of 'x' cells that make it up.

For each ring, you can choose either to "use" it or "not use" it. The sum of the strengths of the used rings is the strength of the grid.
You cannot use both a ring and the ring immediately outside it.

For example, in the figures below, used rings are recolored red and unused rings are recolored blue.




In this case, the following 5 ways of using rings are allowed.



Find the maximum strength of the grid when the rings to use are chosen appropriately.

Input

$N$ $M$
$grid_0$
$grid_1$
...
$grid_{N-1}$

  • $1 ≤ N, M ≤ 10^3$
  • $grid_i$ $(0 ≤ i ≤ N-1)$ is a string with $M$ elements, consisting only of '.' and 'x'.
  • It is guaranteed that each "linkage" forms a unique ring (you may assume that it cannot be interpreted as multiple rings).
  • There is no 'x' that does not form part of a ring.

Output

Output the maximum strength of the grid.

Sample

Sample 1
Input
5 10
..........
..xx...xx.
.x..x.x..x
..xx...xx.
..........
Output
12

The two rings do not enclose each other. Each ring has strength 6, and the strength of the grid is 12.

Sample 2
Input
2 2
xx
x.
Output
3

Each 'x' is always linked with exactly 2 other 'x' cells, forming one ring.

Sample 3
Input
1 1
.
Output
0

There are no rings at all.

Sample 4
Input
12 23
..xxxxxxxxxxxxxxxxxxxx.
.x....................x
.x..xxxxxx...xxxxxxx..x
.x.x......x.x.......x.x
.x.x..xx..x.x..xx...x.x
.x.x.x..x.x.x.x..x..x.x
.x.x..xx..x.x.x..x.x..x
.x.x......x.x..xx..x..x
.x..xxxxxx...x....x...x
.x............xxxx....x
.x....................x
..xxxxxxxxxxxxxxxxxxxx.
Output
74


The figure below shows the case where used rings are colored red and unused rings are colored blue.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。