Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
import sys
def main():
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  grid=[list(input()) for _ in range(h)]
  def score(t):
    total=0
    for i in range(1,h):
      for j in range(w-1):
        cnt=1
        ten=int(gc[i][j])
        while gc[i][j+1]==gc[i][j]:
          cnt+=1
          j+=1
          if j==w-1:
            break
        if cnt>=k:
          total+=2**t*(ten*cnt)
          gc[i][j-cnt+1:j+1]=['0']*cnt
    return total
  def upd():
    while 1:
      ok=True
      for i in range(1,h):
        for j in range(w):
          if gc[i][j]=='0' and gc[i-1][j]!='0':
            x=gc[i-1][j]
            gc[i-1][j]='0'
            gc[i][j]=x
            ok=False
      if ok:
        return
  ans=0
  for i in range(1,h):
    for j in range(w):
      gc=deepcopy(grid)
      gc[i][j]='0'
      t=0
      res=0
      while 1:
        upd()
        total=score(t)
        if total==0:
          break
        t+=1
        res+=total
      ans=max(ans,res)
  return print(ans)
if __name__=='__main__':
  main()

Fixed submission:
from copy import deepcopy
import sys
def main():
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  grid=[]
  for _ in range(h):
    s=input().rstrip()
    s=list(s)
    s=list(map(int,s))
    grid.append(s)
  def score(t):
    total=0
    for i in range(1,h):
      for j in range(w-1):
        cnt=1
        ten=gc[i][j]
        while gc[i][j+1]==gc[i][j]:
          cnt+=1
          j+=1
          if j==w-1:
            break
        if cnt>=k:
          total+=2**t*(ten*cnt)
          gc[i][j-cnt+1:j+1]=[0]*cnt
    return total
  def upd():
    while 1:
      ok=True
      for i in range(1,h):
        for j in range(w):
          if gc[i][j]==0 and gc[i-1][j]!=0:
            x=gc[i-1][j]
            gc[i-1][j]=0
            gc[i][j]=x
            ok=False
      if ok:
        return
  ans=0
  for i in range(1,h):
    for j in range(w):
      gc=deepcopy(grid)
      gc[i][j]=0
      t=0
      res=0
      while 1:
        upd()
        total=score(t)
        if total==0:
          break
        t+=1
        res+=total
      ans=max(ans,res)
  return print(ans)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
def main():
  h,w,k=map(int,input().split())
  grid=[list(input()) for _ in range(h)]
  def score(t):
    total=0
    for i in range(1,h):
      for j in range(w-1):
        cnt=1
        ten=int(gc[i][j])
        while gc[i][j+1]==gc[i][j]:
          cnt+=1
          j+=1
          if j==w-1:
            break
        if cnt>=k:
          total+=2**t*(ten*cnt)
          gc[i][j-cnt+1:j+1]=['0']*cnt
    return total
  def upd():
    while 1:
      ok=True
      for i in range(1,h):
        for j in range(w):
          if gc[i][j]=='0' and gc[i-1][j]!='0':
            x=gc[i-1][j]
            gc[i-1][j]='0'
            gc[i][j]=x
            ok=False
      if ok:
        return
  ans=0
  for i in range(1,h):
    for j in range(w):
      gc=deepcopy(grid)
      gc[i][j]='0'
      t=0
      res=0
      while 1:
        upd()
        total=score(t)
        if total==0:
          break
        t+=1
        res+=total
      ans=max(ans,res)
  return print(ans)
if __name__=='__main__':
  main()

Fixed submission:
from copy import deepcopy
import sys
def main():
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  grid=[]
  for _ in range(h):
    s=input().rstrip()
    s=list(s)
    s=list(map(int,s))
    grid.append(s)
  def score(t):
    total=0
    for i in range(1,h):
      for j in range(w-1):
        cnt=1
        ten=gc[i][j]
        while gc[i][j+1]==gc[i][j]:
          cnt+=1
          j+=1
          if j==w-1:
            break
        if cnt>=k:
          total+=2**t*(ten*cnt)
          gc[i][j-cnt+1:j+1]=[0]*cnt
    return total
  def upd():
    while 1:
      ok=True
      for i in range(1,h):
        for j in range(w):
          if gc[i][j]==0 and gc[i-1][j]!=0:
            x=gc[i-1][j]
            gc[i-1][j]=0
            gc[i][j]=x
            ok=False
      if ok:
        return
  ans=0
  for i in range(1,h):
    for j in range(w):
      gc=deepcopy(grid)
      gc[i][j]=0
      t=0
      res=0
      while 1:
        upd()
        total=score(t)
        if total==0:
          break
        t+=1
        res+=total
      ans=max(ans,res)
  return print(ans)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
h,w,k=map(int,input().split())
grid=[list(input()) for _ in range(h)]
def score(t):
  total=0
  for i in range(1,h):
    for j in range(w-1):
      cnt=1
      ten=int(gc[i][j])
      while gc[i][j+1]==gc[i][j]:
        cnt+=1
        j+=1
        if j==w-1:
          break
      if cnt>=k:
        total+=2**t*(ten*cnt)
        gc[i][j-cnt+1:j+1]=['0']*cnt
  return total
def upd():
  while 1:
    ok=True
    for i in range(1,h):
      for j in range(w):
        if gc[i][j]=='0' and gc[i-1][j]!='0':
          x=gc[i-1][j]
          gc[i-1][j]='0'
          gc[i][j]=x
          ok=False
    if ok:
      return
ans=0
for i in range(1,h):
  for j in range(w):
    gc=deepcopy(grid)
    gc[i][j]='0'
    t=0
    res=0
    while 1:
      upd()
      total=score(t)
      if total==0:
        break
      t+=1
      res+=total
    ans=max(ans,res)
print(ans)

Fixed submission:
from copy import deepcopy
import sys
def main():
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  grid=[]
  for _ in range(h):
    s=input().rstrip()
    s=list(s)
    s=list(map(int,s))
    grid.append(s)
  def score(t):
    total=0
    for i in range(1,h):
      for j in range(w-1):
        cnt=1
        ten=gc[i][j]
        while gc[i][j+1]==gc[i][j]:
          cnt+=1
          j+=1
          if j==w-1:
            break
        if cnt>=k:
          total+=2**t*(ten*cnt)
          gc[i][j-cnt+1:j+1]=[0]*cnt
    return total
  def upd():
    while 1:
      ok=True
      for i in range(1,h):
        for j in range(w):
          if gc[i][j]==0 and gc[i-1][j]!=0:
            x=gc[i-1][j]
            gc[i-1][j]=0
            gc[i][j]=x
            ok=False
      if ok:
        return
  ans=0
  for i in range(1,h):
    for j in range(w):
      gc=deepcopy(grid)
      gc[i][j]=0
      t=0
      res=0
      while 1:
        upd()
        total=score(t)
        if total==0:
          break
        t+=1
        res+=total
      ans=max(ans,res)
  return print(ans)
if __name__=='__main__':
  main()