Portal solution codeforces

Portal solution codeforces

CQXYM found a rectangle AA of size n×mn×m. There are nn rows and mm columns of blocks. Each block of the rectangle is an obsidian block or empty. CQXYM can change an obsidian block to an empty block or an empty block to an obsidian block in one operation.

A rectangle MM size of a×ba×b is called a portal if and only if it satisfies the following conditions:

  • a5,b4a≥5,b≥4.
  • For all 1<x<a1<x<a, blocks Mx,1Mx,1 and Mx,bMx,b are obsidian blocks.
  • For all 1<x<b1<x<b, blocks M1,xM1,x and Ma,xMa,x are obsidian blocks.
  • For all 1<x<a,1<y<b1<x<a,1<y<b, block Mx,yMx,y is an empty block.

Note that the there must be aa rows and bb columns, not bb rows and aa columns.CQXYM wants to know the minimum number of operations he needs to make at least one sub-rectangle a portal.

Portal solution codeforces

The first line contains an integer tt (t1t≥1), which is the number of test cases.

For each test case, the first line contains two integers nn and mm (5n4005≤n≤4004m4004≤m≤400).

Then nn lines follow, each line contains mm characters 00 or 11. If the jj-th character of ii-th line is 00, block Ai,jAi,j is an empty block. Otherwise, block Ai,jAi,j is an obsidian block.

It is guaranteed that the sum of nn over all test cases does not exceed 400400.

It is guaranteed that the sum of mm over all test cases does not exceed 400400.

Portal solution codeforces

Output tt answers, and each answer in a line.

Examples
input

Copy
1
5 4
1000
0000
0110
0000
0001

Portal solution codeforces

Copy
12
input

Cop

Portal solution codeforces

1
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000
output

Copy
5

Portal solution codeforces

In the first test case, the final portal is like this:

1110
1001
1001
1001
0111

Leave a Comment

Your email address will not be published.