<p>Given an <code>n x n</code> <code>grid</code>&nbsp;containing only values <code>0</code> and <code>1</code>, where&nbsp;<code>0</code> represents water&nbsp;and <code>1</code> represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance.&nbsp;If no land or water exists in the grid, return <code>-1</code>.</p>

<p>The distance used in this problem is the Manhattan distance:&nbsp;the distance between two cells <code>(x0, y0)</code> and <code>(x1, y1)</code> is <code>|x0 - x1| + |y0 - y1|</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,1],[0,0,0],[1,0,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The cell (1, 1) is as far as possible from all the land with distance 2.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,0]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The cell (2, 2) is as far as possible from all the land with distance 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n&nbsp;&lt;= 100</code></li>
	<li><code>grid[i][j]</code>&nbsp;is <code>0</code> or <code>1</code></li>
</ul>

class Solution {
public:
int bfs(vector<pair<int, int>> before, vector<vector<bool>>& visited, vector<vector<int>>& grid) {
    queue<pair<pair<int, int>, int>> q;
    for (int i = 0; i < before.size(); i++) {
        q.push({{before[i].first, before[i].second}, 0});
        visited[before[i].first][before[i].second] = true;
    }
    
    int n = grid.size();
    int m = grid[0].size();
    int maxDistance = -1;

    while (!q.empty()) {
        int row = q.front().first.first;
        int col = q.front().first.second;
        int dis = q.front().second;
        q.pop();
        
        int dr[] = {-1, 0, 1, 0};
        int dc[] = {0, 1, 0, -1};
        
        for (int i = 0; i < 4; i++) {
            int newr = row + dr[i];
            int newc = col + dc[i];
            if (newr >= 0 && newr < n && newc >= 0 && newc < m && !visited[newr][newc] && grid[newr][newc] == 0) {
                q.push({{newr, newc}, dis + 1});
                visited[newr][newc] = true;
                maxDistance = max(maxDistance, dis + 1);
            }
        }
    }

    return maxDistance;
}

int maxDistance(vector<vector<int>>& grid) {
    vector<pair<int, int>> before;
    int n = grid.size();
    int m = grid[0].size();
    vector<vector<bool>> visited(n, vector<bool>(m, false));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (grid[i][j] == 1) {
                before.push_back({i, j});
            }
        }
    }

    if (before.size() == 0 || before.size() == n * m) {
        return -1;  // No land cells or all cells are land.
    }

    return bfs(before, visited, grid);
}
};
29306