Here is a recursive routine for your fill problem. Enjoy.
;
; Flood Fill example... by Brien King
; Check out FrostByte Freddie at <a href="http://www.arcaderestoration.com" target="_blank">http://www.arcaderestoration.com</a> under "Software"
;
Graphics 800,600,8
Dim G_grid(10,10)
;------------------------------------
; Initialize our grid to all Zeros
;------------------------------------
For x% = 1 To 10
For y% = 1 To 10
G_grid(x%,y%) = 0
Next
Next
;------------------------------------
; Now create a block that we want
; to fill in.
;------------------------------------
For x% = 4 To 7
For y% = 4 To 7
G_grid(x%,y%) = 1
Next
Next
G_grid(3,3) = 1
G_grid(3,4) = 1
G_grid(8,3) = 1
;------------------------------------
; Display our "Before" block
;------------------------------------
For y% = 1 To 10
temp$ = ""
For x% = 1 To 10
temp$ = temp$ + G_grid(x%,y%)
Next
Print temp$
Next
;------------------------------------
; This FILL should do nothing as it
; won't find the color we want to
; fill in here.
; We want to fill COLOR 1 with
; COLOR 2.
;------------------------------------
FloodFill(1,2,1,1)
Print
Print "The next grid should look just like the one above"
Print
;------------------------------------
; Display the Results of our fill
; they should be the same as before
;------------------------------------
For y% = 1 To 10
temp$ = ""
For x% = 1 To 10
temp$ = temp$ + G_grid(x%,y%)
Next
Print temp$
Next
;------------------------------------
; Now lets fill in the block in
; the Center. We want to fill
; COLOR 1 with COLOR 3. We should
; see the 1's turn into 3's and
; the 0's stay the same.
;------------------------------------
FloodFill(1,3,5,5)
Print
Print "The next grid should have all 3's in the center"
Print
;------------------------------------
; Show us the Results....
;------------------------------------
For y% = 1 To 10
temp$ = ""
For x% = 1 To 10
temp$ = temp$ + G_grid(x%,y%)
Next
Print temp$
Next
;------------------------------------
; Now lets fill outside block in
; the Center. We want to fill
; COLOR 0 with COLOR 5. We should
; see the 0's turn into 5's and
; the 3's stay the same.
;------------------------------------
FloodFill(0,5,1,1)
Print
Print "The next grid should be 5's, 3's and a single 1"
Print
;------------------------------------
; Show us the Results....
;------------------------------------
For y% = 1 To 10
temp$ = ""
For x% = 1 To 10
temp$ = temp$ + G_grid(x%,y%)
Next
Print temp$
Next
WaitKey
;---------------------------------------------------
; This function Recursivly fills in an area. of a
; Grid...
;
; NOTE: This does not handle Diagnols
; as you will see the Upper Right 1 doesn't change.
; If you need that and can't figure it out,
; let me know. Also you should keep in mind that
; if you have a HUGE area to fill you could run into
; stack overflow issues. But that would have
; to be a VERY LARGE area.
;---------------------------------------------------
Function FloodFill(P_colorToFill%, P_color%, P_x%, P_y%)
;---------------------------------------------
; If the spot we are on is not the color we
; want to fill then exit the function, else
; fill in the color.
;---------------------------------------------
If G_grid(P_x%,P_y%) <> P_colorToFill% Then
Return
Else
G_grid(P_x%,P_y%) = P_color%
EndIf
;---------------------------------------------
; We start by looking behind our current
; position to see if have the color we want.
;---------------------------------------------
If P_x% > 1 Then
FloodFill(P_colorToFill%, P_color%, P_x%-1, P_y%)
EndIf
;---------------------------------------------
; Now look in front of our current
; position to see if have the color we want.
;---------------------------------------------
If P_x% < 10 Then
FloodFill(P_colorToFill%, P_color%, P_x% + 1, P_y%)
EndIf
;---------------------------------------------
; Now look above our current
; position to see if have the color we want.
;---------------------------------------------
If P_y% > 1 Then
FloodFill(P_colorToFill%, P_color%, P_x%, P_y%-1)
EndIf
;---------------------------------------------
; Now look below our current
; position to see if have the color we want.
;---------------------------------------------
If P_y% < 10 Then
FloodFill(P_colorToFill%, P_color%, P_x%, P_y%+1)
EndIf
End Function