## 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化する bit_sums = [0] * (2 ** M) for i in range(N): bit = 0 s = S[i] for m in range(M): if s[m] == "o": bit |= (1 << m) bit_sums[bit] += V[i] # 高速ゼータ変換で任意の箱の集合Sの中に収まる範囲でしか入らないスライムVの総和を計算 zz = [[0] * (2 ** M) for _ in range(M + 1)] for i in range(2 ** M): zz[0][i] = bit_sums[i] for j in range(M): for i in range(2 ** M): if i & (1 << j) > 0: zz[j + 1][i] = zz[j][i - (1 << j)] + zz[j][i] else: zz[j + 1][i] = zz[j][i] # dp[選ばれていない箱S]についてdpを解く dp = [0] * (2 ** M) for s in range(2 ** M): for i in range(M): if s & (1 << i) > 0: s_ = s - (1 << i) dp[s] = max(dp[s], dp[s_] + (zz[-1][s] - zz[-1][s_]) ** 2) print(dp[-1]) if __name__ == "__main__": main()