Description
Count the number of battleships on a board. Battleships are represented by X and can only be placed horizontally or vertically.
Examples
Input:
board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]Output:
2Explanation:
Two battleships.
Input:
board = [[1]]Output:
1Explanation:
Minimal case with a single-element matrix.
Input:
board = [["X","X",".","X"],[".",".",".","X"],["X",".",".","."],["X",".","X","X"]]Output:
4Explanation:
Four separate battleships: a horizontal 2-cell ship in row 1 (positions [0,0] and [0,1]), a vertical 2-cell ship in column 4 (positions [0,3] and [1,3]), a vertical 2-cell ship in column 1 (positions [2,0] and [3,0]), and a horizontal 2-cell ship in row 4 (positions [3,2] and [3,3]).
Constraints
- •
1 ≤ m, n ≤ 200