Problem:
  Max Score: $300$ Points   Problem Statement We are playing a puzzle. An upright board with $H$ rows by $W$ columns of cells, is used in this puzzle. A stone in $i$-th row and $j$-th column engraved with a digit, one of 1 through 9, is placed in each of the cells. You can erase 1 cell in the board.  The game process is following:   When $K$ or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.  When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.  After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.  Score is $\sum_i 2^i \times \left(\text{Sum of numbers in the stones which disappeared in the $i$-th chain (0-indexed)}\right)$.  Please answer the points if he erased the optimal place.        Input The input is given from standard input in the following format.

Solution (Python):
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()

Solution (Python):
import copy
h, w, k = map(int, input().split())
a = [[] for j in range(h)]
for i in range(h):
    a[i] = [int(i) for i in input()]
ans = 0
for p in range(h):
    for q in range(w):
        cand = 0
        b = copy.deepcopy(a)
        b[p][q] = 0
        disap = True
        fall = True
        disapnum = 0
        while(disap):
            disap=False
            cur = 0
            for i in range(h):
                suc = 1
                for j in range(w-1):
                    if b[i][j] == b[i][j+1]:
                        suc += 1
                    else:
                        if suc >= k:
                            if b[i][j]:
                                disap=True
                                fall=True
                                cur += b[i][j]*suc
                            for m in range(j+1-suc, j+1):
                                b[i][m] = 0
                        suc = 1
                if suc >= k:
                    if b[i][w-1]:
                        disap=True
                        fall=True
                        cur += b[i][w-1]*suc
                    for m in range(w-suc, w):
                        b[i][m] = 0
            if disap:
                cand += cur*(2**disapnum)
                disapnum += 1
            while(fall):
                fall=False
                for i in range(1, h):
                    for j in range(w):
                        if b[i][j] == 0 and b[i-1][j]:
                            b[i][j], b[i-1][j] = b[i-1][j], b[i][j]
                            fall = True
                            disap = True
        ans = max(cand, ans)
print(ans)

Solution (Python):
import sys
H, W, K = map(int, input().split())
HW = [list(map(int, list(str(input())))) 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))

Solution (Python):
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))

Solution (Python):
import copy
H, W, K = map(int, input().split())
C = [[0] * W for i in range(H)]
for i in range(H):
  s = str(input())
  for j in range(W):
    C[i][j] = int(s[j])
  
def delete(mass, s, t):
  
  mass[s][t] = 0
  
  score = 0
  p = -1
  time = -1
  while p < 1:
    this = 0
    for i in range(H):
      now = 1
      for j in range(1, W):
        if (mass[i][j] == mass[i][j - 1]) and (mass[i][j] != 0):
          now += 1
        else:
          if now >= K:
            this += 1
            score += (2 ** time) * now * mass[i][j - 1]
            for k in range(j - now, j):
              mass[i][k] = 0
          now = 1
       
      if now >= K:
        this += 1
        score += (2 ** time) * now * mass[i][j - 1]
        for k in range(j - now + 1, j + 1):
          mass[i][k] = 0
    
    if this == 0:
      p += 1
    
    for i in range(1, H + 1):
      for j in range(W):
        if (mass[H - i][j] == 0) and (i != H):
          k = 1
          while (mass[H - i - k][j] == 0) and (k + i < H):
            k += 1
          mass[H - i][j] = mass[H - i - k][j]
          mass[H - i - k][j] = 0
    time += 1
  return score

ans = 0
for i in range(H):
  for j in range(W):
    D = copy.deepcopy(C)
    ans = max(ans, delete(D, i, j))
  
print(ans)

Solution (Python):
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, W, G):
    for i in range(1, H):
        for j in range(W):
            if G[i][j] == 0 and G[i - 1][j] != 0:
                k = i
                for k in range(i, 0, -1):
                    if G[k][j] != 0:
                        break
                    G[k][j], G[k - 1][j] = G[k - 1][j], 0


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

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

            current_ans = 0
            scale = 1
            while True:
                score = 0
                for row in G:
                    cnt = 1
                    for k in range(1, W):
                        if row[k] == row[k - 1]:
                            cnt += 1
                        else:
                            if cnt >= K and row[k - 1] != 0:
                                score += row[k - 1] * cnt
                                row[k - cnt : k] = [0] * cnt
                            cnt = 1
                    if cnt >= K and row[W - 1] != 0:
                        score += row[W - 1] * cnt
                        row[W - cnt : W] = [0] * cnt

                if score == 0:
                    break

                current_ans += score * scale
                scale *= 2
                drop(H, W, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
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, W, G):
    for i in range(1, H):
        for j in range(W):
            if G[i][j] == 0 and G[i - 1][j] != 0:
                k = i
                for k in range(i, 0, -1):
                    if G[k][j] != 0:
                        break
                    G[k][j], G[k - 1][j] = G[k - 1][j], 0


def main():
    H, W, K = map(int, readline().split())
    G_init = [list(map(int, 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] = 0
            drop(H, W, G)

            current_ans = 0
            scale = 1
            while True:
                score = 0
                for row in G:
                    cnt = 1
                    for k in range(1, W):
                        if row[k] == row[k - 1]:
                            cnt += 1
                        else:
                            if cnt >= K and row[k - 1] != 0:
                                score += row[k - 1] * cnt
                                row[k - cnt : k] = [0] * cnt
                            cnt = 1
                    if cnt >= K and row[W - 1] != 0:
                        score += row[W - 1] * cnt
                        row[W - cnt : W] = [0] * cnt

                if score == 0:
                    break

                current_ans += score * scale
                scale *= 2
                drop(H, W, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
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, W, G):
    for i in range(1, H):
        for j in range(W):
            if G[i][j] == -1 and G[i - 1][j] != -1:
                k = i
                for k in range(i, 0, -1):
                    if G[k][j] != -1:
                        break
                    G[k][j], G[k - 1][j] = G[k - 1][j], -1


def main():
    H, W, K = map(int, readline().split())
    G_init = [list(map(int, 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
            drop(H, W, G)

            current_ans = 0
            step = 0
            while True:
                score = 0
                for row in G:
                    cnt = 1
                    for k in range(1, W):
                        if row[k] == row[k - 1]:
                            cnt += 1
                        else:
                            if cnt >= K and row[k - 1] != -1:
                                score += row[k - 1] * cnt
                                row[k - cnt : k] = [-1] * cnt
                            cnt = 1
                    if cnt >= K and row[W - 1] != -1:
                        score += row[W - 1] * cnt
                        row[W - cnt : W] = [-1] * cnt

                if score == 0:
                    break

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

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
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, W, G):
    for i in range(1, H):
        for j in range(W):
            if G[i][j] == -1 and G[i - 1][j] != -1:
                k = i
                for k in range(i, 0, -1):
                    if G[k][j] != -1:
                        break
                    G[k][j], G[k - 1][j] = G[k - 1][j], -1


def main():
    H, W, K = map(int, readline().split())
    G_init = [list(map(int, 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
            drop(H, W, 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
                drop(H, W, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
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, W, G):
    for i in range(H - 1, 0, -1):
        for j in range(W):
            if G[i][j] == -1 and G[i - 1][j] != -1:
                k = i
                while k < H and G[k][j] == -1:
                    G[k][j], G[k - 1][j] = G[k - 1][j], -1
                    k += 1


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
            drop(H, W, 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
                drop(H, W, G)

            if current_ans > ans:
                ans = current_ans

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
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()

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    import copy
    h,w,K = map(int, input().split())
    c = [[] for _ in range(w)]
    for i in range(h):
        a = input()
        for j in range(w):
            c[j].append(a[j])
    
    ans = 0
    for i in range(h):
        for j in range(w):
            C = copy.deepcopy(c)
            C[j].pop(i)
            ren = 0
            point = 0
            while True:
                cnt = 0
                for k in range(30,0,-1):
                    
                    start = 0
                    while start <= w-K:
                        if (len(C[start]) < k):
                            start += 1
                            continue
                        num = C[start][-k]
                        end = start
                        while True:
                            
                            if (end<w-1 and len(C[end+1])>=k and C[end+1][-k]==num):
                                end += 1
                            else:
                                break
                        if (end+1-start >= K):
                            point += (end+1-start)*int(C[start][-k])*(2**ren)
                            for p in range(start,end+1):
                                
                                C[p].pop(-k)
                                cnt += 1
                        start = end + 1
                if (cnt == 0):
                    break
                ren += 1
            if (point > ans):
                ans = point
    print(ans)
    
if __name__=="__main__":
    main()

Solution (Python):
from copy import deepcopy


def puzzle(x,y,A):
    ans,point=0,0
    A[x][y]=0
    B=[[-1 for _ in range(w)] for _ in range(h)]
    for i in range(w):
        cnt=h-1
        for j in range(h)[::-1]:
            if A[j][i]!=0:
                B[j][i]=cnt
                cnt -=1
    C=[[0 for _ in range(w)] for _ in range(h)]
    for i in range(w):
        for j in range(h):
            if B[j][i]!=-1:
                C[B[j][i]][i]=A[j][i]
    A=C

    while 1:
        score=ans
        for i in range(h):
            cnt,num=0,A[i][0]
            for j in range(w):
                if A[i][j]==num:cnt +=1
                else:
                    if cnt>=k:
                        ans +=num*cnt*2**point
                        for l in range(cnt):
                            A[i][j-l-1]=0
                    num,cnt=A[i][j],1
                if j==w-1 and cnt>=k:
                    ans +=num*cnt*2**point
                    for l in range(cnt):
                        A[i][j-l]=0

        B=[[-1 for _ in range(w)] for _ in range(h)]
        for i in range(w):
            cnt=h-1
            for j in range(h)[::-1]:
                if A[j][i]!=0:
                    B[j][i]=cnt
                    cnt -=1
        C=[[0 for _ in range(w)] for _ in range(h)]
        for i in range(w):
            for j in range(h):
                if B[j][i]!=-1:
                    C[B[j][i]][i]=A[j][i]
        A=C
        if score==ans:break
        point +=1
    return ans


h,w,k=map(int,input().split())
D=[list(map(int,list(input()))) for _ in range(h)]

ans=0
for i in range(h):
    for j in range(w):
        A=deepcopy(D)
        ans=max(ans,puzzle(i,j,A))
print(ans)

Solution (Python):
import sys, itertools, copy
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    H, W, K = LI()
    c = [[int(i) for i in S()] for _ in range(H)]

    ans = 0
    for i, j in itertools.product(range(H), range(W)):
        c_tmp = copy.deepcopy(c)
        c_tmp[i][j] = 0
        score = 0
        chain_cnt = 0
        while True:
            
            
            drop = [[0]*W for _ in range(H)]
            for k in range(W):
                for l in range(H-1):
                    drop[H-l-2][k] = drop[H-l-1][k] + (1 if c_tmp[H-l-1][k]==0 else 0)
            
            
            
            
            
            for k in range(W):
                for l in range(H):
                    if drop[H-l-1][k]!=0:
                        c_tmp[H-l-1+drop[H-l-1][k]][k] = c_tmp[H-l-1][k]
                        c_tmp[H-l-1][k] = 0
            
            
            

            
            num_sum = 0
            for k in range(H):
                prev = 0
                cnt = 1
                for l in range(W):
                    if c_tmp[k][l]==prev:
                        cnt += 1
                    else:
                        if cnt>=K:
                            num_sum += prev*cnt
                            for m in range(cnt):
                                c_tmp[k][l-cnt+m] = 0
                        prev = c_tmp[k][l]
                        cnt = 1
                if cnt>=K:
                    num_sum += prev*cnt
                    for m in range(cnt):
                        c_tmp[k][l+1-cnt+m] = 0
            
            
            
            if num_sum==0:
                break
            score += (2**chain_cnt) * num_sum
            chain_cnt += 1
        ans = max(score, ans)

    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()

import copy
def resolve():
    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

    H, W, K = map(int, input().split())
    mp = []
    mp.append([0] * (W + 1))
    mp += [list(map(int, input())) + [0] for _ in range(H)]

    ans = 0
    for i in range(1, H + 1):
        for j in range(W):
            mp_copy = copy.deepcopy(mp)
            for k in range(i, 0, -1):
                mp_copy[k][j] = mp_copy[k - 1][j]
            score = drop(mp_copy)
            ans = max(ans, score)
    print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):
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(1,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)

Solution (Python):
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(1,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)

Solution (Python):
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(W):
                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]*ser
                        change = True
                        for k in range(i,0,-1):
                            board_copy[k][j-ser+1:j+1] = board_copy[k-1][j-ser+1:j+1]
                    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)

Solution (Python):
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)

Solution (Python):
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(1,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)

Solution (Python):
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(W):
                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]*ser
                        change = True
                        for k in range(i,0,-1):
                            board_copy[k][j-ser+1:j+1] = board_copy[k-1][j-ser+1:j+1]
                    ser = 1
        p += 1
    return score
ans = 0
for h in range(1,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)