#C9345. Distinct Values in Grid Paths

    ID: 53428 Type: Default 1000ms 256MiB

Distinct Values in Grid Paths

Distinct Values in Grid Paths

You are given a square grid of size \(N \times N\) filled with non-negative integers. Your task is to determine the number of distinct values that can be collected by traversing any path from the top-left cell to the bottom-right cell, where you are only allowed to move either right or down.

For each test case, output a single integer denoting the number of distinct values that appear in any valid path from the top-left to the bottom-right corner. Note that every cell in the grid can be reached by some valid path, so the answer effectively equals the number of unique integers present in the grid.

inputFormat

The first line contains an integer (T) denoting the number of test cases. For each test case, the first line contains an integer (N) representing the size of the grid. This is followed by (N) lines, each containing (N) space-separated integers representing the grid.

outputFormat

For each test case, output a single line with one integer — the number of distinct values that can be collected along any valid path from the top-left corner to the bottom-right corner.## sample

2
2
1 2
3 4
3
1 2 3
4 5 6
7 8 9
4

9

</p>