Stock Charts

Given N piecewise-linear price graphs over K time points, find the minimum number of charts so that no two graphs on a chart intersect.

Medium6GeometryIntervalsGreedyNo attempts yetTime limit2sMemory limit512 MB

Problem

Minho owns NN stock products. Each product has KK yearly prices recorded in time order.

Minho wants to draw one line graph per product, joining that product's prices in time order, so he can read the price changes at a glance. Giving every product its own chart takes too much space, so he wants to spread all of the graphs over as few charts as possible.

Overlapping or crossing segments make the changes hard to read, so the graphs drawn on one chart must never meet. Two graphs count as overlapping if they touch at even a single point.

Find the smallest number of charts needed to draw the graphs of every stock product under this rule.

Input

The first line contains the number of test cases TT.

The first line of each test case contains NN and KK separated by a space (1N1001 \le N \le 100, 1K251 \le K \le 25). NN is the number of stocks to chart and KK is the number of prices recorded for each product.

Each of the next NN lines contains the prices of one product. The jj-th number on the ii-th line, PijP_{ij}, is the price of product ii at time jj (0Pij10000000 \le P_{ij} \le 1000000).

Output

For each test case, print on its own line the minimum number of charts needed to draw every price graph without overlapping or crossing.