#C3470. Largest Connected Component in a Grid

    ID: 46901 Type: Default 1000ms 256MiB

Largest Connected Component in a Grid

Largest Connected Component in a Grid

Given an ( n \times m ) grid, some cells are marked. Two marked cells are considered connected if they share a common side (up, down, left, or right). Your task is to compute the size of the largest connected component of marked cells. Formally, for a grid ( G ) with ( n ) rows and ( m ) columns and a set of marked cells ( S ), find the maximum ( |C| ) over all connected components ( C \subseteq S ) where two cells ( (i,j) ) and ( (k,l) ) are adjacent if (|i-k| + |j-l| = 1).

inputFormat

The input begins with a single line containing three integers ( n ), ( m ), and ( k ) (the number of rows, columns, and marked cells, respectively). Each of the next ( k ) lines contains two integers ( r ) and ( c ) (1-indexed) representing the position of a marked cell. It is guaranteed that (1 \leq r \leq n) and (1 \leq c \leq m).

outputFormat

Output a single integer, the size of the largest connected component of marked cells.## sample

3 3 3
1 1
2 1
1 2
3