#K75117. Largest Connected Block
Largest Connected Block
Largest Connected Block
You are given a grid representing a city map. Each cell is either a building (denoted by 1) or an empty space (denoted by 0). A block is defined as a group of adjacent building cells connected in the four cardinal directions (up, down, left, right).
Your task is to compute the size of the largest connected block of buildings in the grid. Formally, two cells \( (i,j) \) and \( (k,l) \) are considered connected if \( |i-k|+|j-l|=1 \).
For example, in the grid below:
1 0 0 1 1 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 1 1 0 1 1 0 0 1 1 1
the largest connected block has a size of 6.
inputFormat
The first line contains two integers ( n ) and ( m ), the number of rows and columns respectively. The next ( n ) lines each contain ( m ) space-separated integers (either 0 or 1) representing the city map.
outputFormat
Output a single integer denoting the size of the largest connected block of buildings.## sample
6 7
1 0 0 1 1 0 1
1 0 1 0 1 0 0
0 0 1 1 0 0 0
1 1 1 0 0 0 1
0 0 0 0 1 1 0
1 1 0 0 1 1 1
6