A canvas is a grid of rows by cols cells. Each cell is either open (.) or a wall (#). Clicking a paint bucket at an open cell fills every open cell reachable from it by repeatedly moving up, down, left, or right through open cells (walls block the fill; diagonal moves do not count).
Given the grid and the clicked cell (guaranteed to be open), report how many cells get filled, including the clicked cell itself.
Line 1: two integers rows and cols.
Next rows lines: a string of exactly cols characters, each . or #.
Last line: two integers r c — the 0-indexed row and column of the clicked cell. The clicked cell is guaranteed to be ..
A single integer: the number of cells filled (the size of the connected open region containing (r, c)).
Example 1
Input
3 3 ... .#. ... 0 0
Expected
8
Explanation
The center is a wall, but the ring of 8 open cells around it is all one region reachable from (0,0). Count: 8.
Example 2
Input
2 3 .## ... 0 0
Expected
4
Explanation
From (0,0) only itself is reachable in the top row (walls block right); the bottom row is separated by walls above it except directly below (0,0) is (1,0), and the whole bottom row is open, connecting to (0,0) via (1,0). Region: (0,0),(1,0),(1,1),(1,2) = 4 cells.
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 →