Problem A
Emptying the Baltic

First, Gunnar wants to build a floodbank connecting Denmark and Norway to separate the Baltic from the Atlantic Ocean. The floodbank will also help protect Nordic countries from rising sea levels in the ocean. Next, Gunnar installs a device that can drain the Baltic from the seafloor. The device will drain as much water as needed to the Earth’s core where it will disappear forever (because that is how physics works, at least as far as Gunnar is concerned). However, depending on the placement of the device, the entire Baltic might not be completely drained – some pockets of water may remain.
To simplify the problem, Gunnar is approximating the map of
the Baltic using a
Water from a square on the grid can flow to any of its
Gunnar is more of an idea person than a programmer, so he has asked for your help to evaluate how much water would be drained for a given placement of the device.
Input
The first line contains two integers
Then follow
The last line contains two integers
Output
Output one line with one integer – the total volume of sea water drained, in cubic meters.
Sample Input 1 | Sample Output 1 |
---|---|
3 3 -5 2 -5 -1 -2 -1 5 4 -5 2 2 |
10 |
Sample Input 2 | Sample Output 2 |
---|---|
2 3 -2 -3 -4 -3 -2 -3 2 1 |
16 |