Consider a matrix where each cell contains either a or a . Any cell containing a is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally. In the following grid, all cells marked X are connected to the cell marked Y.
If one or more filled cells are also connected, they form a region. Note that each cell in a region is connected to zero or more cells in the region but is not necessarily directly connected to all the other cells in the region.
Given an matrix, find and print the number of cells in the largest region in the matrix. Note that there may be more than one region in the matrix.
For example, there are two regions in the following matrix. The larger region at the top left contains cells. The smaller one at the bottom right contains .
Complete the connectedCell function in the editor below. It should return an integer that denotes the area of the largest region.
connectedCell has the following parameter(s):
- matrix: a 2D array of integers where represents the row of the matrix
The first line contains an integer , the number of rows in the matrix.
The second line contains an integer , the number of columns in the matrix.
Each of the next lines contains space-separated integers .
Print the number of cells in the largest region in the given matrix.
1 1 0 0
0 1 1 0
0 0 1 0
1 0 0 0
The diagram below depicts two regions of the matrix; for each region, the component cells forming the region are marked with an X:
X X 0 0 1 1 0 0
0 X X 0 0 1 1 0
0 0 X 0 0 0 1 0
1 0 0 0 X 0 0 0
The first region has five cells and the second region has one cell. We print the size of the largest region.