#K68357. Counting Islands

    ID: 32846 Type: Default 1000ms 256MiB

Counting Islands

Counting Islands

The task is to determine the number of islands in a two-dimensional grid. An island is defined as a group of adjacent cells containing the character X that are connected horizontally or vertically (not diagonally). In other words, any two X cells are part of the same island if there exists a sequence of cells such that for every two consecutive cells \( (i,j) \) and \( (k,l) \), the condition \(|i-k| + |j-l| = 1\) holds.

You are required to implement an algorithm that calculates the total number of such islands in the grid. Make sure your solution reads input from standard input (stdin) and writes output to standard output (stdout).

inputFormat

The first line of input contains two space-separated integers (m) and (n), representing the number of rows and columns of the grid respectively. This is followed by (m) lines, each containing a string of length (n) comprised only of the characters 'X' and 'O'.

outputFormat

Output a single integer to stdout representing the number of islands in the grid.## sample

4 4
XOOO
OXOO
OOXX
OOOO
3