## https://yukicoder.me/problems/no/1912 def main(): N, M = map(int, input().split()) V = list(map(int, input().split())) S = [] for _ in range(N): S.append(input()) # 各Nについて使える箱をbitmask化する usable_box_set = [] for i in range(N): bit = 0 s = S[i] for m in range(M): if s[m] == "o": bit |= (1 << m) usable_box_set.append(bit) # 箱に入れた時の価値を最大にしてく used = 0 answer = 0 for _ in range(M): values = [] for i in range(M): if used & (1 << i) > 0: continue total_value = 0 for j in range(N): bit = usable_box_set[j] v = V[j] if bit & (1 << i) > 0 and bit & used == 0: total_value += v values.append((i, total_value)) values.sort(key=lambda x: x[1],reverse=True) index, v = values[0] used |= (1 << index) answer += v ** 2 print(answer) if __name__ == "__main__": main()