Buggy python submission (verdict: Time Limit Exceeded):
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for ii in xrange(h):
        for jj in xrange(w):
            a = [c[i][:] for i in xrange(h)]
            a[ii][jj] = 0
            r = 0
            e = 1
            while 1:
                for j in xrange(w):
                    st = [a[i][j] for i in xrange(h) if a[i][j]]
                    for i in xrange(h):
                        a[i][j] = 0
                    i = -1
                    for x in reversed(st):
                        a[i][j] = x
                        i -= 1
                res = 0
                for i in xrange(h):
                    j = 0
                    while j < w:
                        t = j + 1
                        while t < w and a[i][j] == a[i][t]:
                            t += 1
                        if t - j >= k:
                            for j in xrange(j, t):
                                res += a[i][j]
                                a[i][j] = 0
                        j = t
                if not res:
                    break
                r += res * e
                e *= 2
            if ans < r:
                ans = r
    print ans
main()


Fixed submission:
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for ii in xrange(h):
        for jj in xrange(w):
            a = [c[i][:] for i in xrange(h)]
            a[ii][jj] = 0
            r = 0
            e = 1
            while 1:
                for j in xrange(w):
                    st = [a[i][j] for i in xrange(h) if a[i][j]]
                    for i in xrange(h):
                        a[i][j] = 0
                    i = -1
                    for x in reversed(st):
                        a[i][j] = x
                        i -= 1
                res = 0
                for i in xrange(h):
                    j = 0
                    while j < w:
                        t = j + 1
                        while t < w and a[i][j] == a[i][t]:
                            t += 1
                        if t - j >= k:
                            for j in xrange(j, t):
                                res += a[i][j]
                                a[i][j] = 0
                        j = t
                if not res:
                    break
                r += res * e
                e *= 2
            if ans < r:
                ans = r
    print ans
main()

Buggy python submission (verdict: Runtime Error):
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for ii in xrange(h):
        for jj in xrange(w):
            a = [c[i][:] for i in xrange(h)]
            a[ii][jj] = 0
            r = 0
            e = 1
            while res:
                for j in xrange(w):
                    st = [a[i][j] for i in xrange(h) if a[i][j]]
                    for i in xrange(h):
                        a[i][j] = 0
                    i = -1
                    for x in reversed(st):
                        a[i][j] = x
                        i -= 1
                res = 0
                for i in xrange(h):
                    j = 0
                    while j < w:
                        t = j + 1
                        while t < w and a[i][j] == a[i][t]:
                            t += 1
                        if t - j >= k:
                            for j in xrange(j, t):
                                res += a[i][j]
                                a[i][j] = 0
                        j = t
                if not res:
                    break
                r += res * e
                e *= 2
            if ans < r:
                ans = r
    print ans
main()


Fixed submission:
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for ii in xrange(h):
        for jj in xrange(w):
            a = [c[i][:] for i in xrange(h)]
            a[ii][jj] = 0
            r = 0
            e = 1
            while 1:
                for j in xrange(w):
                    st = [a[i][j] for i in xrange(h) if a[i][j]]
                    for i in xrange(h):
                        a[i][j] = 0
                    i = -1
                    for x in reversed(st):
                        a[i][j] = x
                        i -= 1
                res = 0
                for i in xrange(h):
                    j = 0
                    while j < w:
                        t = j + 1
                        while t < w and a[i][j] == a[i][t]:
                            t += 1
                        if t - j >= k:
                            for j in xrange(j, t):
                                res += a[i][j]
                                a[i][j] = 0
                        j = t
                if not res:
                    break
                r += res * e
                e *= 2
            if ans < r:
                ans = r
    print ans
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        for j in xrange(w):
            st = [a[i][j] for i in xrange(h) if a[i][j]]
            for i in xrange(h):
                a[i][j] = 0
            i = -1
            for x in reversed(st):
                a[i][j] = x
                i -= 1
        res = 0
        for i in xrange(h):
            j = 0
            while j < w:
                t = j
                while t < w and a[i][j] == a[i][t]:
                    t += 1
                if t - j >= k:
                    for j in xrange(j, t):
                        res += a[i][j]
                        a[i][j] = 0
                j = t
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for i in xrange(h):
        for j in xrange(w):
            t = [c[ii][:] for ii in xrange(h)]
            t[i][j] = 0
            r = go(t)
            if ans < r:
                ans = r
    print ans
main()


Fixed submission:
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for ii in xrange(h):
        for jj in xrange(w):
            a = [c[i][:] for i in xrange(h)]
            a[ii][jj] = 0
            r = 0
            e = 1
            while 1:
                for j in xrange(w):
                    st = [a[i][j] for i in xrange(h) if a[i][j]]
                    for i in xrange(h):
                        a[i][j] = 0
                    i = -1
                    for x in reversed(st):
                        a[i][j] = x
                        i -= 1
                res = 0
                for i in xrange(h):
                    j = 0
                    while j < w:
                        t = j + 1
                        while t < w and a[i][j] == a[i][t]:
                            t += 1
                        if t - j >= k:
                            for j in xrange(j, t):
                                res += a[i][j]
                                a[i][j] = 0
                        j = t
                if not res:
                    break
                r += res * e
                e *= 2
            if ans < r:
                ans = r
    print ans
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        for j in xrange(w):
            st = [a[i][j] for i in xrange(h) if a[i][j]]
            for i in xrange(h):
                a[i][j] = 0
            i = -1
            for x in reversed(st):
                a[i][j] = x
                i -= 1
        b = [a[i][:] for i in xrange(h)]
        res = 0
        for i in xrange(h):
            j = 0
            while j < w:
                t = j
                while t < w and a[i][j] == a[i][t]:
                    t += 1
                if t - j >= k:
                    for j in xrange(j, t):
                        res += a[i][j]
                        b[i][j] = 0
                j = t
        if not res:
            return 0
        return go(b) * 2 + res
    ans = 0
    for i in xrange(h):
        for j in xrange(w):
            t = [c[ii][:] for ii in xrange(h)]
            t[i][j] = 0
            r = go(t)
            if ans < r:
                ans = r
    print ans
main()


Fixed submission:
def main():
    h, w, k = map(int, raw_input().split())
    c = [map(int, raw_input().strip()) for _ in xrange(h)]
    def go(a):
        if not res:
            return 0
        return go(a) * 2 + res
    ans = 0
    for ii in xrange(h):
        for jj in xrange(w):
            a = [c[i][:] for i in xrange(h)]
            a[ii][jj] = 0
            r = 0
            e = 1
            while 1:
                for j in xrange(w):
                    st = [a[i][j] for i in xrange(h) if a[i][j]]
                    for i in xrange(h):
                        a[i][j] = 0
                    i = -1
                    for x in reversed(st):
                        a[i][j] = x
                        i -= 1
                res = 0
                for i in xrange(h):
                    j = 0
                    while j < w:
                        t = j + 1
                        while t < w and a[i][j] == a[i][t]:
                            t += 1
                        if t - j >= k:
                            for j in xrange(j, t):
                                res += a[i][j]
                                a[i][j] = 0
                        j = t
                if not res:
                    break
                r += res * e
                e *= 2
            if ans < r:
                ans = r
    print ans
main()