QOJ.ac

QOJ

時間限制: 2 s 記憶體限制: 128 MB 總分: 100

#14483. Combinatorics

统计

To improve her intelligence, ZJY has started studying combinatorics. One day, she solved the following problem: "Given a grid where some cells contain treasure, starting from the top-left corner and only moving right or down, what is the minimum number of paths required to collect all the treasure?" However, she was not satisfied and thought of a variation: suppose each cell contains multiple pieces of treasure, and each time you pass through a cell, you can collect at most one piece of treasure. With the other conditions remaining the same, what is the minimum number of paths required to collect all the treasure?

She cannot solve this one; can you help her?

Input

The first line contains a positive integer $t$, representing the number of test cases.

For each test case, the first line contains two positive integers $n$ and $m$, representing that the grid has $n$ rows and $m$ columns.

The next $n$ lines each contain $m$ non-negative integers, representing the amount of treasure in each cell ($0$ means no treasure).

Output

For each test case, output a single integer representing the minimum number of paths required.

Constraints

  • For 30% of the data, $n \le 5, m \le 5$, and the number of treasures in each cell does not exceed 5.
  • For 50% of the data, $n \le 100, m \le 100$, and the number of treasures in each cell does not exceed 1000.
  • For 100% of the data, $n \le 1000, m \le 1000$, and the number of treasures in each cell does not exceed $10^6$.

Examples

Input 1

1
3 3
0 1 5
5 0 0
1 0 0

Output 1

10

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.