
# black: 0
# blue: 1
# orange: 2

grids = [
    [0, 0, 1, 2], 
    [0, 1, 2, 1], 
    [2, 1, 1, 1]
]
rows_no = len(grids)
cols_no = len(grids[0])

# cells that have been traversed will be CROSS-ed
CROSS_IT = -1
# keep track of the largest size
size = 0
# temp holder for the color patches
holder = []

# start from cell (i, j), get the color patch
def check(i, j, color):
    if i >= rows_no or j >= cols_no or i < 0 or j < 0:
        return
    if grids[i][j] != color:
        return
    
    holder.append((i, j))
    grids[i][j] = CROSS_IT
    check(i+1, j, color)
    check(i, j+1, color)
    check(i-1, j, color)
    check(i, j-1, color)

# traverse all the cells, calculate the patches
for i in range(rows_no):
    for j in range(cols_no):
        if grids[i][j] == CROSS_IT:
            break

        cur_color = grids[i][j]
        holder.clear()
        check(i, j, cur_color)
        
        print(holder)
        if len(holder) > size:
            size = len(holder)

print('size is', size)