eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків

MAXCOMP

Ліміт часу 0.5 секунд
Ліміт використання пам'яті 256 MiB

For a matrix, let’s call a subset of cells, S, connected if there is a path between any two cells of S which consists only of cells from S. A path is a sequence of cells u1, u2, ..., uk where ui and ui+1 are adjacent for any i = 1,k-1

Given a matrix A with N rows and M columns, we define the following formula for a connected subset S of A:weight(S) = max{A(s)|s∈S}-|S|.where |*| represents the cardinality of a set and A(s) represents the value of the cell s in A.

Приклад

Вхідні дані #1
2 3
2 4 3
5 7 5
Вихідні дані #1
2