uses
	testlib, SysUtils;
type integer = longint;
const 
    MAXN = 50;
    DX : array[1..4] of integer = (-1,1,0,0);
    DY : array[1..4] of integer = (0,0,-1,1);
var
	n : longint;
    u : array[1..MAXN, 1..MAXN] of boolean;
    f, a : array[1..MAXN] of string[MAXN];

function inside(x, y : integer) : boolean;
begin
    result := (1 <= x) and (x <= n) and (1 <= y) and (y <= n);
end;

procedure dfs(x, y : integer; c : char);
var it, nx, ny : integer;
begin
    u[x][y] := true;
    for it := 1 to 4 do begin
        nx := x + DX[it];
        ny := y + DY[it];
        if (inside(nx, ny)) and (not u[nx][ny]) and (a[nx][ny] = c) then begin
                dfs(nx, ny, c);
        end;
    end;
end;

procedure checkConnectivity(c : char);
var
    i, j : integer;
    was : boolean;
begin
    fillchar(u, sizeof(u), 0);
    was := false;
    for i := 1 to n do begin
        for j := 1 to n do begin
            if (a[i][j] = c) then begin
                if (not was) then begin
                    dfs(i, j, c);
                    was := true;
                end
                else begin
                    if not u[i][j] then begin
                        quit(_wa, 'Color ' + c + ' doesn''t connected');
                    end;
                end;
            end;
        end;
    end;
end;

var
    i, j : integer;
    first, second : integer;
begin
	n := inf.readLongint;
    inf.nextLine; 
    for i := 1 to n do begin
            f[i] := inf.readString;
    end;
    for i := 1 to n do begin
            a[i] := ouf.readString;
            if (length(a[i]) <> n) then begin
                quit(_pe, 'Wrong length of line: ' + intToStr(i));
            end;
            for j := 1 to n do begin
                if (a[i][j] <> '1') and (a[i][j] <> '2') then begin
                    quit(_pe, 'Wrong symbol in output: expected 1 or 2, got "' + a[i][j] +'"');
                end;
            end;
    end;
    checkConnectivity('1');
    checkConnectivity('2');
    first := 0;
    second := 0;
    for i := 1 to n do begin
        for j := 1 to n do begin
            if (f[i][j] = 'C') then begin
                if (a[i][j] = '1') then begin
                    inc(first);
                end
                else if (a[i][j] = '2') then begin
                    inc(second);
                end
                else begin
                    quit(_fail, 'Assertion error, this is impossible')
                end;
            end;
        end;
    end;
    if (first <> second) then begin
        quit(_wa, 'Parts contain different number of cities: ' +
            'part one contain ' + intToStr(first) + 
            ', and part two contain ' + intToStr(second));
    end;
    quit(_ok, intToStr(first) + ' cities per partition');
end.
