Problem p02855 (python)

Fixed submission:
import copy
class Strawberry():
    def __init__(self,id, h, w):
        self.id = id
        self.h = h
        self.w = w

def main():
    h, w, k = map(int, input().split())
    strawberry_area = [[0 for _ in range(w)] for _ in range(h)]
    strawberry_id = 0
    first_row_strawberries = []
    for h_index in range(h):
        line = input()
        line_list = list(line)
        row_strawberries = 0
        strawberries = []
        for w_index in range(w):
            if line_list[w_index] == "#":
                strawberry_id += 1
                row_strawberries += 1
                strawberries.append(Strawberry(strawberry_id, h_index, w_index))
        if len(first_row_strawberries) == 0 and len(strawberries) != 0:
            first_row_strawberries = copy.copy(strawberries)

        if row_strawberries == 0 and h_index != 0:
            for w_index in range(w):
                strawberry_area[h_index][w_index] = strawberry_area[h_index-1][w_index]

        if row_strawberries != 0:
            for strawberry_index in range(len(strawberries)):
                strawberry = strawberries[strawberry_index]
                if strawberry_index == 0:
                    for w_index in range(0, strawberry.w + 1):
                        strawberry_area[h_index][w_index] = strawberry.id

                if strawberry_index != len(strawberries)-1:
                    next_strawberry = strawberries[strawberry_index+1]
                    for w_index in range(strawberry.w, next_strawberry.w):
                        strawberry_area[h_index][w_index] = strawberry.id

                if strawberry_index == len(strawberries)-1:
                    for w_index in range(strawberry.w, w):
                        strawberry_area[h_index][w_index] = strawberry.id
    first_h = first_row_strawberries[0].h
    for h_index in range(first_h-1, -1, -1):
        for w_index in range(w):
            strawberry_area[h_index][w_index] = strawberry_area[h_index+1][w_index]

    for strawberry_line in strawberry_area:
        print(" ".join(map(str, strawberry_line)))


main()

Buggy submission (verdict: Runtime Error):
import copy
class Strawberry():
    def __init__(self,id, h, w):
        self.id = id
        self.h = h
        self.w = w

def main():
    h, w, k = map(int, input().split())
    strawberry_area = [[0 for _ in range(w)] for _ in range(h)]
    strawberry_id = 0
    first_row_strawberries = []
    for h_index in range(h):
        line = input()
        line_list = list(line)
        row_strawberries = 0
        strawberries = []
        for w_index in range(w):
            if line_list[w_index] == "#":
                strawberry_id += 1
                row_strawberries += 1
                strawberries.append(Strawberry(strawberry_id, h_index, w_index))
        if len(first_row_strawberries) == 0 and len(strawberries) != 0:
            first_row_strawberries = copy.copy(strawberries)

        if row_strawberries == 0 and h_index != 0:
            for w_index in range(w):
                strawberry_area[h_index][w_index] = strawberry_area[h_index-1][w_index]

        if row_strawberries != 0:
            for strawberry_index in range(len(strawberries)):
                strawberry = strawberries[strawberry_index]
                if strawberry_index == 0:
                    for w_index in range(0, strawberry.w + 1):
                        strawberry_area[h_index][w_index] = strawberry.id

                if strawberry_index != len(strawberries)-1:
                    next_strawberry = strawberries[strawberry_index+1]
                    for w_index in range(strawberry.w, next_strawberry.w):
                        strawberry_area[h_index][w_index] = strawberry.id

                if strawberry_index == len(strawberries)-1:
                    for w_index in range(strawberry.w, w):
                        strawberry_area[h_index][w_index] = strawberry.id
    first_h = first_row_strawberries[1].h
    for h_index in range(first_h-1, -1, -1):
        for w_index in range(w):
            strawberry_area[h_index][w_index] = strawberry_area[h_index+1][w_index]

    for strawberry_line in strawberry_area:
        print(" ".join(map(str, strawberry_line)))


main()

Buggy submission (verdict: Runtime Error):
import copy
class Strawberry():
    def __init__(self,id, h, w):
        self.id = id
        self.h = h
        self.w = w

def main():
    h, w, k = map(int, input().split())
    strawberry_area = [[0 for _ in range(w)] for _ in range(h)]
    strawberry_id = 0
    first_row_strawberries = []
    for h_index in range(h):
        line = input()
        line_list = list(line)
        row_strawberries = 0
        strawberries = []
        for w_index in range(w):
            if line_list[w_index] == "#":
                strawberry_id += 1
                row_strawberries += 1
                strawberries.append(Strawberry(strawberry_id, h_index, w_index))
        if len(first_row_strawberries) == 0 and len(strawberries) != 0:
            first_row_strawberries = copy.copy(strawberries)

        if row_strawberries == 0 and h_index != 0:
            for w_index in range(w):
                strawberry_area[h_index][w_index] = strawberry_area[h_index-1][w_index]

        if row_strawberries != 0:
            for strawberry_index in range(len(strawberries)):
                strawberry = strawberries[strawberry_index]
                if strawberry_index == 0:
                    for w_index in range(0, strawberry.w + 1):
                        strawberry_area[h_index][w_index] = strawberry.id

                if strawberry_index != len(strawberries)-1:
                    next_strawberry = strawberries[strawberry_index+1]
                    for w_index in range(strawberry.w, next_strawberry.w):
                        strawberry_area[h_index][w_index] = strawberry.id

                if strawberry_index == len(strawberries)-1:
                    for w_index in range(strawberry.w, w):
                        strawberry_area[h_index][w_index] = strawberry.id
    first_h = first_row_strawberries[1].h
    for h_index in range(first_h-1, -1, -1):
        for w_index in range(w):
            strawberry_area[h_index][w_index] = strawberry_area[h_index+1][w_index]
    print(strawberry_area)


main()