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):
def drop(lst,h,w):
    for i in range(w-1,-1,-1):
        work=[lst[j][i] for j in range(h)]
        work=[x for x in work if x]
        for j in range(h-1,-1,-1):
            if work: lst[j][i]=work.pop()
            else : lst[j][i]=None

def chain(lst,h,w,k):
    sm=0
    for j in range(h):
        train=1
        for i in range(w-1):
            if not lst[j][i] :
                train=1
                continue
            if lst[j][i]==lst[j][i+1]:
                train+=1
            else :
                if train>=k:
                    sm+=lst[j][i]*train
                    lst[j][i-train+1:i+1]=[None]*train
                train=1
        if train>=k:
            sm+=lst[j][-1]*train
            lst[j][-train:]=[None]*train
    return sm


def procedure(lst,h,w,k,x):
    lst[x[0]][x[1]]=None
    drop(lst,h,w)
    sm=0
    i=0
    point=chain(lst,h,w,k)
    while point:
        sm+=(2**i)*point
        drop(lst,h,w)
        point=chain(lst,h,w,k)
        i+=1
        
    return sm


def main():
    h,w,k=map(int,input().split())
    lst=[[int(x) for x in input()] for _ in range(h)]
    mx=0

    for i in range(1,h):
        for j in range(w):
            work=[[lst[i][j] for j in range(w)] for i in range(h)]
            sm=procedure(work,h,w,k,(i,j))
            if mx<sm : mx=sm
    print(mx)


main()

Solution (Python):
def cont(a, K):
    contL = []
    j = 1
    while j < W: 
        cnt = 1
        while a[j] == a[j-1] and a[j] != None:
            cnt += 1
            j += 1
        if cnt >= K:
            contL.append((j-cnt, j, a[j-1]))
        j += 1
    return contL

def fall(d):
    for j in range(W):
        for k in range(H):
            if d[k][j] == None:
                for i in range(k, 0, -1):
                    d[i][j] = d[i-1][j]
                d[0][j] = None
    return d


import copy 

H, W, K = map(int, input().split())
c = [list(map(int, list(input().strip()))) + [0] for _ in range(H)] 

candL = [(i, j) for i in range(H) for j in range(W)]

ans = 0
for i_push, j_push in candL:   
    d = copy.deepcopy(c)    
    d[i_push][j_push] = None
    d = fall(d)             
    n = -1
    ans0 = 0
    while True:
        flag = False
        n += 1
        for i in range(i_push, -1, -1):
            cL = cont(d[i], K)       
            for j0, j1, val in cL:
                d[i][j0:j1] = [None]*(j1-j0)                
                ans0 += val *(j1-j0) * 2**(n)  
                flag = True
        if not flag: break
        
        d = fall(d)          
    ans = max(ans, ans0)    
print(ans)

Solution (Python):
def dropcell(h, w, rm):
  for i in range(w):
    y=0
    for j in range(h):
      if rm[h-j-1][i]!=0:
        rm[h-y-1][i]=rm[h-j-1][i]
        y+=1
    for j in range(h-y):
      rm[h-y-j-1][i]=0

def removecell(h, w, k, rm, a):
  p=0
  for y in range(h):
    c,t=1,0
    for i in range(w+1):
      x=rm[y][i]
      if x>0 and x==t:
        c+=1
      else:
        if c>=k:
          for j in range(c):
            rm[y][i-1-j]=0
          p+=(t*c)
        t=x
        c=1
  return p*(2**a)

def game(h,w,k,M,y,x):
  f,score,a=1,0,0
  rm=[[j for j in i]for i in M[:y+1]]
  rm[y][x]=0
  while f:
    dropcell(len(rm),w,rm)
    f=removecell(len(rm),w,k,rm,a)
    score+=f
    a+=1
  return score

H,W,K=map(int,input().split())
M=[list(map(int,list(input())))+[0] for i in range(H)]
ans=0
for j in range(W):
  t=0
  for i in range(1, H):
    if M[i][j]!=t:
      ans=max(ans,game(H,W,K,M,i,j))
      t=M[i][j]
print(ans)

Solution (Python):

import copy

H, W, K = map(int, input().split())
if K > W:
    exit(print(0))


c = [[] for _ in range(W)]
for i in range(H):
    S = input()
    for j in range(W):
        c[j].append(int(S[j]))

for j in range(W):
    c[j] = c[j][::-1]


def cal_score(c,score,loop):
    
    nextc = []
    for j in range(W):
        num0 = c[j].count(0)
        tmp = [*filter(lambda x: x>0,c[j])]
        nextc.append(tmp+[0]*num0)

    
    res = 0
    for i in range(H):
        j = 0
        while j < W:
            seq = 1
            while j<W-1 and nextc[j][i] == nextc[j+1][i] > 0:
                seq += 1
                j += 1
            if seq >= K:
                res += seq * nextc[j][i]
                for k in range(j-seq+1,j+1):
                    nextc[k][i] = 0
            j += 1
    if res == 0:
        return score
    else:
        return cal_score(nextc,score+res*pow(2,loop),loop+1)
    

ans = 0
for p in range(H):
    for q in range(W):
        cc = copy.deepcopy(c)
        cc[q][p] = 0
        z = cal_score(cc,0,0)
        ans = max(ans, z)
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    from copy import deepcopy
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7

    h,w,k=MI()
    lis=[list(map(int,list(SI()))) for i in range(h)]
    
    
    if k==1:
        ans=0
        for i in range(h):
            ans+=sum(lis[i])
        print(ans)
        sys.exit()
    def process(board):
        res=0
        for i in range(w):
            plus=[]
            for j in range(h):
                m=h-1-j
                if board[m][i]>0:
                    plus.append(board[m][i])
            for j in range(h):
                if j<len(plus):
                    board[h-1-j][i]=plus[j]
                else:
                    board[h-1-j][i]=0
                '''m=h-1-j
                pos=0
                print(board[m][i])
                z=board[m][i]
                if z>0:
                    board[h-1-pos][i]=z
                    if pos!=j:
                        board[m][i]=0
                    pos+=1
                print(board)'''
                    
        
        
        for i in range(h):
            seq=1
            for j in range(w-1):
                if board[i][j]==board[i][j+1]:
                    seq+=1
                else:
                    if seq>=k:
                        res+=seq*board[i][j]
                        for l in range(j+1-seq,j+1):
                            board[i][l]=0
                    seq=1
            if seq>=k:
                res+=seq*board[i][j]
                for l in range(w-seq,w):
                    board[i][l]=0
        
        return res
    process(lis)
    ans=0
    for i in range(w):
        for j in range(h):
            boardx=deepcopy(lis)
            boardx[j][i]=0
            flag=True
            z=0
            res=0
            while flag==True:
                x=process(boardx)
                if x==0:
                    flag=False
                else:
                    res+=x*(2**z)
                    
                    z+=1
            
            
            ans=max(ans,res)
    print(ans)


    
        
    
    
if __name__=="__main__":
    main()

Solution (Python):
from copy import deepcopy
H, W, K = map(int, input().split())

def clear(row, start, end, mult):
    if end - start + 1 < K:
        return 0
    gain = 0
    for i in range(start, end + 1):
        gain += mult * row[i]
        row[i] = 0
    return gain

def drop(board):
    for x in range(W):
        wp = 0
        for y in range(H):
            if board[y][x] > 0:
                board[wp][x] = board[y][x]
                if wp != y:
                    board[y][x] = 0
                wp += 1

def proceed(board, mult):
    gain = 0
    for row in board:
        start = -1
        num = -1
        for i in range(W):
            if row[i] == num:
                continue
            else:
                gain += clear(row, start, i-1, mult)
                num = row[i]
                start = i
        if num > 0:
            gain += clear(row, start, W-1, mult)
    drop(board)
    return gain

board_ = []
for _ in range(H):
    board_.append(list(map(int, input())))
board_.reverse()

ans = 0
for y in range(H):
    for x in range(W):
        board = deepcopy(board_)
        board[y][x] = 0
        drop(board)
        mult = 1
        cand = 0
        while True:
            gain = proceed(board, mult)
            if gain == 0:
                break
            cand += gain
            mult *= 2
        ans = max(ans, cand)
print(ans)

Solution (Python):
from copy import deepcopy


def calc(a, t):
    global ansd, tmp
    flag = deepcopy(a)
    f = False
    for y, b in enumerate(a):
        cnt = 0
        now = tmp - 1
        for x, c in enumerate(b):
            flag[y][x] = False
            if c == now:
                cnt += 1
            else:
                if cnt >= K:
                    ansd += now * cnt * (2 ** t)
                    for z in range(x - cnt, x):
                        flag[y][z] = True
                        f = True
                now = c
                cnt = 1
        if cnt >= K:
            ansd += now * cnt * (2**t)
            for z in range(x - cnt + 1, x + 1):
                flag[y][z] = True
                f = True
    if f:
        a = collapse(a, flag)
        calc(a, t + 1)
    else:
        return


def collapse(m, f):
    global tmp
    ans = [[0] * W for _ in range(H)]
    for x in range(W):
        cnt = H - 1
        for y in range(H)[::-1]:
            if f[y][x]:
                continue
            if m[y][x] < 0:
                continue
            ans[cnt][x] = m[y][x]
            cnt -= 1
            tmp -= 1
    for y in range(H):
        for x in range(W):
            if ans[y][x] == 0:
                ans[y][x] = tmp
                tmp -= 1
    m = ans
    return m


H, W, K = map(int, input().split())
C = [[0] * W for _ in range(H)]
for h in range(H):
    s = input()
    for w, s_ in enumerate(s):
        C[h][w] = int(s_)
ans = 0
for i in range(1, H):
    for j in range(W):
        D = deepcopy(C)
        tmp = -1
        for k in range(1, i+1)[::-1]:
            D[k][j] = D[k-1][j]
        D[0][j] = tmp
        tmp -= 1
        ansd = 0
        calc(D, 0)
        ans = max(ans, ansd)
print(ans)

Solution (Python):
from copy import deepcopy
H, W, K = [int(x) for x in input().split()]
c = [[int(x) for x in list(input())] for _ in range(H)]
c.reverse()

max_score = 0
for y in range(H):
    for x in range(W):
        board = deepcopy(c)
        board[y][x] = None  
        score = 0
        erased = True
        coeff = 1
        while erased:
            erased = False
            
            for i in range(1, H):
                for j in range(W):
                    k = i
                    while k > 0 and board[k - 1][j] == None:
                        k -= 1
                    board[k][j], board[i][j] = board[i][j], board[k][j]

            
            for i in range(H):
                cnt = 1
                num = board[i][0]
                for j in range(1, W):
                    if board[i][j] != None and board[i][j] == board[i][j - 1]:
                        num = board[i][j]
                        cnt += 1
                    else:
                        if cnt >= K:
                            erased = True
                            score += coeff * cnt * num
                            board[i][j - cnt : j] = [None] * cnt
                        cnt = 1
                if cnt >= K:
                    erased = True
                    score += coeff * cnt * num
                    board[i][j - cnt + 1 : j + 1] = [None] * cnt

            coeff *= 2

        max_score = max(max_score, score)

print(max_score)

Solution (Python):
from copy import deepcopy
from typing import List
import sys
input = sys.stdin.readline


def delete_cell_line(line: List[int], k: int) -> int:
    prev = -1
    block = 1
    point = 0
    for idx, num in enumerate(line+[0]):
        if num == prev:
            block += 1

        else:
            if block >= k:
                point += prev * block
                for i in range(idx-block, idx):
                    line[i] = 0
            block = 1
            prev = num
    return point


def drop_down(board: List[List[int]]):
    w = len(board[0])
    h = len(board)

    for x in range(w):
        vertical_line = []
        for y in range(h):
            cell = board[y][x]
            if cell:
                vertical_line.append(cell)

        vertical_line = [0]*(h-len(vertical_line)) + vertical_line
        for y, new_cell in enumerate(vertical_line):
            board[y][x] = new_cell


def calc_score(y: int, x: int, k: int, board: List[List[int]]) -> int:
    board[y][x] = 0
    drop_down(board)
    chain = 0
    score = 0
    while True:
        point = 0
        for line in board:
            point += delete_cell_line(line, k)
        if not point:
            break

        score += point * 2**chain
        chain += 1
        drop_down(board)
    return score


def main():
    h, w, k = map(int, input().split())
    board = []
    for _ in range(h):
        pre_line = input().rstrip("\n")
        line = list(map(int, list(pre_line)))
        board.append(line)

    ans = 0
    for y in range(h):
        for x in range(w):
            new_board = deepcopy(board)
            score = calc_score(y, x, k, new_board)
            if ans < score:
                ans = score

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from copy import deepcopy

H, W, K = map(int, input().split())
puzzle = [[-1] * H for _ in range(W)]

for i in range(H - 1, -1, -1):
    for j, stone in enumerate(list(map(int, input()))):
        puzzle[j][i] = stone


def solve(h, w):
    pz = deepcopy(puzzle)
    pz[w].pop(h)
    pz[w].append(0)
    ans = 0
    cnt = 0
    tmp_score = -1
    while tmp_score != 0:
        tmp_score = 0
        tmp_sum = 0

        
        for i in range(H - 1, -1, -1):
            ch_cnt = 0
            for j in range(W - 1):
                if pz[j][i] == pz[j + 1][i]:
                    ch_cnt += 1
                else:
                    if ch_cnt + 1 >= K:
                        
                        tmp_sum += pz[j][i] * (ch_cnt + 1)
                        
                        for p in range(j, j - (ch_cnt + 1), -1):
                            pz[p].pop(i)
                            pz[p].append(0)
                    ch_cnt = 0
            else:
                if ch_cnt + 1 >= K:
                    
                    tmp_sum += pz[j][i] * (ch_cnt + 1)
                    
                    for p in range(j + 1, j - ch_cnt, -1):
                        pz[p].pop(i)
                        pz[p].append(0)
        tmp_score += (2 ** cnt) * tmp_sum
        cnt += 1
        ans += tmp_score
    return ans


f_ans = 0
for i in range(H):
    for j in range(W):
        f_ans = max(f_ans, solve(i, j))


print(f_ans)

Solution (Python):
import sys
input = sys.stdin.readline
H, W, K = map(int, input().split())
B = [list(map(int, input()[:-1])) for _ in [0]*H]


def calc(S):
    gain = 0
    for line in S:
        gain += clear(line)
    remove(S)
    return gain


def clear(line):
    gain = 0
    cnt = 0
    pre = line[0]
    for i, c in enumerate(line):
        if pre == c:
            cnt += 1
        else:
            if cnt >= K:
                gain += line[i-1]*cnt
                for k in range(i-cnt, i):
                    line[k] = 0
            cnt = 1
        pre = c

    if cnt >= K:
        gain += line[-1]*cnt
        for k in range(W-cnt, W):
            line[k] = 0
    return gain


def remove(S):
    for w in range(W):
        i = H-1
        for h in range(H-1, -1, -1):
            if S[h][w]:
                S[i][w] = S[h][w]
                i -= 1
        for i in range(i+1):
            S[i][w] = 0


def remove_sq(S, i, j):
    for h in range(i, 0, -1):
        S[h][j] = S[h-1][j]
    S[0][j] = 0


ans = 0
for i in range(H):
    for j in range(W):
        S = [b.copy() for b in B]
        remove_sq(S, i, j)
        score = 0
        p = 1
        while True:
            gain = calc(S)
            if gain == 0:
                break
            score += p * gain
            p *= 2
        if ans < score:
            ans = score
print(ans)

Solution (Python):
from copy import deepcopy

h,w,k=map(int,input().split())
b=[list(i[::-1]) for i in zip(*[input() for _ in range(h)])]
b=[[int(s) for s in strs] for strs in b]
fans=0
for x in range(w):
  for y in range(h):
    a=deepcopy(b)
    a[x].pop(y)
    a[x].append(0)
    q=[]
    ans=0
    d=-1
    m=0
    while True:
      if d==ans:break
      d=ans
      for i in range(h):
        cnt=0
        for j in range(w-1):
          if a[j][i]==a[j+1][i]:cnt+=1
          else:
            if cnt>=k-1:
              q.append((j-cnt,i,cnt+1))
            cnt=0
        if cnt>=k-1:
          q.append((w-cnt-1,i,cnt+1))      
      while q:
        j,i,cnt=q.pop()
        for c in range(cnt):
          ans+=2**m*a[j+c].pop(i)
          a[j+c].append(0)
      m+=1
    fans=max(fans,ans) 
print(fans)

Solution (Python):
from itertools import groupby
def calc():
  s = 0
  for i in range(h):
    gr = groupby(g[i])
    j = 0
    for key,it in gr:
      l = len(list(it))
      if key!=-1:
        if l >= K:
          g[i][j:j+l] = [-1]*l
          
          
          s += key*l
      j += l
 
  return s

def flatten():
  for j in range(w):
    lst = []
    for i in range(h-1, -1, -1):
      if g[i][j] > 0:
        lst.append(g[i][j])

    lst += [-1] * (h - len(lst))

    for i in range(h-1, -1, -1):
      g[i][j] = lst[h-i-1]

h,w,K = map(int,input().split())
mat = [ [int(e) for e in list(input())] for _ in range(h) ]

res = 0
for i in range(h):
  for j in range(w):
    score = 0
    g = [ [ mat[n][m] for m in range(w) ] for n in range(h) ]
    g[i][j] = -1
    time = 0
    flatten()
    while True:
      s = calc()
      if s==0: break
      score += (2**time)*s
      flatten()
      time += 1
    res = max(res, score)

print(res)

Solution (Python):
from itertools import groupby
def calc():
  s = 0
  for i in range(h):
    gr = groupby(g[i])
    j = 0
    for key,it in gr:
      l = len(list(it))
      if key!=-1:
        if l >= K:
          
          for k in range(l):
            g[i][j+k] = -1
          s += key*l
      j += l
 
  return s

def flatten():
  for j in range(w):
    lst = []
    for i in range(h-1, -1, -1):
      if g[i][j] > 0:
        lst.append(g[i][j])

    lst += [-1] * (h - len(lst))

    for i in range(h-1, -1, -1):
      g[i][j] = lst[h-i-1]

h,w,K = map(int,input().split())
mat = [ [int(e) for e in list(input())] for _ in range(h) ]

res = 0
for i in range(h):
  for j in range(w):
    score = 0
    g = [ [ mat[n][m] for m in range(w) ] for n in range(h) ]
    g[i][j] = -1
    time = 0
    flatten()
    while True:
      s = calc()
      if s==0: break
      score += (2**time)*s
      flatten()
      time += 1
    res = max(res, score)

print(res)

Solution (Python):
from itertools import groupby

def calc(time):
  t = 0
  for i in range(h):
    gr = groupby(g[i])
    lst = []
    p = 0
    for key,it in gr:
      j = len(list(it))
      lst.append([p,key,j])
      p += j
    for j in range(len(lst)):
      if lst[j][1] == -1:
        continue
      if lst[j][2] >= K:
        t += (2**time)*lst[j][1]*lst[j][2]
        for k in range(lst[j][0],lst[j][0]+lst[j][2]):
          g[i][k] = 0

  return t

def flatten():
  for j in range(w):
    lst = []
    for i in range(h-1, -1, -1):
      if g[i][j] > 0:
        lst.append(g[i][j])

    lst += [-1] * (h - len(lst))

    for i in range(h-1, -1, -1):
      g[i][j] = lst[h-i-1]

h,w,K = map(int,input().split())
mat = [ [int(e) for e in list(input())] for _ in range(h) ]

res = 0
for i in range(h):
  for j in range(w):
    score = 0
    g = [ [ mat[n][m] for m in range(w) ] for n in range(h) ]
    g[i][j] = -1
    time = 0
    flatten()
    while True:
      s = calc(time)
      if s==0: break
      score += s
      flatten()
      time += 1
    res = max(res, score)

print(res)

Solution (Python):
H,W,K = list(map(int,input().split()))
C = [list(input()) for _ in range(H)]

def F1(c):
    for i in range(W):
        for j in range(H-1,-1,-1):
            if c[j][i] != 'X': continue
            for k in range(j-1,-1,-1):
                if c[k][i] != 'X':
                    c[j][i], c[k][i] = c[k][i], c[j][i]
                    break
    return c

def F2(c):
    delete_sum = 0
    for i in range(H):
        for j in range(W):
            if c[i][j] == 'X': continue
            tmp = c[i][j]
            count = 1
            for k in range(j+1,W):
                if c[i][k] == tmp:
                    count += 1
                else:
                    break
            if count >= K:
                for k in range(j,W):
                    if c[i][k] == tmp:
                        delete_sum += int(c[i][k])
                        c[i][k] = 'X'
                    else:
                        break
    return c, delete_sum

MAX = 0
for i in range(H):
    for j in range(W):
        tmpC = []
        for k in range(H):
            tmpC.append(C[k][:])
        tmpC[i][j] = 'X'
        tmpC = F1(tmpC)
        point = 0
        for k in range(1000):
            tmpC, delete_sum = F2(tmpC)
            if delete_sum == 0:
                break
            tmpC = F1(tmpC)
            point += 2**k*delete_sum
        MAX = max(MAX,point)
print(MAX)

Solution (Python):
H,W,K = list(map(int,input().split()))
C = [list(input()) for _ in range(H)]

def F1(c):
    for i in range(W):
        for j in range(H-1,-1,-1):
            if c[j][i] != 'X': continue
            for k in range(j-1,-1,-1):
                if c[k][i] != 'X':
                    c[j][i], c[k][i] = c[k][i], c[j][i]
                    break
    return c

def F2(c):
    delete_sum = 0
    for i in range(H):
        for j in range(W):
            if c[i][j] == 'X': continue
            tmp = c[i][j]
            count = 1
            for k in range(j+1,W):
                if c[i][k] == tmp:
                    count += 1
                else:
                    break
            if count >= K:
                for k in range(j,W):
                    if c[i][k] == tmp:
                        delete_sum += int(c[i][k])
                        c[i][k] = 'X'
                    else:
                        break
    return c, delete_sum

MAX = 0
for i in range(H):
    for j in range(W):
        tmpC = []
        for k in range(H):
            tmpC.append(C[k][:])
        tmpC[i][j] = 'X'
        tmpC = F1(tmpC)
        point = 0
        for k in range(1000):
            tmpC, delete_sum = F2(tmpC)
            if delete_sum == 0:
                break
            tmpC = F1(tmpC)
            point += 2**k*delete_sum
        MAX = max(MAX,point)
print(MAX)

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



H, W, K = map(int, input().split())
C = [0] * H
for i in range(H):
    C[i] = list(input())

def solve(y, x, B):
    B[y][x] = '.'
    score = 0
    step = 0
    while True:
        updated = False
        
        for i in range(H - 1, -1, -1):
            for j in range(W - 1, -1, -1):
                if B[i][j] != '.':
                    continue
                k = i - 1
                while k >= 0 and B[k][j] == '.':
                    k -= 1
                if k >= 0:
                    B[i][j] = B[k][j]
                    B[k][j] = '.'
                    updated = True
        if not updated:
            break

        
        erased = 0
        for i in range(H):
            start = 0
            while start < W:
                count = 1
                if B[i][start] == '.':
                    start += 1
                    continue
                while start + count < W and B[i][start + count] == B[i][start]:
                    count += 1
                if count >= K:
                    erased += count * int(B[i][start])
                    for k in range(start, start + count):
                        B[i][k] = '.'
                start += count
        score += (2 ** step) * erased
        step += 1
    return score

ans = 0
for i in range(H):
    for j in range(W):
        B = [[C[i][j] for j in range(W)] for i in range(H)]
        ans = max(ans, solve(i, j, B))
print(ans)

Solution (Python):
import copy
from itertools import groupby
h,w,K = map(int,input().split())
l0 = [[int(i) for i in list(input())] for _ in range(h)]


def check(time,l):
    global K
    t = 0
    for i in range(h):
        gr = groupby(l[i])
        lst = []
        p = 0
        for key,g in gr:
            j = len(list(g))
            lst.append([p,key,j])
            p += j
        for j in range(len(lst)):
            if lst[j][1] == 0:
                continue
            if lst[j][2] >= K:
                t += (2**time)*lst[j][1]*lst[j][2]
                for k in range(lst[j][0],lst[j][0]+lst[j][2]):
                    l[i][k] = 0
    return t


def drop(l):
    for j in range(w):
        lst = []
        for i in range(h-1,-1,-1):
            if l[i][j] > 0:
                lst.append(l[i][j])
        lst += [0]*(h-len(lst))
        for i in range(h-1,-1,-1):
            l[i][j] = lst[h-i-1]

ans = 0
for i in range(h):
    for j in range(w):
        l = copy.deepcopy(l0)
        s = 0
        l[i][j] = 0
        drop(l)
        time = 0
        while True:
            temp = check(time, l)
            if temp == 0:
                break
            s += temp
            drop(l)
            time += 1
        ans = max(ans,s)
print(ans)

Solution (Python):


import sys
input=sys.stdin.readline
from collections import deque
from copy import deepcopy

def main():
    H,W,K=map(int,input().split())
    mas=[[-1]*W for i in range(H)]
    for i in range(H):
        t=input()
        for j in range(W):
            mas[H-1-i][j]=int(t[j])
    maso=deepcopy(mas)
    maxans=0       
    for h in range(H):
        for w in range(W):
            mas=deepcopy(maso)
            mas[h][w]=-1
            for hh in range(h,H-1):
                t=mas[hh][w]
                mas[hh][w]=mas[hh+1][w]
                mas[hh+1][w]=t
            
            ans=0
            nankaime=0
            while(1):
                kiesum=0
                mast=deepcopy(mas)
                for i in range(H):
                    for j in range(W):
                        cnt=1
                        for k in range(j+1,W):
                            if mas[i][j]!=-1 and mas[i][k]!=-1 and mas[i][j]==mas[i][k]:
                                cnt+=1
                            else:
                                break
                        if cnt>=K:
                            for k in range(cnt):
                                kiesum+=mas[i][j+k]
                                mas[i][j+k]=-1
                f=[deque() for i in range(W)]
                for j in range(W):
                    for i in range(H):
                        if mas[i][j]!=-1:
                            f[j].append(mas[i][j])
                    for i in range(H-len(f[j])):
                        f[j].append(-1)
                for i in range(H):
                    for j in range(W):
                        mas[i][j]=f[j].popleft()
                ans+=pow(2,nankaime)*kiesum
                nankaime+=1
                if mas==mast:
                    break
            maxans=max(maxans,ans)
    print(maxans)
                    
                    
                
    
if __name__=="__main__":
    main()

Solution (Python):
import copy

def score(i,j):
  Ali = copy.deepcopy(data)
  Ali[i][j] = 0
  score = 0
  turn = 0
  
  for i in range(1,H+1):
    for j in range(W):
      if Ali[-i][j] == 0:
        for k in range(i+1,H+1):
          if Ali[-k][j] != 0:
            Ali[-i][j] = Ali[-k][j]
            Ali[-k][j] = 0
            break
            
  while True:
    sc = score
    
    for i in range(H):
      a = 0 
      b = 0 
      
      for j in range(W):
        if Ali[i][j] != a:
          if j-b >= K:
            for k in range(b,j):
              score += Ali[i][k]*2**turn
              Ali[i][k] = 0
          a = Ali[i][j]
          b = j
      if W-b >= K:
        for k in range(b,W):
          score += Ali[i][k]*2**turn
          Ali[i][k] = 0
      
      
    if sc == score:
        break
    turn += 1
      
    
    
    for i in range(1,H+1):
      for j in range(W):
        if Ali[-i][j] == 0:
          for k in range(i+1,H+1):
            if Ali[-k][j] != 0:
              Ali[-i][j] = Ali[-k][j]
              Ali[-k][j] = 0
              break
              
  return score
  
H,W,K = map(int,input().split())
data = [[int(d) for d in input()] for _ in range(H)]
res = 0

for i in range(H):
  for j in range(W):
    res = max(res,score(i,j))
    
print(res)