from collections import deque

def pacific_atlantic(heights):
    if not heights or not heights[0]:
        return []
    rows, cols = len(heights), len(heights[0])
    def reachable(starts):
        seen = set(starts)
        queue = deque(seen)
        while queue:
            r, c = queue.popleft()
            for dr, dc in ((1,0),(-1,0),(0,1),(0,-1)):
                nr, nc = r + dr, c + dc
                if (0 <= nr < rows and 0 <= nc < cols and (nr, nc) not in seen
                        and heights[nr][nc] >= heights[r][c]):
                    seen.add((nr, nc))
                    queue.append((nr, nc))
        return seen
    pacific = reachable([(0,c) for c in range(cols)] + [(r,0) for r in range(rows)])
    atlantic = reachable([(rows-1,c) for c in range(cols)]
                         + [(r,cols-1) for r in range(rows)])
    return [[r,c] for r in range(rows) for c in range(cols)
            if (r,c) in pacific and (r,c) in atlantic]
