n, m = map(int, input().split()) v = list(map(int, input().split())) s = [input().strip() for _ in range(n)] sum_potential = [0] * m allowed_boxes = [[] for _ in range(n)] for i in range(n): current_s = s[i] boxes = [] for j in range(m): if current_s[j] == 'o': boxes.append(j) sum_potential[j] += v[i] allowed_boxes[i] = boxes # Create a list of (v, allowed_boxes) sorted by descending v slimes = sorted(zip(v, allowed_boxes), key=lambda x: (-x[0], x[1])) sum_boxes = [0] * m for val, boxes in slimes: if not boxes: continue # As per problem statement, each slime has at least one 'o' # Find max current sum among allowed boxes max_sum = max(sum_boxes[j] for j in boxes) candidates = [j for j in boxes if sum_boxes[j] == max_sum] # Select the candidate with the highest sum_potential selected_j = candidates[0] max_p = sum_potential[selected_j] for j in candidates[1:]: if sum_potential[j] > max_p: max_p = sum_potential[j] selected_j = j sum_boxes[selected_j] += val # Update sum_potential for all allowed boxes of this slime for j in boxes: sum_potential[j] -= val # Calculate the result result = sum(x * x for x in sum_boxes) print(result)