n, m = map(int, input().split()) v = list(map(int, input().split())) s_list = [input().strip() for _ in range(n)] # Precompute count of slimes per box count = [0] * m for s in s_list: for j in range(m): if s[j] == 'o': count[j] += 1 # Pair each slime's value with its allowed boxes and sort by value descending slimes = sorted(zip(v, s_list), key=lambda x: -x[0]) sums = [0] * m for val, s in slimes: allowed = [] for j in range(m): if s[j] == 'o': allowed.append(j) max_gain = -1 best_j = -1 max_cnt = -1 for j in allowed: current_sum = sums[j] gain = 2 * current_sum * val + val * val if gain > max_gain or (gain == max_gain and count[j] > max_cnt): max_gain = gain best_j = j max_cnt = count[j] sums[best_j] += val total = sum(x ** 2 for x in sums) print(total)