<p>Given an <code>m x n</code> matrix <code>board</code> where each cell is a battleship <code>&#39;X&#39;</code> or empty <code>&#39;.&#39;</code>, return <em>the number of the <strong>battleships</strong> on</em> <code>board</code>.</p>

<p><strong>Battleships</strong> can only be placed horizontally or vertically on <code>board</code>. In other words, they can only be made of the shape <code>1 x k</code> (<code>1</code> row, <code>k</code> columns) or <code>k x 1</code> (<code>k</code> rows, <code>1</code> column), where <code>k</code> can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img height="333" src="https://assets.leetcode.com/uploads/2024/06/21/image.png" width="333" />
<pre>
<strong>Input:</strong> board = [[&quot;X&quot;,&quot;.&quot;,&quot;.&quot;,&quot;X&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;X&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;X&quot;]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> board = [[&quot;.&quot;]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>board[i][j]</code> is either <code>&#39;.&#39;</code> or <code>&#39;X&#39;</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> Could you do it in one-pass, using only <code>O(1)</code> extra memory and without modifying the values <code>board</code>?</p>

class Solution {
public:
    bool isValidNeighbor(vector<vector<char>>& board, pair<int,int> p) {
        return (p.first >= 0 && p.first < board.size())
                && (p.second >= 0 && p.second < board[p.first].size())  
                && board[p.first][p.second] == 'X';
    }
    
    vector<pair<int,int>> getNeighbors(vector<vector<char>>& board, pair<int,int> cell) {
        vector<pair<int,int>> all_neighbors = {
            {cell.first-1, cell.second},
            {cell.first+1, cell.second},
            {cell.first, cell.second-1},
            {cell.first, cell.second+1}
        };

        vector<pair<int,int>> neighbors;
        for(auto p: all_neighbors) {
            if (isValidNeighbor(board, p)) neighbors.push_back(p);
        }
        /*if (cell.first > 0 && isValidNeighbor(board, {cell.first-1,cell.second})) 
            neighbors.push_back({cell.first-1, cell.second});
        if (cell.first < board.size()-1 && board[cell.first+1][cell.second] =='X') 
            neighbors.push_back({cell.first+1, cell.second});
        if (cell.second > 0 && board[cell.first][cell.second-1] =='X') 
            neighbors.push_back({cell.first, cell.second-1});
        if (cell.second < board[0].size()-1 && board[cell.first][cell.second+1] =='X') 
            neighbors.push_back({cell.first, cell.second+1});*/
        return neighbors;
    }

    void dfs(vector<vector<char>>& board, pair<int,int> cell) {
        board[cell.first][cell.second] = 'V';
        for(auto& p: getNeighbors(board, cell)) dfs(board, p);
    }

    int countBattleships(vector<vector<char>>& board) {
        int ships = 0;
        for(int i = 0; i < board.size(); i++) {
            for(int j = 0; j < board[i].size(); j++) {
                if (board[i][j] == 'X') {
                    dfs(board, make_pair(i,j));
                    ships++;
                }
            }
        }
        return ships;
    }
};
13200