BFS travels solution in matrix
Complexity
Time complexity:
O(n∗m)
Space complexity:
O(n∗m)
Code
# Complexity
- Time complexity:
$$O(n*m)$$
- Space complexity:
$$O(n*m)$$

# Code
```cpp []
class Solution {
    vector<pair<int, int>> dir = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        int n = isWater.size();
        int m = isWater[0].size();
        vector<vector<int>> res(n, vector<int>(m, -1));
        queue<pair<int, pair<int, int>>> q;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (isWater[i][j] == 1) {
                    res[i][j] = 0;
                    q.push({0, {i, j}});
                }
            }
        }

        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int a = dir[i].first + y.first;
                int b = dir[i].second + y.second;
                if (a >= 0 && a < n && b >= 0 && b < m) {
                    if (res[a][b] == -1) {
                        res[a][b] = x + 1;
                        q.push({x + 1, {a, b}});
                    }
                }
            }
        }
        return res;
    }
};
```
['Array', 'Breadth-First Search', 'Matrix', 'C++']