n, m = map(int, input().split()) v = list(map(int, input().split())) allowed_boxes_list = [] for _ in range(n): s = input().strip() allowed = [j for j, c in enumerate(s) if c == 'o'] allowed_boxes_list.append(allowed) # Compute potential_sum for each box potential_sum = [0] * m for i in range(n): vi = v[i] for j in allowed_boxes_list[i]: potential_sum[j] += vi # Sort slimes in descending order of value sorted_slimes = sorted(zip(v, allowed_boxes_list), key=lambda x: -x[0]) current_sum = [0] * m for value, allowed in sorted_slimes: # Find the maximum current sum among allowed boxes max_curr = max(current_sum[j] for j in allowed) # Collect all boxes with this maximum current sum candidates = [j for j in allowed if current_sum[j] == max_curr] # Among candidates, select the one with the highest potential_sum best_j = candidates[0] max_potential = potential_sum[best_j] for j in candidates[1:]: if potential_sum[j] > max_potential: max_potential = potential_sum[j] best_j = j current_sum[best_j] += value # Calculate the total sum of squares total = sum(x * x for x in current_sum) print(total)