#K90752. Minimum Cleaning Moves

    ID: 37822 Type: Default 1000ms 256MiB

Minimum Cleaning Moves

Minimum Cleaning Moves

A robot is placed in a rectangular grid of size n rows and m columns. The robot starts at the top-left corner of the grid (cell (0, 0)) and can move one step at a time in any of the four directions: up, down, left, or right.

Certain cells of the grid are marked as dirty (represented by the number 1). In order to clean the grid, the robot must visit all cells that are marked as dirty at least once. Note that the robot is allowed to pass through any cell (dirty or clean) and may revisit cells if necessary.

Observing the structure of the grid, one can deduce that in order to cover all cells (and therefore ensure that all dirty cells are visited), the minimal number of moves the robot must perform is given by the formula:

\(n \times m - 1\)

Your task is to compute this value given the grid dimensions and grid configuration. Although the grid input includes the pattern of dirty and clean cells, the answer is always determined solely by the grid's dimensions.

inputFormat

Input: The input is given via standard input (stdin). The first line contains two space-separated integers n and m representing the number of rows and columns respectively. Each of the following n lines contains m space-separated integers (each either 0 or 1) describing the grid configuration, where 1 indicates a dirty cell and 0 indicates a clean cell.

outputFormat

Output: Print a single integer which is the minimum number of moves required for the robot to clean all dirty cells. This value is given by the formula \(n \times m - 1\).

## sample
3 3
1 0 1
0 1 0
1 0 1
8

</p>