Given an m x n matrix board where each cell is a battleship 'X' or empty '.', return the number of the battleships on board.

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

&nbsp;
Example 1:

Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2


Example 2:

Input: board = [["."]]
Output: 0


&nbsp;
Constraints:


	m == board.length
	n == board[i].length
	1 &lt;= m, n &lt;= 200
	board[i][j] is either '.' or 'X'.


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

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        
        m = len(board)
        n = len(board[0])
        res = 0
        
        pole = [ [True for i in range(n)] for j in range(m) ]
        
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'X' and pole[i][j]:
                    for z in range(i+1, m):
                        if board[z][j] == 'X':
                            pole[z][j] = False
                        else:
                            break
                            
                    for z in range(j+1, n):
                        if board[i][z] == 'X':
                            pole[i][z] = False
                        else:
                            break
                    
                    
                    res += 1
                

        
        return res
class Solution {
    public int countBattleships(char[][] board) {
            int result = 0;
            for(int i = 0;i<board.length;i++)
            {
                for(int j = 0;j<board[i].length;j++)
                {
                    if(board[i][j] == 'X')
                    {
                        remover(i, j, board);
                        result++;
                    }

                }
            }
            return result;

        }
        public void remover(int i , int j, char[][] board)
        {
            if(i<0 || i>= board.length || j<0 || j>=board[0].length || board[i][j] == '.')
            {
                return;
            }
            board[i][j] = '.';
            remover(i+1, j, board);
            remover(i, j+1, board);
        }
}
class Solution {
public:

    bool isSafe(int i,int j,int r,int c,vector<vector<char>> &arr,vector<vector<bool>> &visited){
           if(i<0 || i>r || j<0 || j>c){
             return false;
         }
       //  cout<<arr[i][j]<<" ";
        if(arr[i][j]!='X'){
            return false;
        }
        if(arr[i][j] == 'X' && visited[i][j] == true){
            return false;
        }

        if(arr[i][j] == 'X' && visited[i][j] == false){
            return true;
        }
         return false;
    
    }

    void solve(vector<vector<char>> &arr,vector<vector<bool>> &visited,int &r,int &c,int i,int j){

        if((i>r && j>c) && (i<0 && j<0)){
            return;
        }

        // down (i+1,j)
        if(isSafe(i+1,j,r,c,arr,visited)){
             visited[i+1][j] = true;
             solve(arr,visited,r,c,i+1,j);
        }else{
            // if(arr[i][j] == 'X'){
            //      visited[i][j] = true;
            // }
        }
        // // right (i,j+1)

        if(isSafe(i,j+1,r,c,arr,visited)){
             visited[i][j+1] = true;
             solve(arr,visited,r,c,i,j+1);
        }else{
            // if(arr[i][j] == 'X'){
            //      visited[i][j] = true;
            // }
        }
        // left (i,j-1);
        if(isSafe(i,j-1,r,c,arr,visited)){
            visited[i][j-1] = true;
             solve(arr,visited,r,c,i,j-1);
        }else{
            // if(arr[i][j] == 'X'){
            //      visited[i][j] = true;
            // }
        }
        // up   (i-1,j)
        if(isSafe(i-1,j,r,c,arr,visited)){
             visited[i-1][j] = true;
             solve(arr,visited,r,c,i-1,j);
        }else{
            // if(arr[i][j] == 'X'){
            //      visited[i][j] = true;
            // }
        }

    }
    int countBattleships(vector<vector<char>>& board) {
        int row = board.size()-1;
        int col = board[0].size()-1;
        vector<vector<bool>> isVisited(row+1,vector<bool>(col+1,false));
        vector<vector<int>> toStart;
        for(int i=0;i<board.size();i++){
            for(int j=0;j<board[i].size();j++){
                if(board[i][j] == 'X'){
                    vector<int> temp(2);
                    temp[0] = i;
                    temp[1] = j;
                  //  cout<<i<<" "<<j<<endl;
                    toStart.push_back(temp);
                }
            }
        }
        int ans = 0;
         for(int i=0;i<toStart.size();i++){
            if(isVisited[toStart[i][0]][toStart[i][1]]){

            }else{
            isVisited[toStart[i][0]][toStart[i][1]] = true;
             solve(board,isVisited,row,col,toStart[i][0],toStart[i][1]);
             ans++;
            }
             
         }

        //  for(auto cc:isVisited){
        //      for(auto c:cc){
        //          cout<<c<<" ";
        //      }
        //      cout<<endl;
        //  }

        return ans;
    }
};
var countBattleships = function(board) {
    
    let count = 0;
    
    for(let rowIndex = 0; rowIndex < board.length; rowIndex++){
        for(let columnIndex = 0; columnIndex < board[rowIndex].length; columnIndex++){
            const isTopEmpty = rowIndex === 0 || board[rowIndex - 1][columnIndex] !== "X";
            const isLeftEmpty = columnIndex === 0 || board[rowIndex][columnIndex - 1] !== "X";
            const isBattleShip = board[rowIndex][columnIndex] === "X" && isTopEmpty && isLeftEmpty;
            if(isBattleShip){
                count++;
            }
        }
    }
    
    return count;
    
};
Battleships in a Board