WATER - Water among Cubes

On a rectangular mesh comprising n*m fields, n*m cuboids were put, one cuboid on each field. A base of each cuboid covers one field and its surface equals to one square inch. Cuboids on adjacent fields adhere one to another so close that there are no gaps between them. A heavy rain pelted on a construction so that in some areas puddles of water appeared. 

Task

Write a program which: 

  • reads from the standard input a size of the chessboard and heights of cuboids put on the fields,
  • computes maximal water volume, which may gather in puddles after the rain,
  • writes results in the standard output.

Input

The number of test cases t is in the first line of input, then t test cases follow separated by an empty line. In the first line of each test case two positive integers 1 <= n <= 100, 1 <= m <= 100 are written. They are the size of the mesh. In each of the following n lines there are m integers from the interval [1..10000]; i-th number in j-th line denotes a height of a cuboid given in inches put on the field in the i-th column and j-th raw of the chessboard.

Output

Your program should write for each tes case one integer equal to the maximal volume of water (given in cubic inches), which may gather in puddles on the construction. 

Example

Sample input:
1
3 6
3 3 4 4 4 2
3 1 3 2 1 4
7 3 1 6 4 1

Sample output:
5

The picture below shows the mesh after the rain (seen from above). Puddles are drawn in gray.


Added by:Piotr Łowiec
Date:2004-09-13
Time limit:7s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:6th Polish Olympiad in Informatics, stage 3

hide comments
2016-04-02 19:41:56 |RAMSDEN|
n^2 log(n) dijkshtra :)
2016-04-02 09:29:21 minhthai
very nice logic, thanks :)
2015-06-11 01:09:16 Rishabh
Awesome Problem !

Last edit: 2015-06-11 09:08:13
2015-05-12 17:14:59 crypt
input:
1
7 4
78 16 94 36
87 93 50 22
63 28 91 60
64 27 41 27
73 37 12 69
68 30 83 31
63 24 68 36

output:
44

Last edit: 2015-05-12 21:18:19
2014-12-24 06:35:29 Karan2k
any tricky test case???
2014-07-26 19:24:28 crypt
please give some more test cases

Last edit: 2014-07-26 20:09:35
2013-09-24 11:43:12 selfcompiler
awesome :)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.