import sys import numpy as np def main(): buf = sys.stdin.buffer n, k = map(int, buf.readline().split()) n2 = 2 * n S = np.empty((n2, n2), np.bool_) for i in range(n2): S[i] = np.frombuffer(buf.readline().rstrip(), 'S1') == b'#' C = np.empty_like(S, np.int64) for i in range(n2): C[i] = np.fromstring(buf.readline().rstrip().decode(), np.int64, sep=' ') m = np.count_nonzero(S) ans = np.sort(C.ravel())[-m:].sum() if ~m & 1: ans = max(ans, np.sort((C[:, :n] + np.fliplr(C[:, n:])).ravel())[-(m // 2):].sum() + k) print(ans) if __name__ == '__main__': main()