The very same method which solves all problems. A method taught to students at high school while programming which helps solving many problems which would require extensive logic just by the use of simple ideas. RECURSION.
Of course, I knew I had to use it, but then the question was the idea. Finally I got the idea a week before my competition(you can see about the competition in my member introduction. You will know which one. It is in one of the replies in my thread.)
The idea was "explore a tile and convert it to a wall". First, I used two for loops to run through the 2D array in the main function. The moment I got a space tile (i.e. cell with value 0), I sent its coordinates(or location) to the 'explore'. There I converted the 0 to 1 (i.e. space to wall) and then checked the neighboring tiles for zeros(space). Each time I got one I again sent its location to the very same explore function(hence, recursion) and the process got repeated. For zeros in multiple directions, I gave preference in the order of down, right, up and left. If no 0 found in any direction, the method would return back to where it was last called from - itself, but with the previous value now which it had used to call itself. Only this time, it would try in some other direction other than the path followed previously. That is the main advantage of using recursion. Ultimately, since it cannot jump squares but only follow the path of zeros, it stays confined in a particular chamber and converts the white tiles to black ones. When all done, it ultimately returns back to the main function, but this doesn't end still. Remember that we are in a loop that runs through the grid, checking for zeros. With one chamber practically erased and its size(area) stored, the loop finds another zero in another chamber(if present) and explore starts from there.
Thus, the program does this to every chamber one by one until no chamber is left. Also while this, I am also calculating the area, incrementing it in a chamber for every 0 converted to 1. After the chamber vanished, I compare it with the previous value found. If greater, I store it in the variable which I compared with just now(this is to find the largest area). The program also keeps count of the chambers. And this is how I did it.
Thank you again.