#K75907. Largest Connected Component of 1s

    ID: 34523 Type: Default 1000ms 256MiB

Largest Connected Component of 1s

Largest Connected Component of 1s

You are given a binary matrix \(A\) of dimensions \(m \times n\), where each element is either 0 or 1. Your task is to find the size of the largest connected component consisting of 1s. Two cells are connected if they share an edge (i.e. up, down, left, or right).

Detail: A connected component is a set of cells such that from any cell in the component, there is a path (moving only in the four cardinal directions) to any other cell in the component, and all the cells in the path contain the value 1. The size of the component is the number of cells in it.

Example:

Input:
4 4
1 0 0 1
1 1 0 0
0 0 0 1
1 1 1 1

Output: 5

</p>

In the above example, the largest connected group of 1s has size \(5\).

inputFormat

The input is given via standard input (stdin) and has the following format:

  1. The first line contains two integers \(m\) and \(n\), representing the number of rows and columns of the matrix respectively.
  2. This is followed by \(m\) lines, each containing \(n\) integers separated by spaces. Each integer is either 0 or 1.

It is guaranteed that \(1 \leq m, n \leq 10^3\) (or as per problem constraints) and each cell value is either 0 or 1.

outputFormat

Output a single integer, which is the size of the largest connected component of 1s in the matrix. The output should be written to standard output (stdout).

## sample
4 4
1 0 0 1
1 1 0 0
0 0 0 1
1 1 1 1
5