Buggy python submission (verdict: Time Limit Exceeded):
# https://atcoder.jp/contests/s8pc-3/tasks/s8pc_3_b

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
                            break
                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()

Fixed submission:
# https://atcoder.jp/contests/s8pc-3/tasks/s8pc_3_b

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()