Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import groupby
from copy import deepcopy

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def do_drop(H, W, G):
    for j in range(W):
        for i in range(H - 1):
            for k in range(H - 1, i, -1):
                if G[k][j] == 0 and G[k - 1][j] != 0:
                    G[k][j], G[k - 1][j] = G[k - 1][j], G[k][j]


def main():
    H, W, K = map(int, readline().split())
    G_base = [list(map(int, readline().strip())) for _ in range(H)]

    ans = 0
    for i in range(H):
        for j in range(W):
            G = deepcopy(G_base)
            G[i][j] = 0
            do_drop(H, W, G)

            power = 1
            score = 0
            while True:
                finished = True
                total = 0
                for row in G:
                    idx = 0
                    for k, g in groupby(row):
                        l = len(list(g))
                        if k > 0 and l >= K:
                            row[idx : idx + l] = [0] * l
                            total += k * l
                            finished = False
                        idx += l

                if finished:
                    break

                score += power * total
                power *= 2
                do_drop(H, W, G)

            if ans < score:
                ans = score

    print(ans)
    return


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from copy import deepcopy

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def drop(H, G):
    G = [[n for n in col if n != -1] for col in zip(*G)]
    G = [[-1] * (H - len(col)) + col for col in G]
    G = [list(row) for row in zip(*G)]
    return G


def main():
    H, W, K = map(int, readline().split())
    G_init = [list(map(int, list(readline().strip()))) for _ in range(H)]

    ans = 0
    for i in range(H):
        for j in range(W):
            G = deepcopy(G_init)
            G[i][j] = -1
            G = drop(H, G)

            current_ans = 0
            step = 0
            while True:
                score = 0
                for row in G:
                    seq = [[row[0], 1]]
                    for n in row[1:]:
                        if n == seq[-1][0]:
                            seq[-1][1] += 1
                        else:
                            seq.append([n, 1])
                    idx = 0
                    for n, c in seq:
                        if n != -1 and c >= K:
                            score += n * c
                            row[idx : idx + c] = [-1] * c
                        idx += c

                if score == 0:
                    break

                current_ans += score * pow(2, step)
                step += 1
                G = drop(H, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from copy import deepcopy

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def drop(H, G):
    G = [[n for n in col if n != -1] for col in zip(*G)]
    G = [[-1] * (H - len(col)) + col for col in G]
    G = [list(row) for row in zip(*G)]
    return G


def main():
    H, W, K = map(int, readline().split())
    G_init = [list(map(int, list(readline().strip()))) for _ in range(H)]

    ans = 0
    for i in range(H):
        for j in range(W):
            G = deepcopy(G_init)
            G[i][j] = -1
            G = drop(H, G)

            current_ans = 0
            step = 0
            while True:
                score = 0
                for row in G:
                    seq = [[row[0], 1]]
                    for n in row[1:]:
                        if n == seq[-1][0]:
                            seq[-1][1] += 1
                        else:
                            seq.append([n, 1])
                    idx = 0
                    for n, c in seq:
                        if n != -1 and c >= K:
                            score += n * c
                            row[idx : idx + c] = [-1] * c
                        idx += c

                if score == 0:
                    break

                current_ans += score * pow(2, step)
                step += 1
                G = drop(H, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from copy import deepcopy

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def drop(H, G):
    G = [[n for n in col if n != -1] for col in zip(*G)]
    G = [[-1] * (H - len(col)) + col for col in G]
    G = [list(row) for row in zip(*G)]
    return G


def main():
    H, W, K = map(int, readline().split())
    G_init = [list(map(int, list(readline().strip()))) for _ in range(H)]

    ans = 0
    for i in range(H):
        for j in range(W):
            G = deepcopy(G_init)
            G[i][j] = -1
            G = drop(H, G)

            current_ans = 0
            step = 0
            while True:
                score = 0
                for row in G:
                    seq = [[row[0], 1]]
                    for n in row[1:]:
                        if n == seq[-1][0]:
                            seq[-1][1] += 1
                        else:
                            seq.append([n, 1])
                    idx = 0
                    for n, c in seq:
                        if n != -1 and c >= K:
                            score += n * c
                            row[idx : idx + c] = [-1] * c
                        idx += c

                if score == 0:
                    break

                current_ans += score * pow(2, step)
                step += 1
                G = drop(H, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()