Buggy python submission (verdict: Time Limit Exceeded):
H, W, K = map(int, input().split())
HW = [list(map(int, list(input()))) for _ in range(H)]

def blocks(hw):
    score = 0
    blank = -1
    flg = True
    delcnt = 0
    while flg:
        flg = False
        for h in range(H):
            cnt = 1
            old = hw[h][:]
            for w in range(1, W):
                if hw[h][w-1]==hw[h][w]:
                    cnt+=1
                    flg = True
                else:
                    if cnt>=K:
                        score += 2**delcnt*cnt*hw[h][w-1]
                        for k in range(1, cnt+1):
                            hw[h][w-k] = blank
                            blank -= 1
                    cnt = 1
            if cnt>=K:
                score += 2**delcnt*cnt*hw[h][W-1]
                for k in range(1, cnt+1):
                    hw[h][W-k] = blank
                    blank -= 1
        for w in range(W):
            cnt = 1
            for h in range(H-1, -1, -1):
                item = hw[h][w]
                hw[h][w] = blank
                blank -= 1
                if item>0:
                    hw[H-cnt][w] = item
                    cnt += 1
        delcnt += 1
    return score
import copy
ans = []
if K>=4:
    print(0)
else:
    for h in range(1, H):
        for w in range(W):
            hw = copy.deepcopy(HW)
            for i in range(H-h, 0, -1):
                hw[i][w] = hw[i-1][w]
            hw[0][w] = 0
            ans.append(blocks(hw))
print(max(ans))

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)
H, W, K = map(int, input().split())
HW = [list(map(int, list(str(input().strip())))) for _ in range(H)]

def blocks(hw):
    score = 0
    ret = 1
    delcnt = 0
    while ret:
        ret = check(delcnt, hw)
        score += ret
        delcnt += 1
    return score

def check(delcnt, hw):
    s = 0
    blank = -1
    for h in range(H):
        cnt = 1
        old = hw[h][:]
        for w in range(1, W):
            if hw[h][w-1]==hw[h][w]:
                cnt+=1
                flg = True
            else:
                if cnt>=K:
                    s += cnt*hw[h][w-1]
                    for k in range(1, cnt+1):
                        hw[h][w-k] = blank
                        blank -= 1
                cnt = 1
        if cnt>=K:
            s += cnt*hw[h][W-1]
            for k in range(1, cnt+1):
                hw[h][W-k] = blank
                blank -= 1
    for w in range(W):
        cnt = 1
        for h in range(H-1, -1, -1):
            item = hw[h][w]
            hw[h][w] = blank
            blank -= 1
            if item>0:
                hw[H-cnt][w] = item
                cnt += 1
    return 2**delcnt*s

import copy
ans = []
if K>=4:
    print(0)
else:
    for h in range(1, H):
        for w in range(W):
            hw = copy.deepcopy(HW)
            for i in range(H-h, 0, -1):
                hw[i][w] = hw[i-1][w]
            hw[0][w] = 0
            ans.append(blocks(hw))
print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):
H, W, K = map(int, input().split())
HW = [list(map(int, list(input()))) for _ in range(H)]

def blocks(hw):
    score = 0
    blank = -1
    flg = True
    delcnt = 0
    while flg:
        flg = False
        for h in range(H):
            cnt = 1
            old = hw[h][:]
            for w in range(1, W):
                if hw[h][w-1]==hw[h][w]:
                    cnt+=1
                    flg = True
                else:
                    if cnt>=K:
                        score += 2**delcnt*cnt*hw[h][w-1]
                        for k in range(1, cnt+1):
                            hw[h][w-k] = blank
                            blank -= 1
                    cnt = 1
            if cnt>=K:
                score += 2**delcnt*cnt*hw[h][W-1]
                for k in range(1, cnt+1):
                    hw[h][W-k] = blank
                    blank -= 1
        fall_flg = True
        while fall_flg:
            fall_flg = False
            for h in range(H-1, 0, -1):
                for w in range(W):
                    if hw[h-1][w]>0 and hw[h][w]<=0:
                        hw[h][w] = hw[h-1][w]
                        hw[h-1][w] = blank
                        blank -= 1
                        fall_flg = True
        delcnt += 1
    return score
import copy
ans = []
for h in range(1, H):
    for w in range(W):
        hw = copy.deepcopy(HW)
        for i in range(H-h, 0, -1):
            hw[i][w] = hw[i-1][w]
        hw[0][w] = 0
        ans.append(blocks(hw))
print(max(ans))

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)
H, W, K = map(int, input().split())
HW = [list(map(int, list(str(input().strip())))) for _ in range(H)]

def blocks(hw):
    score = 0
    ret = 1
    delcnt = 0
    while ret:
        ret = check(delcnt, hw)
        score += ret
        delcnt += 1
    return score

def check(delcnt, hw):
    s = 0
    blank = -1
    for h in range(H):
        cnt = 1
        old = hw[h][:]
        for w in range(1, W):
            if hw[h][w-1]==hw[h][w]:
                cnt+=1
                flg = True
            else:
                if cnt>=K:
                    s += cnt*hw[h][w-1]
                    for k in range(1, cnt+1):
                        hw[h][w-k] = blank
                        blank -= 1
                cnt = 1
        if cnt>=K:
            s += cnt*hw[h][W-1]
            for k in range(1, cnt+1):
                hw[h][W-k] = blank
                blank -= 1
    for w in range(W):
        cnt = 1
        for h in range(H-1, -1, -1):
            item = hw[h][w]
            hw[h][w] = blank
            blank -= 1
            if item>0:
                hw[H-cnt][w] = item
                cnt += 1
    return 2**delcnt*s

import copy
ans = []
if K>=4:
    print(0)
else:
    for h in range(1, H):
        for w in range(W):
            hw = copy.deepcopy(HW)
            for i in range(H-h, 0, -1):
                hw[i][w] = hw[i-1][w]
            hw[0][w] = 0
            ans.append(blocks(hw))
print(max(ans))