Here's a recursive routine taken from a colour match game I did ages ago in B3D, if it helps any...
Global g_balls_selected Dim board.orb(13, 11) ; x and y = array position ; id = balls id Function pick_all(x, y, id, root=True) If root = True g_balls_selected = 0 root = False EndIf If x > 0 If board(x - 1, y)\id = id If board(x, y)\selected = False board(x, y)\selected = True g_balls_selected = g_balls_selected + 1 EndIf If board(x - 1, y)\selected = False Then pick_all(x - 1, y, id, root) EndIf EndIf If x < (C_BOARD_MAX_X - 1) If board(x + 1, y)\id = id If board(x, y)\selected = False board(x, y)\selected = True g_balls_selected = g_balls_selected + 1 EndIf If board(x + 1, y)\selected = False Then pick_all(x + 1, y, id, root) EndIf EndIf If y > 0 If board(x, y - 1)\id = id If board(x, y)\selected = False board(x, y)\selected = True g_balls_selected = g_balls_selected + 1 EndIf If board(x, y - 1)\selected = False Then pick_all(x, y - 1, id, root) EndIf EndIf If y < (C_BOARD_MAX_Y - 1) If board(x, y + 1)\id = id If board(x, y)\selected = False board(x, y)\selected = True g_balls_selected = g_balls_selected + 1 EndIf If board(x, y + 1)\selected = False Then pick_all(x, y + 1, id, root) EndIf EndIf Return board(x, y)\selected End Function