Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with maximum number of 0s.
Example:
Input:
N = 3
M[][] = {{1, 1, 0},
         {1, 1, 0},
         {1, 1, 0}}
Output:2
Explanation:
2nd column (0-based indexing) 
is having 3 zeros which is 
maximum among all columns.
 
Your Task:
Your task is to complete the function columnWithMaxZero() which should return the column number with the maximum number of zeros. If more than one column exists, print the one which comes first.
Constraints:
1 <= N <= 10^{2}
0 <= A[i][j] <= 1

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

    def columnWithMaxZeros(self,arr,N):

        # code here 

Solutions:
class Solution:

	def columnWithMaxZeros(self, arr, N):
		maxi = 0
		res = 0
		for i in range(N):
			d = 0
			for j in range(N):
				if arr[j][i] == 0:
					d += 1
			if d > maxi:
				res = i
				maxi = d
		return res


class Solution:

	def columnWithMaxZeros(self, arr, N):
		k = []
		for i in range(N):
			for j in range(N):
				if arr[i][j] == 0:
					k.append(j)
		p = list(set(k))
		g = []
		for i in range(len(p)):
			g.append(k.count(p[i]))
		m = max(g)
		d = [x for x in p if k.count(x) == m]
		return d[0]


class Solution:

	def columnWithMaxZeros(self, arr, N):
		big = []
		for i in range(N):
			count = 0
			for j in range(N):
				if arr[j][i] == 0:
					count += 1
				else:
					continue
			big.append(count)
		return big.index(max(big))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		total_count = 0
		index = -1
		for i in range(N):
			count = 0
			for j in range(N):
				if arr[j][i] == 0:
					count += 1
			if count > total_count:
				total_count = count
				index = i
		return index


class Solution:

	def columnWithMaxZeros(self, arr, N):
		ans = 0
		maxx = 0
		for i in range(N):
			nums = 0
			for j in range(N):
				if arr[j][i] == 0:
					nums += 1
			if nums > maxx:
				maxx = nums
				ans = i
		return ans


class Solution:

	def columnWithMaxZeros(self, arr, N):
		(mcc, mc) = (0, 0)
		for i in range(N):
			cc = 0
			for j in range(N):
				if arr[j][i] == 0:
					cc += 1
			if cc > mc:
				(mcc, mc) = (i, cc)
		return mcc


class Solution:

	def columnWithMaxZeros(self, arr, N):
		max1 = 0
		res = 0
		for i in range(N):
			z = 0
			for j in range(N):
				z += arr[j][i]
			z = N - 1 - z
			if z > max1:
				res1 = i
				max1 = z
		return res1


class Solution:

	def columnWithMaxZeros(self, arr, N):
		col = [0] * N
		for index in range(N):
			row = arr[index]
			for inner in range(N):
				col[inner] += 1 if row[inner] == 0 else 0
		return col.index(max(col))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		M = len(arr[0])
		c = 0
		cc = 0
		i = 0
		p = 0
		cc = 0
		cp = 0
		while p < M:
			i = 0
			v = 0
			c = 0
			for j in range(0, N):
				if arr[j][p] == 0:
					c += 1
			if c > cc:
				cc = c
				cp = p
			i += 1
			p += 1
		if c == 0:
			return -1
		return cp


class Solution:

	def columnWithMaxZeros(self, arr, N):
		ma = 0
		mc = 0
		for i in range(N):
			temp = 0
			for j in range(N):
				if arr[j][i] == 0:
					temp += 1
			if temp > ma:
				ma = temp
				mc = i
		return mc


class Solution:

	def columnWithMaxZeros(self, arr, N):
		(maxcount, col) = (0, -1)
		for i in range(N):
			count = 0
			for j in range(N):
				if arr[j][i] == 0:
					count += 1
			if count > maxcount:
				maxcount = count
				col = i
		return col


class Solution:

	def columnWithMaxZeros(self, arr, n):
		su = 0
		cnt = 0
		for i in range(n):
			count = 0
			for j in range(n):
				if arr[j][i] == 0:
					count += 1
			if cnt < count:
				cnt = count
				su = i
		return su


class Solution:

	def columnWithMaxZeros(self, arr, N):
		j = 0
		lst = []
		while j < N:
			count = 0
			for i in range(N):
				if arr[i][j] == 0:
					count += 1
			lst.append(count)
			j += 1
		return lst.index(max(lst))


class Solution:

	def columnWithMaxZeros(self, arr, n):
		d = {}
		for i in range(n):
			d[i] = 0
		for i in range(n):
			for j in range(n):
				if arr[i][j] == 0:
					d[j] += 1
		m = 0
		t = -1
		for i in d:
			if d[i] > m:
				m = d[i]
				t = i
		if t == -1:
			return -1
		return t


class Solution:

	def columnWithMaxZeros(self, arr, N):
		c = 0
		x = 0
		for k in range(N):
			z = 0
			for i in range(N):
				if arr[i][k] == 0:
					z += 1
			if x < z:
				x = z
				c = k
		return c


class Solution:

	def columnWithMaxZeros(self, arr, N):
		summat = [sum(col) for col in zip(*arr)]
		mini = float('inf')
		output = 0
		for i in range(N):
			if summat[i] < mini:
				mini = summat[i]
				output = i
		return output


class Solution:

	def columnWithMaxZeros(self, arr, N):
		z = list(zip(*arr))
		c = 0
		ind = 0
		for i in z:
			if i.count(0) > c:
				c = i.count(0)
				ind = z.index(i)
		return ind


class Solution:

	def columnWithMaxZeros(self, arr, N):
		max = float('-inf')
		min_ind = -1
		for j in range(N):
			count = 0
			for i in range(N):
				if arr[i][j] == 0:
					count += 1
			if count > max:
				max = count
				if min_ind < j:
					min_ind = j
		return min_ind


class Solution:

	def columnWithMaxZeros(self, arr, N):
		a = 0
		m = 0
		for i in range(N):
			n = 0
			for j in range(N):
				if arr[j][i] == 0:
					n += 1
			if n > m:
				m = n
				a = i
		return a


class Solution:

	def columnWithMaxZeros(self, arr, n):
		res = 0
		maxi = 0
		for j in range(n):
			c = 0
			for i in range(n):
				if arr[i][j] == 0:
					c += 1
			if maxi < c:
				maxi = c
				res = j
		return res


class Solution:

	def columnWithMaxZeros(self, M, N):
		l = []
		count = 0
		for i in range(0, N):
			for j in range(0, N):
				if M[j][i] == 0:
					count += 1
			l.append(count)
			count = 0
		return l.index(max(l))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		d = {}
		for i in range(N):
			for j in range(N):
				if arr[i][j] == 0:
					if j in d:
						d[j] += 1
					else:
						d[j] = 1
		if d == {}:
			return -1
		m = max(d.values())
		for i in d:
			if d[i] == m:
				return i


class Solution:

	def columnWithMaxZeros(self, arr, N):
		max_zero = 0
		max_index = -1
		for i in range(N):
			count_zero = 0
			for j in range(N):
				if arr[j][i] == 0:
					count_zero = count_zero + 1
			if count_zero > max_zero:
				max_zero = count_zero
				max_index = i
		return max_index


class Solution:

	def columnWithMaxZeros(self, arr, N):
		ls = []
		row = 0
		col = 0
		while col < N:
			r = row
			c = col
			count = 0
			while r < N:
				if arr[r][c] == 0:
					count += 1
				r += 1
			ls.append(count)
			col += 1
		x = ls.index(max(ls))
		return x


import numpy as np

class Solution:

	def columnWithMaxZeros(self, arr, N):
		a = np.array(arr)
		b = np.sum(a, axis=0)
		c = min(b)
		for i in range(N):
			if b[i] == c:
				return i


class Solution:

	def columnWithMaxZeros(self, arr, N):
		dict = {}
		for i in range(N):
			for j in range(len(arr[i])):
				if arr[i][j] == 0:
					dict[j] = dict.get(j, 0) + 1
		value = max(dict.values())
		for (k, v) in dict.items():
			if v == value:
				return k


class Solution:

	def columnWithMaxZeros(self, arr, N):
		for i in range(N):
			for j in range(i):
				(arr[i][j], arr[j][i]) = (arr[j][i], arr[i][j])
		maxm = 0
		col = 0
		for i in arr:
			if i.count(0) > maxm:
				maxm = i.count(0)
				col = arr.index(i)
		return col


class Solution:

	def columnWithMaxZeros(self, arr, N):
		min_total = N + 1
		min_col = -1
		for col in range(N):
			total = 0
			for row in range(N):
				total += arr[row][col]
			if total < min_total:
				min_total = total
				min_col = col
		return min_col


class Solution:

	def columnWithMaxZeros(self, arr, N):
		maxnum = 0
		ans = 0
		for i in range(N):
			t = 0
			for j in range(N):
				if arr[j][i] == 0:
					t += 1
			if t > maxnum:
				maxnum = t
				ans = i
		return ans


class Solution:

	def columnWithMaxZeros(self, arr, n):
		max_val = 0
		col_idx = 0
		for i in range(n):
			c = 0
			for j in range(n):
				if arr[j][i] == 0:
					c += 1
			if c > max_val:
				max_val = c
				col_idx = i
		return col_idx


class Solution:

	def columnWithMaxZeros(self, arr, n):
		dic = {}
		for i in range(n):
			for j in range(n):
				if arr[i][j] == 0:
					if j in dic:
						dic[j] += 1
					else:
						dic[j] = 1
		res = ''
		prev = 0
		for (key, value) in dic.items():
			if value > prev:
				res = key
				prev = value
		return res


class Solution:

	def columnWithMaxZeros(self, arr, N):
		max_c = 0
		max_col = 0
		for col in range(N):
			c = 0
			for row in range(0, N):
				ele = arr[row][col]
				if ele == 0:
					c += 1
			if c > max_c:
				max_c = c
				max_col = col
		return max_col


class Solution:

	def columnWithMaxZeros(self, a, n):
		maxx = 0
		x = 0
		for i in range(n):
			count = 0
			for j in range(n):
				if a[j][i] == 0:
					count += 1
			if count > maxx:
				maxx = max(maxx, count)
				x = i
		return x


class Solution:

	def columnWithMaxZeros(self, arr, n):
		ans = 0
		maxcnt = 0
		for j in range(n):
			cnt = 0
			for i in range(n):
				if arr[i][j] == 0:
					cnt += 1
			if maxcnt != max(cnt, maxcnt):
				ans = j
				maxcnt = cnt
		return ans


class Solution:

	def columnWithMaxZeros(self, arr, N):
		(max_count, ans) = (0, 0)
		for i in range(N):
			temp = 0
			for j in range(N):
				if arr[j][i] == 0:
					temp += 1
			if temp > max_count:
				max_count = temp
				ans = i
		return ans


class Solution:

	def columnWithMaxZeros(self, arr, N):
		colsum = []
		for i in range(N):
			xsm = 0
			for j in range(N):
				xsm += arr[j][i]
			colsum.append(xsm)
		return colsum.index(min(colsum))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		bufferArr = []
		count = 0
		coloum = 0
		for ColNum in range(N):
			for List in arr:
				bufferArr.append(List[ColNum])
			if bufferArr.count(0) > count:
				coloum = ColNum
				count = bufferArr.count(0)
			bufferArr.clear()
		return coloum


class Solution:

	def columnWithMaxZeros(self, arr, N):
		num_zero = []
		x = 0
		for i in range(N):
			count = 0
			for j in range(N):
				if arr[j][i] == 0:
					count += 1
			num_zero.append(count)
		x = num_zero.index(max(num_zero))
		return x


class Solution:

	def columnWithMaxZeros(self, arr, N):
		colsum1 = 0
		index1 = 0
		for i in range(len(arr)):
			colsum = 0
			index = i
			for j in range(len(arr)):
				if arr[j][i] == 0:
					colsum += 1
			if colsum > colsum1:
				colsum1 = colsum
				index1 = index
		return index1


class Solution:

	def columnWithMaxZeros(self, arr, N):
		c = []
		for i in range(0, len(arr)):
			j = 0
			for row in arr:
				if row[i] == 0:
					j += 1
			c.append(j)
		m = c.index(max(c))
		return m


class Solution:

	def columnWithMaxZeros(self, arr, N):
		col = []
		for i in range(N):
			Li = []
			for j in range(N):
				Li.append(arr[j][i])
			col.append(Li)
		Max = 0
		index = 0
		for i in range(N):
			count = 0
			for j in col[i]:
				if j == 0:
					count += 1
			if count > Max:
				Max = count
				index = i
		return index


class Solution:

	def columnWithMaxZeros(self, arr, N):
		l = 0
		m = 0
		for i in range(N):
			c = 0
			for j in range(N):
				if arr[j][i] == 0:
					c = c + 1
			if c > l:
				l = c
				m = i
		return m


class Solution:

	def columnWithMaxZeros(self, arr, N):
		c = 0
		l = []
		for i in range(N):
			for j in arr:
				if j[i] == 0:
					c += 1
			l.append(c)
			c = 0
		return l.index(max(l))


import sys

class Solution:

	def columnWithMaxZeros(self, arr, N):
		maxi = -sys.maxsize
		column = 0
		for i in range(0, N):
			count = 0
			for j in range(0, N):
				if arr[j][i] == 0:
					count += 1
				if count > maxi:
					maxi = count
					column = i
		return column


class Solution:

	def columnWithMaxZeros(self, arr, N):
		d = {}
		ma = 0
		key = 0
		for i in range(N):
			for j in range(N):
				if j not in d:
					if arr[i][j] == 0:
						d[j] = 1
				elif arr[i][j] == 0:
					d[j] += 1
		for (i, j) in d.items():
			if j > ma:
				ma = j
				key = i
		return key


class Solution:

	def columnWithMaxZeros(self, arr, N):
		r = []
		for i in range(N):
			a = 0
			for j in range(N):
				if arr[j][i] == 0:
					a += 1
			r.append(a)
		indexx = r.index(max(r))
		return indexx


class Solution:

	def columnWithMaxZeros(self, arr, N):
		k = []
		for i in range(len(arr)):
			res = [sub[i] for sub in arr]
			k.append(res.count(0))
		l = max(k)
		return k.index(l)


class Solution:

	def columnWithMaxZeros(self, arr, N):
		a = 0
		c = 0
		f = 0
		for i in range(len(arr)):
			c = 0
			for j in range(len(arr[0])):
				if arr[j][i] == 0:
					c = c + 1
			if c > a:
				f = i
				a = c
		return f


class Solution:

	def columnWithMaxZeros(self, arr, N):
		maxzero = -1
		for j in range(N):
			c = 0
			for i in range(N):
				if arr[i][j] == 0:
					c += 1
				if c == maxzero:
					continue
				if c > maxzero:
					maxzero = c
					ans = j
		return ans


class Solution:

	def columnWithMaxZeros(self, arr, N):
		ans = 0
		c = 0
		fans = 0
		for i in range(len(arr)):
			c = 0
			for j in range(len(arr[0])):
				if arr[j][i] == 0:
					c = c + 1
			if c > ans:
				fans = i
				ans = c
		return fans


class Solution:

	def columnWithMaxZeros(self, arr, N):
		x = []
		for j in range(0, N):
			c = 0
			for i in range(0, N):
				c = c + arr[i][j]
			x.append(c)
		for i in range(0, N):
			if x[i] == min(x):
				return i


class Solution:

	def columnWithMaxZeros(self, arr, N):
		max = 0
		index = 0
		res = []
		i = 0
		while i < N:
			zeroCount = 0
			for j in range(N):
				if arr[j][i] == 0:
					zeroCount += 1
			if zeroCount > max:
				max = zeroCount
				index = i
			i += 1
		return index


class Solution:

	def columnWithMaxZeros(self, arr, N):
		count = 0
		max_count = 0
		for col in range(N):
			count = 0
			for row in range(N):
				if arr[row][col] == 0:
					count += 1
			if count > max_count:
				max_count = count
				res = col
		return res


class Solution:

	def columnWithMaxZeros(self, arr, N):
		num_dict = {}
		for i in range(N - 1, -1, -1):
			num = 0
			for j in range(N):
				if arr[j][i] == 0:
					num += 1
					num_dict[num] = i
					max_0 = max(num_dict)
		return num_dict[max_0]


class Solution:

	def columnWithMaxZeros(self, arr, N):
		res = []
		res2 = []
		for i in range(N):
			for j in range(N):
				res2.append(arr[j][i])
			res.append(res2)
			res2 = []
		fin = []
		m = 0
		for i in range(len(res)):
			fin.append([i, res[i].count(0)])
			m = max(m, res[i].count(0))
		for i in fin:
			if i[1] == m:
				a = i[0]
				break
		return a


class Solution:

	def columnWithMaxZeros(self, arr, N):
		max_c = 0
		ind = 0
		for i in range(N):
			count = 0
			for j in range(N):
				if arr[j][i] == 0:
					count += 1
			if max_c < count:
				max_c = count
				ind = i
		return ind


class Solution:

	def columnWithMaxZeros(self, arr, N):
		count = 0
		arr1 = []
		a = 0
		x = int(a)
		for i in range(N):
			for j in range(N):
				if arr[j][i] == x:
					count += 1
			arr1.append(count)
			count = 0
		maximum = max(arr1)
		for i in range(len(arr1)):
			if arr1[i] == maximum:
				break
		return i


class Solution:

	def columnWithMaxZeros(self, arr, N):
		col = 0
		maxc = 0
		for i in range(N):
			c = 0
			for j in range(N):
				if arr[j][i] == 0:
					c += 1
			if maxc < c:
				col = i
			maxc = max(maxc, c)
		return col


class Solution:

	def columnWithMaxZeros(self, arr, N):
		n = len(arr)
		m = len(arr[0])
		cnt = [0] * m
		for i in range(n):
			for j in range(m):
				cnt[j] += arr[i][j]
		mini = min(cnt)
		return cnt.index(mini)


class Solution:

	def columnWithMaxZeros(self, arr, N):
		(zc, out) = (0, 0)
		for i in range(N):
			col = 0
			for j in range(N):
				if arr[j][i] == 0:
					col += 1
			if col > zc:
				zc = col
				out = i
		return out


class Solution:

	def columnWithMaxZeros(self, arr, N):
		mydict = dict()
		for i in range(N):
			for j in range(N):
				if arr[i][j] == 0:
					if j not in mydict:
						mydict[j] = 1
					else:
						mydict[j] += 1
		maxm = -1
		res = -1
		for i in mydict:
			if mydict[i] > maxm:
				maxm = max(maxm, mydict[i])
				res = i
		return res


class Solution:

	def columnWithMaxZeros(self, arr, N):
		cnt = [0 for j in range(N)]
		for i in range(N):
			for j in range(N):
				ele = arr[i][j]
				if ele == 0:
					cnt[j] += 1
		maxi = max(cnt)
		maxpos = cnt.index(maxi)
		return maxpos
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = []
		for i in range(N):
			line = [int(x) for x in input().strip().split()]
			arr.append(line)
		ob = Solution()
		print(ob.columnWithMaxZeros(arr, N))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		ls = []
		for i in range(N):
			s = 0
			for j in range(N):
				s += arr[j][i]
			ls.append(s)
		m = min(ls)
		return ls.index(m)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = []
		for i in range(N):
			line = [int(x) for x in input().strip().split()]
			arr.append(line)
		ob = Solution()
		print(ob.columnWithMaxZeros(arr, N))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		maxx = 0
		cmaxx = 0
		flag = 0
		for col in range(N):
			count = 0
			for row in range(N):
				if arr[row][col] == 0:
					count += 1
			if count > maxx:
				maxx = count
				cmaxx = col
				flag = 1
		if flag == 1:
			return cmaxx
		else:
			return -1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = []
		for i in range(N):
			line = [int(x) for x in input().strip().split()]
			arr.append(line)
		ob = Solution()
		print(ob.columnWithMaxZeros(arr, N))


class Solution:

	def columnWithMaxZeros(self, arr, N):
		highnum = 0
		col = 0
		for i in range(N):
			temp = 0
			for j in range(N):
				if arr[j][i] == 0:
					temp += 1
				if temp > highnum:
					highnum = temp
					col = i
		return col