Buggy python submission (verdict: Time Limit Exceeded):
# input
N, M = map(int, input().split())
Cake = [list(map(int, input().split())) for i in range(N)]

Cake_sum = {i:0 for i in range(N)}

# x_i, y_i, z_iの合計の正負で8通りに場合分け
# x_i >= 0, y_i >= 0, z_i >= 0
ans1 =  0
for i in range(N):
    Cake_sum[i] = Cake[i][0] + Cake[i][1] + Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans1 += Cake_sorted[j][1]

ans2 = 0
# x_i < 0, y_i >= 0, z_i >= 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] + Cake[i][1] + Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans2 += Cake_sorted[j][1]

ans3 = 0
# x_i >= 0, y_i < 0, z_i >= 0
for i in range(N):
    Cake_sum[i] = Cake[i][0] - Cake[i][1] + Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans3 += Cake_sorted[j][1]

ans4 = 0
# x_i >= 0, y_i >= 0, z_i < 0
for i in range(N):
    Cake_sum[i] = Cake[i][0] + Cake[i][1] - Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans4 += Cake_sorted[j][1]

ans5 = 0
# x_i < 0, y_i < 0, z_i >= 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] - Cake[i][1] + Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans5 += Cake_sorted[j][1]

ans6 = 0
# x_i >= 0, y_i < 0, z_i < 0
for i in range(N):
    Cake_sum[i] = Cake[i][0] - Cake[i][1] - Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans6 += Cake_sorted[j][1]

ans7 = 0
# x_i < 0, y_i >= 0, z_i < 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] + Cake[i][1] - Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans7 += Cake_sorted[j][1]

ans8 = 0
# x_i < 0, y_i < 0, z_i < 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] - Cake[i][1] - Cake[i][2]
    Cake_sorted = sorted(Cake_sum.items(), key=lambda x: -x[1])
for j in range(M):
    ans8 += Cake_sorted[j][1]

ans = max(ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8)

print(ans)

Fixed submission:
# input
N, M = map(int, input().split())
Cake = [list(map(int, input().split())) for i in range(N)]

Cake_sum = [0 for i in range(N)]

# sum(x_i), sum(y_i), sum(z_i)の正負で8通りに場合分け
# x_i >= 0, y_i >= 0, z_i >= 0
ans1 =  0
for i in range(N):
    Cake_sum[i] = Cake[i][0] + Cake[i][1] + Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans1 += Cake_sum_sorted[j]

ans2 = 0
# x_i < 0, y_i >= 0, z_i >= 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] + Cake[i][1] + Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans2 += Cake_sum_sorted[j]

ans3 = 0
# x_i >= 0, y_i < 0, z_i >= 0
for i in range(N):
    Cake_sum[i] = Cake[i][0] - Cake[i][1] + Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans3 += Cake_sum_sorted[j]

ans4 = 0
# x_i >= 0, y_i >= 0, z_i < 0
for i in range(N):
    Cake_sum[i] = Cake[i][0] + Cake[i][1] - Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans4 += Cake_sum_sorted[j]

ans5 = 0
# x_i < 0, y_i < 0, z_i >= 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] - Cake[i][1] + Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans5 += Cake_sum_sorted[j]

ans6 = 0
# x_i >= 0, y_i < 0, z_i < 0
for i in range(N):
    Cake_sum[i] = Cake[i][0] - Cake[i][1] - Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans6 += Cake_sum_sorted[j]

ans7 = 0
# x_i < 0, y_i >= 0, z_i < 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] + Cake[i][1] - Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans7 += Cake_sum_sorted[j]

ans8 = 0
# x_i < 0, y_i < 0, z_i < 0
for i in range(N):
    Cake_sum[i] = -Cake[i][0] - Cake[i][1] - Cake[i][2]

Cake_sum_sorted = sorted(Cake_sum, reverse = True) 

for j in range(M):
    ans8 += Cake_sum_sorted[j]

ans = max(ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8)

print(ans)