#P15308. [VKOSHP 2025] Big World Politics

[VKOSHP 2025] Big World Politics

Description

You are given a map of the world, represented as a rectangle of size n×mn \times m. Each cell of the map belongs to one of kk countries. The set of cells of each country is connected by sides---this means that from any cell, you can reach any other cell of the same country by moving down, up, left, and right, without entering the territory of another country.

In these troubled times, many countries have territorial claims against each other. Country ii considers all cells lying within the minimum rectangle with sides parallel to the edges of the map that contains all cells belonging to the country as its historical territories. Thus, country ii has territorial claims against all countries jj that have cells within the minimum rectangle of country ii, except for country ii itself.

As a professional analyst, your task is to determine for each country ii how many countries it has territorial claims against.

Input Format

The first line of input contains three integers nn, mm, and kk (1n,m21051 \le n, m \le 2 \cdot 10^5, 1knm21061 \le k \le nm \le 2 \cdot 10^6) --- the height of the map, the width of the map, and the number of different countries, respectively.

The following nn lines contain mm integers each, where the ii-th line contains ai1,ai2,,aima_{i1}, a_{i2}, \dots, a_{im} (1aijk1 \le a_{ij} \le k) --- the numbers of the countries to which the cells (i,1),(i,2),,(i,m)(i,1), (i,2), \dots, (i,m) belong, respectively.

It is guaranteed that each of the kk countries has at least one cell.

It is guaranteed that the set of cells of each country is connected by sides.

Output Format

Output kk integers, where the ii-th integer is the number of countries that country ii has territorial claims against.

3 4 4
1 3 3 2
1 2 2 2
1 1 1 4
2 1 0 0