'Solved a Kata in python, doesn't work if I put if name == main. What is changing?

Why does it give me IndexError: list index out of range in line 28 if (cells[y][x] % TOP == 0 ?

If i remove if __name__ == '__main__': everything works perfectly.

Count Connectivity Components is a Kata on codewars, if interested.

TOP, RIGHT, BOTTOM, LEFT = 2, 3, 5, 7

def components(grid):
    grid = grid.split('\n')
    result = bfs(set_directions(grid))
    d = {}
    for k in result:
        if not k in d:
            d[k] = 0
        d[k] += 1
    result = []
    for k in d:
        result.append((k, d[k]))
    result.sort(reverse=True)
    return result

def bfs(cells):
    queue = [(0,0)]
    marked = set()
    result = []
    node_count = 0
    
    while True:
        x,y = queue.pop(0)
        marked.add((x,y))
        node_count += 1
        
        if (cells[y][x] % TOP == 0 
                and (x,y-1) not in marked 
                and (x,y-1) not in queue):
            queue.append((x,y-1))

        if (cells[y][x] % BOTTOM == 0 
                and (x,y+1) not in marked 
                and (x,y+1) not in queue):
            queue.append((x,y+1))

        if (cells[y][x] % LEFT == 0 
                and (x-1,y) not in marked 
                and (x-1,y) not in queue):
            queue.append((x-1,y))

        if (cells[y][x] % RIGHT == 0 
                and (x+1,y) not in marked 
                and (x+1,y) not in queue):
            queue.append((x+1,y))

        cells[y][x] = 'm'
        if not queue:
            result.append(node_count)
            node_count = 0
            flag = False
            for y in range(len(cells)):
                for x in range(len(cells[y])):
                    if cells[y][x] != 'm':
                        queue.append((x,y))
                        flag = True
                        break
                if flag:
                    break
            else:
                break
    return result

def set_directions(grid):
    cells = \
        [[1 for i in range(grid[0].count('+')-1)] 
                for r in range(len(grid)//2)]
    for r in range(1,len(grid), 2):
        for i in range(0, len(grid[r])-1, 3):
            subject = grid[r][i:i+3]
            if subject == '   ':
                cells[r//2][i//3] *= LEFT
        for i in range(3, len(grid[r]), 3):
            subject = grid[r][i-2:i+1]
            if subject == '   ':
                cells[r//2][i//3-1] *= RIGHT
    for c in range(1,len(grid[0]), 3):
        column = ''.join([r[c] for r in grid])
        for i in range(0,len(grid)-1, 2):
            subject = column[i:i+2]
            if subject == '  ':
                cells[i//2][c//3] *= TOP
        for i in range(1,len(grid),2):
            subject = column[i:i+2]
            if subject == '  ':
                cells[i//2][c//3] *= BOTTOM
    return cells

if __name__ == '__main__':
    a = '''\
    +--+--+--+
    |     |  |
    +--+  +  +
    |  |     |
    +  +--+--+
    |  |     |
    +--+--+--+'''
    print(a)
    print(components(a))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source