Help me in solving GRDXOR problem

My code

``````def max_xor_sum(T, test_cases):
result = []

for _ in range(T):
N, M = test_cases[_][0]
matrix = test_cases[_][1]

row_xor_sum = [0] * N
col_xor_sum = [0] * M

# Calculate XOR sums of each row
for i in range(N):
for j in range(M):
row_xor_sum[i] ^= matrix[i][j]

# Calculate XOR sums of each column
for j in range(M):
for i in range(N):
col_xor_sum[j] ^= matrix[i][j]

max_sum = 0

for i in range(N):
for j in range(M):
# Calculate sum by combining row and column XOR sums
curr_sum = row_xor_sum[i] + col_xor_sum[j] - 2 * matrix[i][j]
max_sum = max(max_sum, curr_sum)

result.append(max_sum)

return result

T = int(input())
test_cases = []
for _ in range(T):
N, M = map(int, input().split())
matrix = []
for _ in range(N):
row = list(map(int, input().split()))
matrix.append(row)
test_cases.append(((N, M), matrix))

# Solve the problem
output = max_xor_sum(T, test_cases)

# Print output
for result in output:
print(result)

``````

Problem Link: GRDXOR Problem - CodeChef