#P7668. [JOI 2018 Final] 团子制作 / Dango Maker

[JOI 2018 Final] 团子制作 / Dango Maker

Description

The dumplings are placed on a grid of cells with NN rows and MM columns. Each cell contains one dumpling. The color of each dumpling is red (R\texttt{R}), green (G\texttt{G}), or white (W\texttt{W}).

You will choose three consecutive dumplings from the cells and skewer them onto one stick. The direction of the chosen three consecutive dumplings must be left to right, or top to bottom.

Now, you want to make dango sticks whose colors, in this order, are red, green, and white. You want to make as many dango sticks as possible. The order of dumplings on the stick must be the same as the order of the dumplings selected from the cells. A dumpling cannot be used on more than one stick.

Given the colors of the dumplings placed on the grid, write a program to compute the maximum number of dango sticks you can make. The colors must be red, green, white in this order.

Input Format

The first line of input contains two space-separated integers NN and MM. Each of the next NN lines contains a string of length MM, consisting of R\texttt{R}, G\texttt{G}, or W\texttt{W}. The jj-th character of this string is the color of the dumpling in the ii-th row from the top and the jj-th column from the left.

Output Format

Print one integer: the maximum number of dango sticks.

3 4
RGWR
GRGG
RGWW
3
4 4
RGWR
GRRG
WGGW
WWWR
4

Hint

Constraints

For 100%100\% of the testdata, 1N30001 \leq N \leq 3000, 1M30001 \leq M \leq 3000, 1iN1 \leq i \leq N, 1jM1 \leq j \leq M.

  • Subtask 11 (1313 points): N4N \leq 4, M4M \leq 4.
  • Subtask 22 (2020 points): N10N \leq 10, M10M \leq 10.
  • Subtask 33 (6767 points): no additional constraints.

Sample Explanation

For Sample 11: You can make 33 dango sticks.

  • You choose three consecutive dumplings starting at the first row from the top and the first column from the left. The direction is left to right. Then you skewer them in this order.
  • You choose three consecutive dumplings starting at the first row from the top and the 44-th column from the left. The direction is top to bottom. Then you skewer them in this order.
  • You choose three consecutive dumplings starting at the third row from the top and the first column from the left. The direction is left to right. Then you skewer them in this order.

Since you cannot make 44 sticks, the output is 33.

For Sample 22: You can make 44 dango sticks.

  • You choose three consecutive dumplings starting at the first row from the top and the first column from the left. The direction is left to right. Then you skewer them in this order.
  • You choose three consecutive dumplings starting at the first row from the top and the 44-th column from the left. The direction is top to bottom. Then you skewer them in this order.
  • You choose three consecutive dumplings starting at the second row from the top and the second column from the left. The direction is top to bottom. Then you skewer them in this order.
  • You choose three consecutive dumplings starting at the second row from the top and the third column from the left. The direction is top to bottom. Then you skewer them in this order.

Since you cannot make 55 sticks, the output is 44.

Problem Note

This problem comes from T3: Dango Maker of The 17th Japanese Olympiad in Informatics (JOI 2017/2018) Final Round: T3: Dango Maker.
Translated and整理 (pinyin: zhengli, compiled/organized) by @求学的企鹅.

Translated by ChatGPT 5