Given a grid of size m x n, initially filled with zeros, you are given a sequence of positions to set to 1. After each update, return the number of connected components (groups of adjacent 1s, connected horizontally or vertically) in the grid. Implement a function that takes the grid dimensions and the list of positions to update, and returns a list of the number of connected components after each update.
Example 1
Input: 3, 3, [[0,0],[1,1],[0,1],[2,2]]
Output: [1,2,1,2]
Explanation: After each update: [[0,0]]: one component; [[1,1]]: two; [[0,1]]: connects (0,0) and (0,1); [[2,2]]: new component.
Example 2
Input: 2, 2, [[0,0],[0,1],[1,1],[1,0]]
Output: [1,1,1,1]
Explanation: All cells become connected as updates proceed.
Constraints
Case 1
Input: 4, 4, [[0,0],[3,3],[0,1],[1,1],[2,2],[2,3]]
Expected: [1,2,1,1,2,2]
Case 2
Input: 5, 5, [[2,2],[2,3],[3,3],[1,1],[1,2],[1,3]]
Expected: [1,1,1,2,2,1]