Count occurrences of each digit in its row and column

Given a matrix uu of digits, we have to produce a new matrix vv of digits with the same dimensions, where the digit in each position is calculated in the following way.

Let i,ji,j be a position in uu, and let dd be the digit in u[i][j]u[i][j]. Then, v[i][j]v[i][j] is the number of occurrences of dd in uu within row ii, plus the number of occurrences of dd in uu within column jj, and all of it modulus 1010 so that the result is again a digit.

Note that the occurrence of dd at position i,ji,j will be counted twice, one for row ii and another for column jj.

For instance, consider this matrix uu:

0044
5233
2221
1150

Then, the resulting matrix vv is:

3333
2333
4542
3322

For instance, u[1][1]=2u[1][1] = 2. At row 11 we have only one 22. And at column 11 we have two 22. Therefore v[1][1]=(1+2)%10=3v[1][1] = (1+2)\%10 = 3.

Input

The input has several cases. Each case starts with two positive natural numbers, n,mn, m, in one line. After that comes a matrix of n×mn\times{}m digits (nn lines with mm digits each), followed by an empty line.

Output

For each case, the program has to write nn lines with mm digits each, which is the resulting matrix, followed by an empty line.

Observation

Evaluation out of 10 points:

A fast solution is one which is correct, of linear cost and passing the test cases, both public and private. A slow solution is one which is not fast, but it is correct and passes the public test cases.

Problem information

Author: PRO1

Generation: 2026-01-25T22:38:39.288Z

© Jutge.org, 2006–2026.
https://jutge.org