It's not clear to me how that code snippet fits in your solution, but if your problem is the neighborhood iteration, a common trick is to pre-define the offsets:
...
# suppose the current position is (x,y)
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
tx = x + dx # t as in target
ty = y + dy
if 0 <= tx < len(board) and 0 <= ty < len(board[tx]):
# the position (tx, ty) exists, do whatever you want with it
pass
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…