C++ with picture (of Mars)
OK, it was an 6:30 AM in my time zone, and I did not see it. So I solved it by raising land layer-by-layer by checking adjacent cells.\n\nIt seemed to work, but
OK, it was an 6:30 AM in my time zone, and I did not see it. So I solved it by raising land layer-by-layer by checking adjacent cells.\n\nIt seemed to work, but then I got TLE. It took me a while (blaming early morning, again), to realize that the same effect can be achieved if we do BFS starting from all water cells.\n\nIn the picture below, you can see that each tern, unvisited (white) cells always board visited cells with the same height. Thus, the rules defined for this problem are satisfied.\n\n> This example may resemble the surface of Mars, with red and rocky hills, and scarce pockets of frozen water.\n\n![image](https://assets.leetcode.com/users/images/aae9620d-3804-43ec-add5-86b3610b2eec_1613943502.4768167.png)\n\n**C++**\n1. Collect all water cells into a queue and mark all other cells as unvisited (`-1`).\n2. For the current round of water cells (`h` is 0) in the queue:\n\t- Check neighboring cells in 4 direction.\n\t- If the cell is unvisited, set it to `h + 1` and add it to the next round queue.\n3. When done with the current round, increase `h`.\n4. Repeat step 2 for the next round queue (if not empty).\n\n```cpp\nint dirs[5] = {0, 1, 0, -1, 0};\nvector<vector<int>> highestPeak(vector<vector<int>>& w) {\n    vector<pair<int, int>> p;\n    int h = 1, si = w.size(), sj = w[0].size();\n    for (auto i = 0; i < si; ++i)\n        for (auto j = 0; j < sj; ++j) {\n            w[i][j] = w[i][j] == 1 ? 0 : -1;\n            if (w[i][j] == 0)\n                p.push_back({i, j});\n        }\n    while (!p.empty()) {\n        vector<pair<int, int>> p1;\n        for (auto [i, j] : p) {\n            for (int d = 0; d < 4; ++d) {\n                int x = i + dirs[d], y = j + dirs[d + 1];\n                if (x >= 0 && y >= 0 && x < si && y < sj && w[x][y] == -1) {\n                    w[x][y] = h;\n                    p1.push_back({x, y});\n                }\n            }\n        }\n        swap(p, p1);\n        ++h;\n    }\n    return w;\n}\n```
[]