# largest connected component on a grid | python3 program to print the largest connected component in a grid ; stores information about which cell are already visited in a particular bfs ; result stores the final result grid ; stores the count of cells in the largest connected component ; function checks if a cell is valid i . e it is inside the grid and equal to the key ; bfs to find all cells in connection with key = input [ i ] [ j ] ; terminating case for bfs ; x_move and y_move arrays are the possible movements in x or y direction ; checks all four points connected with input [ i ] [ j ] ; called every time before a bfs so that visited array is reset to zero ; if a larger connected component is found this function is called to store information about that component . ; function to print the result ; prints the largest component ; function to calculate the largest connected component ; checking cell to the right ; updating result ; checking cell downwards ; updating result ; 

n = 6
m = 8
visited = [[0 for j in range(m)]for i in range(n)]
result = [[0 for j in range(m)]for i in range(n)]
COUNT = 0


def is_valid(x, y, key, input):
    if (x < n and y < m and x >= 0 and y >= 0):
        if (visited[x][y] == 0 and input[x][y] == key):
            return True
        else:
            return False
    else:
        return False


def BFS(x, y, i, j, input):
    global COUNT
    if (x != y):
        return
    visited[i][j] = 1
    COUNT += 1
    x_move = [0, 0, 1, -1]
    y_move = [1, -1, 0, 0]
    for u in range(4):
        if (is_valid(i + y_move[u], j + x_move[u], x, input)):
            BFS(x, y, i + y_move[u], j + x_move[u], input)


def reset_visited():
    for i in range(n):
        for j in range(m):
            visited[i][j] = 0


def reset_result(key, input):
    for i in range(n):
        for j in range(m):
            if (visited[i][j] != 0 and input[i][j] == key):
                result[i][j] = visited[i][j]
            else:
                result[i][j] = 0


def print_result(res):
    print("The largest connected " + "component of the grid is :" + str(res))
    for i in range(n):
        for j in range(m):
            if (result[i][j] != 0):
                print(result[i][j], end=' ')
            else:
                print('. ', end='')
        print()


def computeLargestConnectedGrid(input):
    global COUNT
    current_max = -10000000000
    for i in range(n):
        for j in range(m):
            reset_visited()
            COUNT = 0
            if (j + 1 < m):
                BFS(input[i][j], input[i][j + 1], i, j, input)
            if (COUNT >= current_max):
                current_max = COUNT
                reset_result(input[i][j], input)
            reset_visited()
            COUNT = 0
            if (i + 1 < n):
                BFS(input[i][j], input[i + 1][j], i, j, input)
            if (COUNT >= current_max):
                current_max = COUNT
                reset_result(input[i][j], input)
    print_result(current_max)


if __name__ == '__main__':
    input = [
        [
            1, 4, 4, 4, 4, 3, 3, 1], [
            2, 1, 1, 4, 3, 3, 1, 1], [
                3, 2, 1, 1, 2, 3, 2, 1], [
                    3, 3, 2, 1, 2, 2, 2, 2], [
                        3, 1, 3, 1, 1, 4, 4, 4], [
                            1, 1, 3, 1, 1, 4, 4, 4]]
    computeLargestConnectedGrid(input)

# Expected output:
The largest connected component of the grid is :9
. . . . . . . . 
. 1 1 . . . . . 
. . 1 1 . . . . 
. . . 1 . . . . 
. . . 1 1 . . . 
. . . 1 1 . . .