#C7429. Maximum Artifacts

    ID: 51299 Type: Default 1000ms 256MiB

Maximum Artifacts

Maximum Artifacts

You are given an R × C grid representing a field. Each cell in the grid is either an obstacle denoted by #, an empty cell denoted by ., or a cell containing a single-digit artifact (from 0 to 9). Two cells are considered connected if they are adjacent vertically or horizontally (4-direction connectivity).

Your task is to find the contiguous area (i.e., connected component) that is free of obstacles and that yields the maximum sum of artifact values. If a cell contains a digit, that value is added to the sum of the connected component. If a cell is empty (.) the value is 0. Obstacles (#) block connectivity.

Note: The sum for an area with no artifacts is 0.

The solution should read from standard input and write to standard output.

inputFormat

The first line contains two integers R and C (the number of rows and columns) separated by a space.

The following R lines each contain a string of length C representing a row of the grid. Each character is either a digit (0-9), a . for an empty cell, or a # for an obstacle.

All input is provided via standard input.

outputFormat

Output a single integer, the maximum sum of artifact values in any contiguous region free of obstacles.

The output should be written to standard output.

## sample
5 5
..1..
.#...
..2#.
.....
3..#.
6