Bandwidth of a matrix A is defined as the smallest non-negative integer K such that A(i, j) = 0 for |i - j| > K.
For example, a matrix with all zeros will have its bandwith equal to zero. Similarly bandwith of diagonal matrix will also be zero.

For example, for the below given matrix, the bandwith of this matrix is 2.

1 0 0
0 1 1
1 1 0 

Bandwidth of the below matrix is 1. 

Bandwidth of the below matrix is 2. 

Bandwidth of the below matrix is also 2. 

You will be a given a binary matrix A of dimensions N × N. You are allowed to make following operation as many times as you wish (possibly zero or more). In a single operation, you can swap any two entries of the matrix. Your aim is to minimize the bandwidth of the matrix. Find the minimum bandwidth of the matrix A you can get after making as many operations of above type as you want.

-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follow.
First line of each test case contains an integer N denoting the height/width of the matrix.
Next N lines of each test case contain N space separated binary integers (either zero or one) corresponding to the entries of the matrix.

-----Output-----
For each test case, output a single integer corresponding to the minimum bandwidth that you can obtain.

-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 500
- 0 ≤ A(i, j) ≤ 1

-----Subtasks-----
- Subtask #1 (40 points) : 1 ≤ N ≤ 100
- Subtask #2 (60 points) : original constraints

-----Example-----
Input:
6
2
0 0
0 0
2
1 0
0 1
2
1 0
1 0
2
1 0
1 1
3
1 0 0
0 1 1
1 1 0
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

Output:
0
0
0
1
1
3

-----Explanation-----
Example case 1. The bandwidth of a matrix will all zero entries will be zero. This is the minimum bandwidth you can get, so there is no need of performing any swap operation.
Example case 2. The bandwidth of a diagonal matrix will also be zero.
Example case 3. You can make the given matrix a diagonal matrix by swapping A(2, 1) and A(2, 2), which will have zero bandwidth.
Example case 4. You can not make swaps in any way that can reduce the bandwidth of this matrix. Bandwidth of this matrix is equal to 1, which is the minimum bandwidth that you can get.
Example case 5. Bandwidth of the given matrix is 2. You can make it equal to be 1 by swapping A(3, 1) and A(3, 3), i.e. the matrix after the operation will look like

1 0 0
0 1 1
0 1 1

The bandwidth of this matrix is 1.

Example case 6. The swap operations won't have any effect on the matrix. Its bandwidth is equal to 3.

Difficulty: interview

Starter code:

Solutions:
t = int(input())
for i in range(t):
    n = int(input())
    A = []
    for i in range(0, n):
        A.append([int(i) for i in input().split()])
    ones = sum([sum(i) for i in A])
    compare = n
    ans = 0
    for i in range(0, n):
        if ones <= compare:
            ans = i
            break
        compare += 2*(n-1-i)
    print(ans)

# cook your dish here
try:
    
    for i in range(int(input())):
        n = int(input())
        A = []
        for i in range(0, n):
            A.append([int(i) for i in input().split()])
        ones = sum([sum(i) for i in A])
        compare = n
        ans = 0
        for i in range(0, n):
            if ones <= compare:
                ans = i
                break
            compare += 2*(n-1-i)
        print(ans)
except:
    pass

# cook your dish here
T = int(input())
for i in range(T):
    n = int(input())
    A = []
    for i in range(0, n):
        A.append([int(i) for i in input().split()])
    ones = sum([sum(i) for i in A])
    compare = n
    ans = 0
    for i in range(0, n):
        if ones <= compare:
            ans = i
            break
        compare += 2*(n-1-i)
    print(ans)

# cook your dish here
#problem code: BANDMATR, language: python3.6
for __ in range(int(input())):
    a = []
    n = int(input())
    for i in range(n):
        a.append(list(map(int,input().split())))
    count=0
    for i in range(n):
        for j in range(n):            
            if a[i][j]==0:
                count+=1
    temp = n-1
    for k in range(n-1):
        arb = int((n-1-k)*(n - k))
        if count >= arb:
            temp = k
            break
    print(temp)   

# cook your dish here
try:
    T=int(input())
    nsw=[]
    for _ in range(T):
        N=int(input())
        a=[]
        for __ in range(N): 
            a+=list(map(int, input().rstrip().split()))
        count=0
        for ___ in a:
            if 1==___:
                count+=1
        count=count-N
        ans=0;
        while count>0:
            ans+=1
            count=count-(2*(N-1))
            N=N-1
            
        nsw+=[ans]
    print('\n'.join(map(str,nsw)))
except EOFError:
    pass

try:
    t = int(input())
    for _ in range(t):
        N = int(input())
        c = 0
        p = N-1
        a = [100 for i in range(N)]
        a[0] = N 
        for i in range(1,N):
            a[i] = a[i-1]+2*p 
            p-=1 
        for j in range(N):
            row = input().split(' ')
            for k in range(N):
                if int(row[k]) == 1:
                    c+=1 
        for i in range(N):
            if c <= a[i]:
                print(i)
                break
except:
    pass
        
    


#problem code: BANDMATR, language: python3.6
for __ in range(int(input())):
    a = []
    n = int(input())
    for i in range(n):
        a.append(list(map(int,input().split())))
    count=0
    for i in range(n):
        for j in range(n):            
            if a[i][j]==0:
                count+=1
    temp = n-1
    for k in range(n-1):
        arb = int((n-1-k)*(n - k))
        if count >= arb:
            temp = k
            break
    print(temp)

# cook your dish here
try:
    T=int(input())
    nsw=[]
    for _ in range(T):
        N=int(input())
        a=[]
        for __ in range(N): 
            a+=list(map(int, input().rstrip().split()))
        count=0
        for ___ in a:
            if 1==___:
                count+=1
        count=count-N
        ans=0;
        while count>0:
            ans+=1
            count=count-(2*(N-1))
            N=N-1
            
        nsw+=[ans]
    print('\n'.join(map(str,nsw)))
except EOFError:
    pass

# cook your dish here
t=int(input())
for _ in range(t):
    n=int(input())
    a=[]
    for i in range(n):
        lis=[int(inp) for inp in input().split()]
        a.append(lis)
    zer_count=0
    for i in range(n):
        for j in range(n):
            if a[i][j]==0:
                zer_count+=1
    count=0
    while count<=n:
        zer_count-=2*(count+1)
        if zer_count<0:
            break
        else:
            count+=1
    print(n-1-count)

# cook your dish here
t=int(input())
while t>0 :
    n=int(input())
    l=[list(map(int,input().split())) for i in range(n)]
    c=0 
    for i in l :
        c+=i.count(0)
    c=c//2 
    i=1 
    while c>=0 :
        c-=i
        i+=1
    print(n-i+1)
    t-=1

# cook your dish here
T = int(input())
for i in range(T):
    n = int(input())
    A = []
    for i in range(0, n):
        A.append([int(i) for i in input().split()])
    ones = sum([sum(i) for i in A])
    compare = n
    ans = 0
    for i in range(0, n):
        if ones <= compare:
            ans = i
            break
        compare += 2*(n-1-i)
    print(ans)

# cook your dish here
T = int(input())
for i in range(T):
    n = int(input())
    A = []
    for i in range(0, n):
        A.append([int(i) for i in input().split()])
    ones = sum([sum(i) for i in A])
    compare = n
    ans = 0
    for i in range(0, n):
        if ones <= compare:
            ans = i
            break
        compare += 2*(n-1-i)
    print(ans)

for _ in range(int(input())):
    n = int(input())
    z = 0
    for _ in range(n):
        z += input().strip().split(' ').count('0')
    
    k = 2
    r = n - 1
    cs = k
    while cs <= z:
        r -= 1
        k += 2
        cs += k
    print(r)


for _ in range(int(input())):
    n = int(input())
    d = {}
    i = 0
    while (i < n):
        j = 0
        o = []
        o = list(map(int, input().split()))
        while (j < len(o)):
            x = o[j]
            d[(i, j)] = x
            j = j + 1

        i = i + 1

    do = {}
    dz = {}

    i = 0
    while(i < n):
        j = 0
        while(j < n):
            e = d[(i,j)]
            ri = i + 1
            rj = j + 1
            y = abs(ri - rj)
            if e == 1:
                if y in do:
                    do[y] = do[y]+1

                else:
                    do[y] = 1

            else:
                if y in dz:
                    dz[y] = dz[y] + 1

                else:
                    dz[y] = 1

            j = j+1

        i = i+1

    lo = sorted(do.keys())
    lz = sorted(dz.keys())
    lo = lo[::-1]
    #print(lo)
    #print(lz)
    #print(do)
    #print(dz)

    i = 0
    j = 0
    if i == len(lo):
        print(0)
        continue

    if j == len(lz):
        print(lo[i])
        continue

    while((lo[i] != lz[j]) and (lo[i] > lz[j])):
        ov = lo[i]
        zv = lz[j]
        #print(ov,zv,3)
        do[ov] = do[ov]-1
        dz[zv] = dz[zv]-1

        if do[ov] == 0:
            i = i+1
            if i == len(lo):
                break

        if dz[zv] == 0:
            j = j+1
            if j == len(lz):
                break

    if i != len(lo):
        print(lo[i])

    else:
        print(lz[j])

for _ in range(int(input())):
    n = int(input())
    d = {}
    i = 0
    while (i < n):
        j = 0
        o = []
        o = list(map(int, input().split()))
        while (j < len(o)):
            x = o[j]
            d[(i, j)] = x
            j = j + 1

        i = i + 1

    do = {}
    dz = {}

    i = 0
    while(i < n):
        j = 0
        while(j < n):
            e = d[(i,j)]
            ri = i + 1
            rj = j + 1
            y = abs(ri - rj)
            if e == 1:
                if y in do:
                    do[y] = do[y]+1

                else:
                    do[y] = 1

            else:
                if y in dz:
                    dz[y] = dz[y] + 1

                else:
                    dz[y] = 1

            j = j+1

        i = i+1

    lo = sorted(do.keys())
    lz = sorted(dz.keys())
    lo = lo[::-1]
    #print(lo)
    #print(lz)
    #print(do)
    #print(dz)

    i = 0
    j = 0
    if i == len(lo):
        print(0)
        continue

    if j == len(lz):
        print(lo[i])
        continue

    while((lo[i] != lz[j]) and (lo[i] > lz[j])):
        ov = lo[i]
        zv = lz[j]
        #print(ov,zv,3)
        do[ov] = do[ov]-1
        dz[zv] = dz[zv]-1

        if do[ov] == 0:
            i = i+1
            if i == len(lo):
                break

        if dz[zv] == 0:
            j = j+1
            if j == len(lz):
                break

    print(lo[i])

for _ in range(int(input())):
    n = int(input())
    d = {}
    i = 0
    while (i < n):
        j = 0
        o = []
        o = list(map(int, input().split()))
        while (j < len(o)):
            x = o[j]
            d[(i, j)] = x
            j = j + 1

        i = i + 1

    do = {}
    dz = {}

    i = 0
    while(i < n):
        j = 0
        while(j < n):
            e = d[(i,j)]
            ri = i + 1
            rj = j + 1
            y = abs(ri - rj)
            if e == 1:
                if y in do:
                    do[y] = do[y]+1

                else:
                    do[y] = 1

            else:
                if y in dz:
                    dz[y] = dz[y] + 1

                else:
                    dz[y] = 1

            j = j+1

        i = i+1

    lo = sorted(do.keys())
    lz = sorted(dz.keys())
    lo = lo[::-1]
    #print(lo)
    #print(lz)
    #print(do)
    #print(dz)
    if len(lo) == 0:
        print(0)
        continue

    if len(lz) == 0:
        print(lo[0])
        continue

    i = 0
    j = 0
    while((lo[i] != lz[j]) and (lo[i] > lz[j])):
        ov = lo[i]
        zv = lz[j]
        #print(ov,zv,3)
        do[ov] = do[ov]-1
        dz[zv] = dz[zv]-1

        if do[ov] == 0:
            i = i+1
            if i == len(lo):
                break

        if dz[zv] == 0:
            j = j+1
            if j == len(lz):
                break

    print(lo[i])

def __starting_point():

    for _ in range(int(input())):
        res = 0
        n = int(input())
        m = []
        for i in range(n):
            m.append(list(map(int, input().split())))
        zeroes = 0
        for a in m:
            for el in a:
                if el == 0:
                    zeroes = zeroes + 1
        for i in range(n):
            if i < n-1:
                x = i * 2 + 2
            else:
                x = n
            if x > zeroes:
                res = i
                break
            elif x == zeroes:
                res = i + 1
                break
            else:
                zeroes = zeroes - x
        if res == n:
            res = res - 1
        print(n-1-res)


__starting_point()

# cook your dish here

t = int(input())

fact = []
for i in range(500):
    fact.append((i*(i+1))//2)
#print(fact)
    
    
def search(fact, n, low, high, key):
    mid = (low + high)//2
    #print(low, high, mid, fact[mid], fact[mid+1], fact)
    if fact[mid] == key:
        return mid
    elif fact[mid] < key and mid == n-1:
        return mid
    elif fact[mid] < key and fact[mid+1] > key:
        return mid
    elif fact[mid] < key and fact[mid+1] == key:
        return mid+1
    elif fact[mid] < key:
        return search(fact, n, mid+1, high, key)
    else:
        return search(fact, n, low, mid-1, key)

for _ in range(t):
    n = int(input())
    
    M = [[int(a) for a in input().strip().split()] for i in range(n)]
    
    count = 0
    for i in range(n):
        for j in range(n):
            count = count + 1 if M[i][j] == 0 else count
    
    i = search(fact, len(fact), 0, len(fact), count//2)
    #print(count, i, fact[i], n, n-i-1)
    print(0 if count >= fact[n-1]*2 else n-i-1)
    


for _ in range(int(input().strip())):
	n = int(input().strip())
	total_ones = 0
	for _ in range(n):
		total_ones += sum(list(map(int, input().strip().split())))
	i = 0
	total_ones -= n
	while total_ones>0:
		i += 1
		total_ones -= 2*(n-i)
	print(i)

import itertools
for _ in range(int(input())):
    N = int(input());A = list()
    for i in range(N):
        B = list(map(int,input().split(" ")))
        A.append(B)
    C = list(itertools.chain.from_iterable(A))
    Z = C.count(0);temp = N-1
    for i in range(N-1):
        y = int((N-1-i)*(N - i))
        if Z >= y:
            temp = i
            break
    print(temp)

from sys import stdin

T = int(stdin.readline())

for i in range(0, T):
    n = int(stdin.readline())
    A = []
    for i in range(0, n):
        A.append([int(i) for i in stdin.readline().split(' ')])
    ones = sum([sum(i) for i in A])
    compare = n
    ans = 0
    for i in range(0, n):
        if ones <= compare:
            ans = i
            break
        compare += 2*(n-1-i)
    print(ans)

from math import sqrt
for _ in range(int(input())):
    n=int(input())
    l=[0]*n
    c=0
    for i in range(n):
        l[i]=list(map(int,input().split()))
        c=c+l[i].count(0)
    q=c//2
    a=(-1+sqrt(1+8*q))//2
    print(int(n-a-1))

# cook your dish here
def f(n, c):
	# print("f", n, c)
	if c <= n:
		return 0
	if c == n * n:
		return n-1
	e = n-1
	s = n
	while c > s:
		s += 2 * e
		e -= 1
	return n-1-e
# hat = n * (n+1) // 2
# if c <= hat:
# 	return 0
# return

t = int(input())
for it in range(t):
	n = int(input())
	c = 0
	for i in range(n):
		a = input().split()
		c += a.count('1')
	print(f(n, c))


def f(n, c):
	# print("f", n, c)
	if c <= n:
		return 0
	if c == n * n:
		return n-1
	e = n-1
	s = n
	while c > s:
		s += 2 * e
		e -= 1
	return n-1-e
# hat = n * (n+1) // 2
# if c <= hat:
# 	return 0
# return

t = int(input())
for it in range(t):
	n = int(input())
	c = 0
	for i in range(n):
		a = input().split()
		c += a.count('1')
	print(f(n, c))


for t in range(int(input())):
    n = int(input())
    matrix = []
    count1 = 0
    for i in range(n):
        matrix.append(list(map(int, input().split())))
        count1 += matrix[-1].count(1)
    bandwidth = 0
    count1 -= n
    for i in range(n-1,0, -1):
        if count1 > 0:
            count1 -= 2 * i
            bandwidth += 1
    print(bandwidth)