Q:

recursive algorithm for 2D array in Python

a = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 8, 1, 2],
    [6, 3, 1, 4],
]

I have a 2D array and given cell indexs(i,j).
for e.g.  we are given x,y=(2,3)  so this is for a[2][3].
What I want to do using dynamic programming or recursion(prefer) is that I will traverse the whole matrix (until a given condition satisfy) by moving up, down, left and right.
I mean , I will go from (x,y) to all 4 adjacent cell, and then do this  recursively for all 4 cell(until some condition). but this is not guaranteed that all 4 cell will be covered(there is algorithm for that, if that satisfy then only I will move to that cell and further from that cell using  same algo. 
I'm able to implement algorithm but I'm having hard time to implement recursive solution in Python.
can you please suggest something, link to some similar recursive code.
I tried to find same on stackoverflow but not success. I'm not sure if this is violating the question posting rule, if so I will remove but please try to help me with this first. 

A:

What you are trying to do is very similar to a flood fill. Check the Wikipedia page to learn the algorithm. It is common enough that it should not be hard to find code for it in multiple languages.

