Under construction

Still being built. Things may change

Matrix Fraud

Time limit1sMemory limit2048 MB

Summary
Find the fewest cell toggles that make every row a contiguous block of 1s, every column contain a 1, and both row endpoints never move left.
Level

Hard9 of 10

Topics
Dynamic programming, Prefix sum, Matrix
Solved
No attempts yet

Definitions

For this problem, a matrix is a binary matrix if every entry is 00 or 11. A binary matrix is a banded binary matrix if it satisfies the following properties:

  1. Every row has at least one 11.
  2. Every column has at least one 11.
  3. All 11s in each row are contiguous.
  4. For row i>1i > 1, let sis_i be the leftmost column with a 11 entry and tit_i the rightmost. Then si≥si−1s_i \ge s_{i-1} and ti≥ti−1t_i \ge t_{i-1}.

Problem

The Immoral Cartel of Pure Cozeners (ICPC) wants to publish results from a data set, but its matrix may not be banded. The group toggles cells, changing a 00 to a 11 or a 11 to a 00, so that the matrix becomes a banded binary matrix. Find the fewest toggles needed to turn the given matrix into a banded binary matrix.

Input

The first line contains two integers rr and cc (1≤r×c≤2⋅1051 \le r \times c \le 2 \cdot 10^5), the number of rows and columns. Each of the next rr lines contains a binary string of length cc, which is a row of the matrix.

Output

Print a single integer, the minimum number of entries to toggle to make the matrix a banded binary matrix.

Examples1

  1. Example 1

    Input
    3 4
    1100
    0101
    0011
    
    Expected output
    1