#abc315d. D - Magical Cookies

D - Magical Cookies

Score : 400400 points

问题描述

H×WH \times W 块饼干排列成 HH 行和 WW 列。 从上到下第 ii 行、从左到右第 jj 列的饼干的颜色用小写英文字母 ci,jc_{i,j} 表示。

我们将执行以下步骤:

  1. 对于每一行,执行以下操作:若该行剩余两个或更多饼干且它们颜色相同,则标记这些饼干。
  2. 对于每一列,执行以下操作:若该列剩余两个或更多饼干且它们颜色相同,则标记这些饼干。
  3. 若存在被标记的饼干,移除所有被标记的饼干并返回步骤 1;否则,终止该过程。

请找出该过程结束后剩余的饼干数量。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are H×WH \times W cookies in HH rows and WW columns.
The color of the cookie at the ii-row from the top and jj-th column from the left is represented by a lowercase English letter ci,jc_{i,j}.

We will perform the following procedure.

1. For each row, perform the following operation: if there are two or more cookies remaining in the row and they all have the same color, mark them.

2. For each column, perform the following operation: if there are two or more cookies remaining in the column and they all have the same color, mark them.

3. If there are any marked cookies, remove them all and return to 1; otherwise, terminate the procedure.

Find the number of cookies remaining at the end of the procedure.

Constraints

  • 2H,W20002 \leq H, W \leq 2000
  • ci,jc_{i,j} is a lowercase English letter.

Input

The input is given from Standard Input in the following format:

HH WW

c1,1c_{1,1}c1,2c_{1,2} \ldots c1,Wc_{1,W}

c2,1c_{2,1}c2,2c_{2,2} \ldots c2,Wc_{2,W}

\vdots

cH,1c_{H,1}cH,2c_{H,2} \ldots cH,Wc_{H,W}

Output

Print the answer.

Sample Input 1

4 3
aaa
aaa
abc
abd

Sample Output 1

2

The procedure is performed as follows.

  • 1. Mark the cookies in the first and second rows.
  • 2. Mark the cookies in the first column.
  • 3. Remove the marked cookies.

At this point, the cookies look like the following, where . indicates a position where the cookie has been removed.

...
...
.bc
.bd
  • 1. Do nothing.
  • 2. Mark the cookies in the second column.
  • 3. Remove the marked cookies.

At this point, the cookies look like the following, where . indicates a position where the cookie has been removed.

...
...
..c
..d
  • 1. Do nothing.
  • 2. Do nothing.
  • 3. No cookies are marked, so terminate the procedure.

The final number of cookies remaining is 22.

Sample Input 2

2 5
aaaaa
abcde

Sample Output 2

4

Sample Input 3

3 3
ooo
ooo
ooo

Sample Output 3

0

update @ 2024/3/10 09:00:46