def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 M = int(input[idx]) idx +=1 V = list(map(int, input[idx:idx+N])) idx += N S = [] for _ in range(N): S.append(input[idx]) idx +=1 boxes = [] for j in range(M): allowed = [] for i in range(N): if S[i][j] == 'o': allowed.append((V[i], i)) allowed.sort(reverse=True, key=lambda x: x[0]) total = sum(v for v, i in allowed) boxes.append((-total, allowed, j)) boxes.sort() boxes = [ (allowed, j) for (total, allowed, j) in boxes ] assigned = [False] * N sum_squares = 0 for allowed, j in boxes: current_sum = 0 for v, i in allowed: if not assigned[i]: current_sum += v assigned[i] = True sum_squares += current_sum ** 2 print(sum_squares) if __name__ == "__main__": main()