Simple solution: using multi source BFS technique 
IntuitionThe problem can be solved using multi-source BFS. Since water cells have height 0, we can propagate this information to neighboring land cells, updatin
### Intuition  
The problem can be solved using **multi-source BFS**. Since water cells have height 0, we can propagate this information to neighboring land cells, updating their height incrementally until all cells are processed.

---

### Approach  
1. **Initialization**:
   - Create a queue (`q`) for BFS.
   - Initialize the result grid (`ans`) with `-1` for land cells and `0` for water cells.
   - Add all water cell coordinates to the queue since they are starting points.

2. **BFS Propagation**:
   - For each cell, process its neighbors using a predefined set of moves: up, down, left, and right.
   - If a neighbor is within bounds and unvisited (`-1` in `ans`), update its height as the current cell's height + 1 and add it to the queue.

3. **Termination**:
   - Continue until the queue is empty. The heights of all land cells are calculated based on their distance to the nearest water cell.

4. **Output**:
   - Return the populated result grid.

---

### Complexity  
- **Time Complexity**:  
  $$O(n \cdot m)$$  
  Each cell is processed once during the BFS traversal.  

- **Space Complexity**:  
  $$O(n \cdot m)$$  
  For the `ans` grid and BFS queue.

---

![UPvote.jpg](https://assets.leetcode.com/users/images/80ee2f5f-534f-4756-ae35-b53a1a25fb07_1737566665.322336.jpeg)


### Code Explanation  

```cpp
class Solution {
private:
    vector<pair<int, int>> moves = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    // Check if a position is valid (inside the grid)
    bool isValid(int i, int j, int n, int m) {
        return i >= 0 && j >= 0 && i < n && j < m;
    }

public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        int n = isWater.size(), m = isWater[0].size();
        vector<vector<int>> ans(n, vector<int>(m, -1)); // Initialize grid
        queue<pair<int, int>> q;

        // Add all water cells to the queue with height 0
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (isWater[i][j]) {
                    q.push({i, j});
                    ans[i][j] = 0;
                }
            }
        }

        // BFS to calculate heights for land cells
        while (!q.empty()) {
            auto [vx, vy] = q.front(); // Current cell
            q.pop();

            for (auto move : moves) { // Iterate over all directions
                int child_x = move.first + vx, child_y = move.second + vy;
                if (!isValid(child_x, child_y, n, m)) continue; // Skip invalid cells
                if (ans[child_x][child_y] != -1) continue;     // Skip already visited cells

                // Update height and add to queue
                q.push({child_x, child_y});
                ans[child_x][child_y] = ans[vx][vy] + 1;
            }
        }

        return ans; // Return the final grid
    }
};
```  
['Array', 'Breadth-First Search', 'Matrix', 'C++']