def to_graph(grid):
    """
    Build adjacency list representation of graph
    Land cells in grid are connected if they are vertically or horizontally adjacent
    """
    adj_list = {}
    n_rows = len(grid)
    n_cols = len(grid[0])
    land_val = "1"
    for i in range(n_rows):
        for j in range(n_cols):
            if grid[i][j] == land_val:
                adj_list[(i,j)] = []
                if i > 0 and grid[i-1][j] == land_val:
                    adj_list[(i,j)].append((i-1,j))
                if i < n_rows-1 and grid[i+1][j] == land_val:
                    adj_list[(i,j)].append((i+1,j))
                if j > 0 and grid[i][j-1] == land_val:
                    adj_list[(i,j)].append((i,j-1))
                if j < n_cols-1 and grid[i][j+1] == land_val:
                    adj_list[(i,j)].append((i,j+1))
    return adj_list