#0002BA

O-molecular mass

On a square table consisting of N x N smaller squares, professor Mira wrote some number of chemical compounds using structural formulas. The first student that calculates the largest molecular mass of all the written compounds gets an ”A”! The compounds are organic and consist of atoms C (carbon-12), O (oxygen-16), H (Hydrogen-1) and N (Nitrogen-14). Atomic masses of elements are in the brackets.


InputIn the first line of standard input there is a number N <= 2000 that represents the dimensions of the table. In the next N rows, there are N characters that can be either ‘.’ (dot), ‘-‘ (hyphen), ‘|’ (vertical line) or letters ‘N’, ‘C’, ‘O’ or ‘H’. Hyphen represents a connection between the two atoms left and right, and vertical line represents a connection between the two atoms above and below. Atoms can stand alone, but all of the connections will have atoms on both sides.

OutputIn the first row of standard output print an integer that represents the mass of the compound with the largest mass.

In 50% of test cases, the compounds will be in seperate rectangular areas.
You don’t have to worry about the atoms’ valence.

Input:
11
..H........
..|........
H-C-H......
..|........
..H........
...........
......H-O..
........|..
......H-N-H
...........
...........

Output:
33

Input:
8
H-H-H.N.
|C-C|..C
H|O|H.O|
|C-C|N|C
H-H-H|O|
...N-N|C
..O-O-O|
NC-C-C-C

Output:
84


Example 2 has 8 candidates:

2 square compounds: first with 8 atoms of hydrogen and the second with 4 atoms of carbon
3 L-chains: 7 atoms of carbon, 5 atoms of oxygen and 3 atoms of nitrogen
3 atoms: 1 oxygen and 2 nitrogen.

L chain of carbon has the largest mass: 7*12=84.



Image: molemasa

Submit solution

Coming later

The grading service will be connected in a later migration step. You can inspect the task and your previous results now.