title: SPOJ WATER : Developing a BFS algorithm
body: <p>I am attempting to solve the following question from SPOJ : </p>
  
  <blockquote>
    <p>On a rectangular mesh comprising n<em>m fields, n</em>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. </p>
    
    <p><strong>Task</strong></p>
    
    <p>Write a program which: </p>
    
    <ul>
    <li>reads from the standard input a size of the chessboard and heights of cuboids put on the fields</li>
    <li>computes maximal water volume, which may gather in puddles after the rain</li>
    <li>writes results in the standard output. </li>
    </ul>
    
    <p><strong>Input</strong></p>
    
    <p>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 &lt;= n &lt;= 100, 1 &lt;= m &lt;= 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.</p>
    
    <p><strong>Output</strong></p>
    
    <p>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. </p>
  
  <pre><code>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
  </code></pre>
  </blockquote>
  
  <p>I am using  a BFS to add how much water will flow from the border elements into the puddle(if theres any path found).  But I am unable to handle cases where a puddle maybe like two consecutive cuboids. Can anyone help me with that case?</p>