You are given a grid of rows rows and cols columns where each cell is either O (open) or X (blocked). Consider the 4-connected regions of O cells. A region is sealed if none of its cells lies on the outer border of the grid.
Count how many O cells belong to sealed regions (i.e. cells that cannot reach the border through open cells).
Line 1: two integers rows and cols.
The next rows lines each contain a string of exactly cols characters, each O or X.
A single integer: the number of O cells in sealed regions.
O or X.Example 1
Input
4 4 XXXX XOOX XOXX XXXX
Expected
3
Explanation
The three interior O cells at (1,1),(1,2),(2,1) form one region that never touches the border, so 3 cells are sealed.
Example 2
Input
3 3 OXO XXX OXO
Expected
0
Explanation
Each O sits on the border, so no region is sealed. The answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →