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.