{$q+,r+,o-}
{$minstacksize 30000000}
uses
    testlib, SysUtils;
const
    max_v = 1000000;

const
    dx: array [1..6] of longint = (0, 0, 0, 0, -1, 1);
    dy: array [1..6] of longint = (0, 0, -1, 1, 0, 0);
    dz: array [1..6] of longint = (-1, 1, 0, 0, 0, 0);
var
    n: longint;
    h, w, l: int64;
    i, j, k: longint;
    a: array of array of string;
    f: array of array of array of byte;
    line: string;
    color: array [char] of longint;
    colors: longint;
    q: array [char, char] of boolean;

procedure fill(i, j, k, c: longint; ch: char);
var
    z: longint;
begin
    if (i < 1) or (h < i) or (j < 1) or (l < j) or (k < 1) or (w < k) then begin
        exit;
    end;

    if (a[i][j][k] = ch) and (f[i][j][k] = 0) then begin
        f[i][j][k] := c;
        for z := 1 to 6 do begin
            fill(i + dx[z], j + dy[z], k + dz[z], c, ch);
        end;
    end else begin
        q[a[i][j][k]][ch] := true;
    end;
end;

const
    chars: set of char = ['A'..'Z', 'a'..'z'];

var
    ch1, ch2: char;
begin
    n := inf.readLongint();

    h := ouf.readLongint();
    w := ouf.readLongint();
    l := ouf.readLongint();

    if (h <= 0) or (h > max_v) then begin
        quit(_wa, format('Invalid h: %d', [h]));
    end;
    if (w <= 0) or (w > max_v) then begin
        quit(_wa, format('Invalid w: %d', [w]));
    end;
    if (l <= 0) or (l > max_v) then begin
        quit(_wa, format('Invalid l: %d', [l]));
    end;
    if (h * w * l > max_v) then begin
        quit(_wa, format('Total volume too large %dx%dx%d', [h, w, l]));
    end;

    setLength(a, h + 1);
    setLength(f, h + 1);
    for i := 1 to h do begin
        if (trim(ouf.readString()) <> '') then begin
            quit(_wa, format('Empty line missed before floor %d', [i]));
        end;
        setLength(a[i], l + 1);
        setLength(f[i], l + 1);
        for j := 1 to l do begin
            setLength(f[i][j], w + 1);
            line := ouf.readString();
            if (length(line) <> w) then begin
                quit(_wa, format('Line %d of floor %d has length %d instead of %d', [j, i, length(line), w]));
            end;
            for k := 1 to w do begin
                if not (line[k] in chars) then begin
                    quit(_wa, format('Invalid character at col %d of line %d of floor %d: %s', [k, j, i, line[k]]));
                end;
            end;
            a[i][j] := line;
        end;
    end;

    colors := 0;
    for i := 1 to h do begin
        for j := 1 to l do begin
            for k := 1 to w do begin
                if f[i][j][k] = 0 then begin
                    if (color[a[i][j][k]] <> 0) then begin
                        quit(_wa, format('Country "%s" is not continuous', [a[i][j][k]]));
                    end;
                    inc(colors);
                    color[a[i][j][k]] := colors;
                    fill(i, j, k, colors, a[i][j][k]);
                end;
            end;
        end;
    end;

    if colors <> n then begin
        quit(_wa, format('%d countries instead of %d', [colors, n]));
    end;

    for ch1 := 'A' to 'z' do begin
        for ch2 := succ(ch1) to 'z' do begin
            if (color[ch1] <> 0) and (color[ch2] <> 0) and not q[ch1][ch2] then begin
                quit(_wa, format('Countries "%s" and "%s" are not adjacent', [ch1, ch2]));
            end;
        end;
    end;
    quit(_ok, format('%dx%dx%d', [h, w, l]));
end.
