This page is still under construction.

Parts of this page are still being built. What you see may change.

Omar Loves Candies

Time limit3sMemory limit128 MB

Summary
Find the largest sum of any non-empty sub-rectangle in a grid whose rows and columns strictly increase.
Level

Medium6 of 10

Topics
Prefix sum, Greedy, Matrix
Solved
No attempts yet

Problem

Omar loves eating a lot of candy, but unfortunately most candy is not healthy. His parents therefore gave every candy a score. A higher score means a healthier candy, and a score is an integer that can be positive, zero or negative.

One day Omar went shopping for candy with his parents and they found a strange store. The store lays its candy out in a grid of NN rows with MM candies in each row. Rows are numbered 11 to NN from top to bottom, columns are numbered 11 to MM from left to right, and every cell holds one candy.

The display follows a rule. Every candy outside the first row has a higher score than the candy directly above it, and every candy outside the first column has a higher score than the candy directly to its left.

There is only one way to buy candy here: pick a sub-rectangle of the grid and buy every candy inside it. A sub-rectangle is the set of cells shared by a block of consecutive rows r1r_1 through r2r_2 and a block of consecutive columns c1c_1 through c2c_2 (1≤r1≤r2≤N1 \le r_1 \le r_2 \le N, 1≤c1≤c2≤M1 \le c_1 \le c_2 \le M). A group of candies with any other shape cannot be picked.

Omar's parents want the non-empty sub-rectangle whose candy scores add up to the largest possible total. Find that total.

Input

The first line contains one integer TT, the number of test cases (1≤T≤1001 \le T \le 100).

Each test case begins with a line holding two integers separated by a single space, NN and MM (1≤N,M≤10001 \le N, M \le 1000), the dimensions of the candy grid. The next NN lines each contain the MM scores of one row, separated by single spaces. The grid satisfies the rule above, and every score is an integer between −2000-2000 and 20002000 inclusive.

Output

For each test case, print one line with the largest score sum reachable from a non-empty sub-rectangle.

Examples2

  1. Example 1

    Input
    1
    3 3
    -4 -2 -1
    -3 2 3
    1 4 5
    
    Expected output
    14
    
  2. Example 2

    Input
    2
    1 1
    -7
    2 3
    1 2 3
    4 5 6
    
    Expected output
    -7
    21