Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
c = [list(map(int, list(input()))) for _ in range(H)]
ans = 0
cc = copy.deepcopy(c)
for z in range(H):
    for m in range(W):
        c = copy.deepcopy(cc)
        c[z][m] = 0
        temp_ans = 0
        repeat_num = -1
        p = 1
        while p:
            p = 0
            for i in range(H):
                num = 1
                for j in range(W):
                    if j != W - 1:
                        if c[i][j] == c[i][j + 1] and c[i][j] != -1:
                            num += 1
                        elif num >= K:
                            temp_ans += 2 ** repeat_num * c[i][j] * num
                            for k in range(j - num + 1, j + 1):
                                c[i][k] = 0
                            num = 1
                            p = 1
                        else:
                            num = 1
                    elif num >= K:
                        temp_ans += 2 ** repeat_num * c[i][j] * num
                        for k in range(j - num + 1, j + 1):
                            c[i][k] = 0
                        num = 1
                        p = 1
            for i in reversed(range(H)):
                for j in reversed(range(W)):
                    if not c[i][j]:
                        if i == 0:
                            c[i][j] = -1
                        else:
                            for k in reversed(range(i)):
                                if c[k][j] != 0:
                                    c[i][j] = c[k][j]
                                    c[k][j] = 0
                                    break
            repeat_num += 1
            if repeat_num == 0:
                p = 1
        ans = max(ans, temp_ans)
print(ans)


Fixed submission:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
c = [list(map(int, list(input()))) for _ in range(H)]
ans = 0
cc = copy.deepcopy(c)
for z in range(H):
    for m in range(W):
        c = copy.deepcopy(cc)
        c[z][m] = 0
        temp_ans = 0
        repeat_num = -1
        p = 1
        while p:
            p = 0
            for i in range(H):
                num = 1
                for j in range(W):
                    if j != W - 1:
                        if c[i][j] == c[i][j + 1] and c[i][j] != -1:
                            num += 1
                        elif num >= K:
                            temp_ans += 2 ** repeat_num * c[i][j] * num
                            for k in range(j - num + 1, j + 1):
                                c[i][k] = 0
                            num = 1
                            p = 1
                        else:
                            num = 1
                    elif num >= K:
                        temp_ans += 2 ** repeat_num * c[i][j] * num
                        for k in range(j - num + 1, j + 1):
                            c[i][k] = 0
                        num = 1
                        p = 1
            for i in reversed(range(H)):
                for j in reversed(range(W)):
                    if not c[i][j]:
                        if i == 0:
                            c[i][j] = -1
                        else:
                            for k in reversed(range(i)):
                                if c[k][j] != 0:
                                    c[i][j] = c[k][j]
                                    c[k][j] = 0
                                    break
            repeat_num += 1
            if repeat_num == 0:
                p = 1
        ans = max(ans, temp_ans)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
H, W, K = map(int, input().split())
board = [[0]*(W+1) for _ in range(H+1)]
for i in range(1,H+1):
    board[i] = list(map(int, list(input())))+[0]
def drop(board_copy):
    change = True
    score = 0
    p = 0
    while change:
        change = False
        for i in range(2,H+1):
            ser = 1
            for j in range(1,W+1):
                if board_copy[i][j]>0 and board_copy[i][j]==board_copy[i][j-1]:
                    ser += 1
                else:
                    if ser>=K:
                        score += 2**p*board_copy[i][j-1]*ser
                        change = True
                        for k in range(i,0,-1):
                            board_copy[k][j-ser:j] = board_copy[k-1][j-ser:j]
                    ser = 1
        p += 1
    return score
ans = 0
for h in range(2,H+1):
    for w in range(W):
        board_copy = deepcopy(board)
        for k in range(h,0,-1):
            board_copy[k][w] = board[k-1][w]
        ans = max(ans,drop(board_copy))
print(ans)

Fixed submission:
h, w, k  = map(int, input().split())

puyo_orig = [list(map(int, list(input()))) for _ in range(h)]

import copy
mx = 0
for i1 in range(h):
    for i2 in range(w):
        puyo = copy.deepcopy(puyo_orig)
        puyo[i1][i2] = -1
        p = 0
        fin = 0
        turn = 0
        while fin==0:
            #[print(*puyo[i]) for i in range(h)]
            fin = 1
            
            for i in range(w):
                d_fin = 0
                while d_fin==0:
                    d_fin = 1
                    for j in range(h-1, 0, -1):
                        if puyo[j][i]==-1 and puyo[j-1][i]>0:
                            puyo[j][i] = puyo[j-1][i]
                            puyo[j-1][i] = -1
                            d_fin = 0
                            
            for i in range(h):
                cnt = 1
                for j in range(1, w):
                    if puyo[i][j-1] == puyo[i][j] and puyo[i][j]>0:
                        cnt += 1
                    else:
                        if cnt>=k:
                            p += cnt * puyo[i][j-1] * 2**turn
                            for k2 in range(cnt):
                                puyo[i][j-k2-1] = -1
                                fin = 0
                        cnt = 1
                    if j==w-1:
                        if cnt>=k:
                            p += cnt * puyo[i][j-1] * 2**turn
                            for k2 in range(cnt):
                                puyo[i][j-k2] = -1
                                fin = 0
            turn += 1
        if p>mx:
            mx=p
            
print(mx)