Calculate a rock rolling down a hill Introduction
Sisyphus was having some troubles at work lately. It seems he just never gets anything done, and he would love to find a solution to this problem.
His current employment requires rolling a rock up a hill. He usually does his job well, but every time he is near the top of the hill it rolls down again.
He is getting really frustrated with his job and wants to solve the problem scientifically by having a computer simulate the rock rolling down the hill.
It so happens that Sisyphus isn't particularly good at programming, so maybe you can help him out?
The Challenge
After this silly introduction, let's come to business. Your program will receive an illustration of the hill and the rock which looks similar to this:
#o        
##
###
######
######## 

Where # represents a part of the hill and o represents the rock.
You now have to implement a program which moves the rock 1 layer down. For example, the output of the above should be:
#        
##o
###
######
######## 

If there is a horizontally even area, the hill just rolls horizontally, so...
o
######## 

...this would just make the stone roll sideways.
 o
######## 

If there is a vertical area, the rock falls down one step, so...
#o
#
#
##### 

...would yield...
#
#o
#
##### 

You will also receive the width and height of the image respectively in one line above the image. So, in complete, our sample input would look like this:
10 5
#o        
##        
###       
######    
######### 

(Note that the whitespace here are spaces. Select the text and see what I mean.)
Some details

When the rock is already in the last line when running the program, you can choose to either terminate the program or output the unchanged input

The hill only ever goes downwards

Your program should format the output exactly the same as the input (including the dimensions), so if you pipe the output of the program to itself, it calculates the next step.

You can assume there is always a way to the bottom, so input where the path is &quot;blocked&quot; may cause undefined behaviour

You can assume there is always a space in the last line. The rock should &quot;rest&quot; there, so after calling the program a few times, always piping it's output into itself, you should end up with the rock in the last line, laying where the space previously was.

You may accept input in any form you like (stdin, file,...). You have to post the WHOLE program (so all pre-initialized variables count as code).

The lines are terminated with \n.

You can get some example inputs here (make sure you copy the spaces correctly!)

This is code-golf, so the working submission with the least bytes wins.

The winner will be chosen on July 26th, 2014. You can post solutions after that, but you can't win


If you have any questions, let me know in the comments.
Happy golfing!
Regex (.NET, Perl, PCRE, JavaScript, ... flavours), 25 bytes

Yes, this will create some debate again, whether a regular expression is a valid program, but I'll pre-empt that and say that this submission is just for fun and doesn't need to be considered for the winner. (As opposed to the 31 byte Perl variant at the bottom ;).)

So here is a pure regex replacement solution.

Pattern (note the trailing space):

o(( *)\n#*)(?=\2) |o 


Replacement (note the leading space):

 $1o


The byte count is for the sum of the two.

You can test it at http://regexhero.net/tester/. Make sure to choose Unix-style line endings and "preserve pasted formatting" when pasting. If it still doesn't work, you still have pasted Windows-style line endings. The easiest fix in that case is to replace \n with \r\n in the pattern to see that it works.

Here is a 48 byte ECMAScript 6 function using this

f=(s)=&gt;s.replace(/o(( *)\n#*)(?=\2) |o /,' $1o')


Finally, I've also got an actual program. It's 31 bytes of Perl (including two bytes for p and 0 flags; thanks to Ventero for the suggestion!).

s/o(( *)\n#*)(?=\2) |o / $1o/


If you want to test it, don't even save it in a file, just do

perl -p0e 's/o(( *)\n#*)(?=\2) |o / $1o/' &lt; hill.txt