# Boss, Thirsty

## Problem Description

Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $ n $ days, numbered from day $ 1 $ to day $ n $ . There are also $ m $ types of drinks, numbered from $ 1 $ to $ m $ .

The profit gained from selling a drink on a particular day can vary. On day $ i $ , the projected profit from selling drink of type $ j $ is $ A_{i, j} $ . Note that $ A_{i, j} $ can be negative, meaning that selling the drink would actually incur a loss.

Pak Chanek wants to help his friend plan the sales over the $ n $ days. On day $ i $ , Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $ i $ and $ j $ such that $ 1 \leq i \leq j \leq m $ . Then all types of drinks between $ i $ and $ j $ (inclusive) will be sold.

However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $ i $ ( $ i>1 $ ) must meet the following conditions:

- At least one drink type sold on day $ i $ must also have been sold on day $ i-1 $ .
- At least one drink type sold on day $ i $ must not have been sold on day $ i-1 $ .

The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $ n $ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally?

## Input Format

Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 1000 $ ). The description of the test cases follows.

The first line of each test case contains two integers $ n $ and $ m $ ( $ 1 \leq n \leq 2 \cdot 10^5 $ ; $ 3 \leq m \leq 2 \cdot 10^5 $ ; $ n \cdot m \leq 2 \cdot 10^5 $ ) — the number of rows and columns in a grid.

The next $ n $ lines of each test case contain $ m $ integers each, where the $ i $ -th line contains the integers $ A_{i,1} A_{i,2}, \ldots, A_{i,m} $ ( $ -10^9 \leq A_{i,j} \leq 10^9 $ ) — project profits of each drink type on the $ i $ -th day.

It is guaranteed that the sum of $ n \cdot m $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

## Output Format

For each test case, output a single integer: the maximum profit that Pak Chanek can achieve.

## Sample #1

### Sample Input #1

```
1
3 6
79 20 49 5 -1000 500
-105 9 109 24 -98 -499
14 47 12 39 23 50
```

### Sample Output #1

```
475
```

## Hint

Here is Pak Chanek's optimal plan:

 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF2021D/7f3c895e123ba63a87bc7e1148e98588d4bb8d72.png)![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF2021D/4913b558091cf536ad505f423605a117c6964776.png)![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF2021D/1e52a9ae8bab076a8dbab9525e40f8a26b2cd856.png)- On day $ 1 $ , Pak Chanek sells drink types $ 1 $ to $ 3 $ . Generating a profit of $ 79+20+49 = 148 $ .
- On day $ 2 $ , Pak Chanek sells drink types $ 2 $ to $ 4 $ . Generating a profit of $ 9+109+24 = 142 $
- On day $ 3 $ , Pak Chanek sells drink types $ 1 $ to $ 6 $ . Generating a profit of $ 185 $ .

So, the total profit of Pak Chanek's plan is $ 148 + 142 + 185 = 475 $ .

## Limit



Time Limit
2.00s

Memory Limit
250.00MB

Tags: dp, greedy, implementation
Difficulty: 2500