Problem p02613 (python)

Fixed submission:
from collections import Counter

N = int(input())
S = []
for i in range(N):
    s_in = input()
    # if s_in == '' or s_in is None: break
    S.append(s_in)

counter = Counter(S)
for ans in ['AC', 'WA', 'TLE', 'RE']:
    print(f'{ans} x {counter[ans]}')

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())

C = []
for _ in range(H):
    C.append(input())

from pprint import pprint
# pprint(C)
import sys
from collections import Counter
from itertools import product, combinations

counter = Counter()
for row in C:
    counter.update(row)
n_blacks = counter['#']

if n_blacks < K:
    print(0)
    sys.exit()
# elif n_blacks == K:
#     # 黒がいない行、列があると破綻するからこの elif は NG
#     print(1)
#     sys.exit()


board = ''
for row in C:
    board += row

pprint(board)

def get_calc_board_index_row(board, n_rows, n_cols):
    b = board
    _n_rows = n_rows
    _n_cols = n_cols
    def fun(n_row):
        # row number
        return list(range(_n_rows * n_row, _n_rows * n_row + _n_cols))
    return fun

calc_board_index_row = get_calc_board_index_row(board=board, n_rows=H, n_cols=W)

def get_calc_board_index_col(board, n_rows, n_cols):
    b = board
    _n_rows = n_rows
    _n_cols = n_cols
    def fun(n_col):
        # row number
        return list(range(
            n_col,
            n_col + _n_rows * _n_cols,
            _n_cols))
    return fun

calc_board_index_col = get_calc_board_index_col(board=board, n_rows=H, n_cols=W)

def get_print_board(board, n_rows, n_cols):
    b = board
    _n_rows = n_rows
    _n_cols = n_cols
    def fun():
        # row number
        for r in _n_rows:
            print(b[r*_n_cols, r*_n_cols+_n_cols])
    return fun

print_board = get_print_board(board, H, W)

# sys.exit()

# 消し込む行と列の数 (n_row, n_col) について、すべてのパターンで繰り返す
# i = 0
ans = 0
for n_row, n_col in product(range(H), range(K)):
    # n_row 個の行を消し込む場合の、行番号の組み合わせ
    choosed_rows = list(combinations(range(H), n_row))
    # n_col 個の列を消し込む場合の、列番号の組み合わせ
    choosed_cols = list(combinations(range(W), n_col))

    # 消し込みするマスを計算
    board_idx = set()
    # for removing_row_numbers in choosed_rows:
    #     for r in removing_row_numbers:
    #         board_idx.update(calc_board_index_row(r))
    # for removing_col_numbers in choosed_cols:        
    #     for c in removing_col_numbers:
    #         board_idx.update(calc_board_index_col(c))
    for i_r, i_c in product(range(len(choosed_rows)), range(len(choosed_cols))):
        for _ in choosed_rows[i_r]:
            board_idx.update(calc_board_index_row(_))
        for _ in choosed_cols[i_c]:
            board_idx.update(calc_board_index_col(_))

        rest_blacks = 0
        for ix in range(len(board)):
            if ix in board_idx: continue
            if board[ix] == '#': rest_blacks += 1

        if rest_blacks == K:
            # print(removing_row_numbers, removing_col_numbers)
            ans += 1
    # print({
    #     'n_row': n_row,
    #     'n_col': n_col,
    #     'choosed_rows': choosed_rows,
    #     'choosed_cols' : choosed_cols,
    #     'board_idx': board_idx,
    #     'rest_blacks': rest_blacks
    # })
    # print()

print(ans)