Buggy python submission (verdict: Time Limit Exceeded):
'''
https://atcoder.jp/contests/s8pc-3/tasks/s8pc_3_b
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    h,w,k = map(int, input().split())
    banmen = []
    for _ in range(h):
        s = input().rstrip()
        s = list(s)
        s = list(map(int, s))
        banmen.append(s)

    def update():
        while 1:
            cnt = 0
            for i in range(h-1):
                for j in range(w):
                    if c[i+1][j] == 0 and c[i][j] != 0:
                        c[i][j], c[i+1][j] = c[i+1][j], c[i][j]
                        cnt += 1
            if cnt == 0:
                return

    #n回目(0~)
    def get(n):
        total = 0
        for i in range(h):
            for j in range(0, w-k+1, 1):
                ten = c[i][j]
                cnt = 1
                while c[i][j] == c[i][j+1]:
                    cnt += 1
                    j += 1
                    if j == w-1:
                        break
                if cnt >= k:
                    total += 2**n * (ten*cnt)
                    c[i][j-(cnt-1):j+1] = [0]*cnt
        return total

    def solve(i, j):
        c[i][j] = 0
        res = 0
        n = 0
        while 1:
            update()
            total = get(n)
            if total == 0:
                break
            n += 1
            res += total
        return res

    import copy
    res = 0
    for i in range(h):
        for j in range(w):
            c = copy.deepcopy(banmen) 
            res = max(res, solve(i, j))
    print(res)

if __name__ == '__main__':
    main()

Fixed submission:
'''
https://atcoder.jp/contests/s8pc-3/tasks/s8pc_3_b
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    h,w,k = map(int, input().split())
    banmen = []
    for _ in range(h):
        s = input().rstrip()
        s = list(s)
        s = list(map(int, s))
        banmen.append(s)

    def update():
        while 1:
            cnt = 0
            for i in range(h-1):
                for j in range(w):
                    if c[i+1][j] == 0 and c[i][j] != 0:
                        c[i][j], c[i+1][j] = c[i+1][j], c[i][j]
                        cnt += 1
            if cnt == 0:
                return

    #n回目(0~)
    def get(n):
        total = 0
        for i in range(h):
            for j in range(0, w-k+1, 1):
                ten = c[i][j]
                cnt = 1
                while c[i][j] == c[i][j+1]:
                    cnt += 1
                    j += 1
                    if j == w-1:
                        break
                if cnt >= k:
                    total += 2**n * (ten*cnt)
                    c[i][j-(cnt-1):j+1] = [0]*cnt
        return total

    def solve(i, j):
        c[i][j] = 0
        res = 0
        n = 0
        while 1:
            update()
            total = get(n)
            if total == 0:
                break
            n += 1
            res += total
        return res

    import copy
    res = 0
    for i in range(h):
        for j in range(w):
            c = copy.deepcopy(banmen) 
            res = max(res, solve(i, j))
    print(res)

if __name__ == '__main__':
    main()