def dfs(i, j, mat, visited):
    """
    Applies Depth first search.
    
    Args:
        i (int): The row index of the src element
        j (int): The column index of the src element
        mat (List[List[int]]): The input matrix
        visited (List[List[bool]]): The visited matrx
    """
    if visited[i][j]:
        return None

    visited[i][j] = True

    # Checking the adjacent vertices whether they 
    # are land masses
    # left
    if j - 1 >= 0 and not visited[i][j-1] and mat[i][j-1] == 1:
        dfs(i, j-1, mat, visited)
    # right
    if j + 1 < len(mat[0]) and not visited[i][j+1] and mat[i][j+1] == 1:
        dfs(i, j+1, mat, visited)
    # top
    if i - 1 >= 0 and not visited[i-1][j] and mat[i-1][j] == 1:
        dfs(i-1, j, mat, visited)
    # bottom
    if i + 1 < len(mat) and not visited[i + 1][j] and mat[i+1][j] == 1:
        dfs(i+1, j, mat, visited)

    return None