cho.sh
Notes
Loading...

Castle

Time limit

2s

Memory limit

128 MB

Problem

A castle is represented as an M by N grid of square cells. Each side of a cell may have a wall, and you can move to an adjacent cell only through a side without a wall.

Given the map of the castle, compute these three values.

  1. The number of rooms in the castle
  2. The area of the largest room
  3. The largest room area obtainable by removing exactly one wall

The area of a room is the number of cells in that room. The castle has at least two rooms, and there is always at least one wall whose removal merges two different rooms.

Input

The first line contains two integers N and M. N is the number of columns, M is the number of rows, and 1 <= N, M <= 50.

Each of the next M lines contains N integers. Each integer describes the walls of one cell. Add 1 for a west wall, 2 for a north wall, 4 for an east wall, and 8 for a south wall. Therefore each value is between 0 and 15, inclusive.

Output

Print the number of rooms in the castle on the first line. Print the area of the largest room on the second line. Print the largest room area obtainable by removing one wall on the third line.