n, m = map(int, input().split()) v = list(map(int, input().split())) s_list = [input().strip() for _ in range(n)] # Create list of slimes with their values and compatibility strings, sorted by value descending slimes = sorted(zip(v, s_list), key=lambda x: (-x[0], x[1])) # Precompute sumVj for each box j (1-based) sumVj = [0] * (m + 1) # sumVj[1] to sumVj[m] for j in range(1, m + 1): total = 0 for i in range(n): if s_list[i][j-1] == 'o': total += v[i] sumVj[j] = total # Initialize current sum for each box (1-based) current_sum = [0] * (m + 1) for value, s in slimes: allowed = [] for j in range(1, m + 1): if s[j-1] == 'o': allowed.append(j) # Find the maximum current sum among allowed boxes max_cs = max(current_sum[j] for j in allowed) # Collect all boxes that have this maximum current sum candidates = [j for j in allowed if current_sum[j] == max_cs] # Choose the candidate with the highest sumVj selected = max(candidates, key=lambda x: sumVj[x]) current_sum[selected] += value # Calculate the total sum of squares total = sum(s * s for s in current_sum) print(total)