P18091 [CCPC 2026 Changchun] Yet Another Pen-and-Paper Problem

Background

Yana, Mino, White and Huzz came from Shanghai to Changchun, and this time they are facing a pen-and-paper problem.

Description

Given an $n$-row, $m$-column matrix $a_{i,j}$ of non-negative integers, you may perform the following operation any number of times: - Choose a column interval $[l,r]$ with $1\le l\le r\le m$, then choose $r-l+1$ row coordinates $p_l,p_{l+1},\dots,p_r$ with $1\le p_l\le p_{l+1}\le \dots\le p_r\le n$, and subtract $1$ from the elements at $(p_l,l),(p_{l+1},l+1),\dots,(p_r,r)$ in $a$. Find the minimum number of operations needed to turn $a$ into the zero matrix.

Input Format

The first line of the input contains two positive integers $n,m$ $(1\le n,m\le 10^6,nm\le 10^6)$, the number of rows and columns of the matrix $a$. The next $n$ lines describe the matrix: the $i$-th line contains $m$ non-negative integers $a_{i,1},a_{i,2},\dots,a_{i,m}$ $(0\le a_{i,j}\le 10^9)$, the elements of the matrix $a$.

Output Format

Output a single integer, the minimum number of operations needed to turn the matrix $a$ into the zero matrix.

Explanation/Hint

For sample 1, one can prove that turning $a$ into the zero matrix needs at least $7$ operations. The following is a feasible optimal plan: (in the figure, dots of the same color indicate the positions chosen by the same operation, and the number of the corresponding color indicates the index of the operation) :::align{center} ![](https://cdn.luogu.com.cn/upload/image_hosting/uptoq136.webp) ::: Translated by QwenWork