<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:
    def countBattleships(self, board: List[List[str]]) -> int:
        if not board:
            return 0
        num_rows = len(board)
        num_cols = len(board[0])
        neighbors= [(1,0),(-1,0),(0,1),(0,-1)]
        visit = set()
        def BFS(row,col):
            queue = deque([(row,col)])
            while queue:
                for _ in range(len(queue)):
                    current_row, current_col = queue.popleft()

                    for neighbor in neighbors:
                        new_row = current_row + neighbor[0]
                        new_col = current_col + neighbor[1]
                        if 0<= new_row < num_rows and 0<= new_col < num_cols:
                            if board[new_row][new_col] == "X" and (new_row,new_col) not in visit:
                                visit.add((new_row,new_col))
                                queue.append((new_row,new_col))
        ans = 0
        for row in range(num_rows):
            for col in range(num_cols):
                if board[row][col] == "X" and (row,col) not in visit:
                    ans+=1
                    visit.add((row,col))
                    BFS(row,col)
        return ans

17700