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. 


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.


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.


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. 


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

Sample output:

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

hide comments
Simes: 2022-11-06 00:53:51

@alex_2008: probably because there's only 1 test case, and your code gives WA.

alex_2008: 2022-11-03 17:54:13

Why I'm getting WA in test case 1?

bigbadwolf: 2021-11-09 10:42:21

any tricky testcases?

crochold: 2021-08-28 18:32:50

1st comment...ac in one go baebyy!!

sohit5012: 2020-05-31 11:06:22

Commenting for the first time. AC in 1 go

Artur Tadra³a: 2020-05-11 10:24:44

"separated by an empty line" - cost me 8 NZEC...

vcode11: 2019-05-23 21:23:45

How to think about such problems? Any similar problems to try?

Last edit: 2019-05-23 21:25:02
vjvjain0: 2019-05-21 17:30:44

Java coders don't use FastReader :|

masterchef2209: 2019-05-13 05:14:29

this problem is super awesome. got 1WA because I missed '\n' after each test case

read Brian Bi's answer if you need help

cegprakash: 2018-10-15 20:41:06

Test cases very weak. There are only 13 test cases in the worst test file. 7 Seconds is way too much for 13 test cases.

Last edit: 2018-10-15 20:41:47

Added by:Piotr Łowiec
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