<?php


/**
 * Polcode Code Contest PHP-2010.10
 *
 * Data: 2010-11-02
 * Nick: ogaws
 * Nazwisko: Gibuła
 * Imię: Krzysztof
 * Data urodzenia: 1985-07-23
 * e-mail: krzysztof.gibula@gmail.com
 */


$alph = array(
    'A' => 0,
    'B' => 1,
    'C' => 2,
    'D' => 3,
    'E' => 4,
    'F' => 5,
    'G' => 6,
    'H' => 7,
    'I' => 8,
    'J' => 9,
    'K' => 10,
    'L' => 11,
    'M' => 12,
    'N' => 13,
    'O' => 14,
    'P' => 15,
    'Q' => 16,
    'R' => 17,
    'S' => 18,
    'T' => 19,
    'U' => 20,
    'V' => 21,
    'W' => 22,
    'X' => 23,
    'Y' => 24,
    'Z' => 25,
    '0' => 26,
    '1' => 27,
    '2' => 28,
    '3' => 29,
    '4' => 30,
    '5' => 31,
    '6' => 32,
    '7' => 33,
    '8' => 34,
    '9' => 35,
);

$alph2 = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
	           'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

$n = strlen($argv[1]);
$fib = array($n, $n + $n);
for ($i = 2; $i < $n; $i++) {
    $fib[] = $fib[$i - 1] + $fib[$i - 2];
}


$kxey = '';

foreach (str_split($argv[1]) as $key => $char) {
    $e = $fib[$key] % 36;
    $e = $alph[$char] - $e;
    if ($e >= 0) {
        $kxey .= $alph2[$e];
    } else {
        $kxey .= $alph2[36 + $e];
    }
}

preg_match_all('/[AZKW]{1}[0-9]{1,2}/', $kxey, $params);

$params2 = array();
foreach ($params[0] as $param) {
    $params2[$param[0]] = (int)substr($param, 1);
}

$cols = $params2['K'];
$rows = $params2['W'];
$min = $params2['A'];
$max = $params2['Z'];

$delta = $max - $min + 1;
$seq = array();

for ($i = 0; $i < $delta; $i++) {
    $seq[] = $min++;
}

$t = str_repeat('+', $cols) . "\n";
$rect_one = str_repeat($t, $rows);
$rect_two = $rect_one;
$rect_three = $rect_one;

$horizontal = $cols - 1;
$vertical = $rows - 1;
$cols_plus = $cols + 1;

//start - third rect
$n = 0;
for ($i = $vertical; $i >= 0; $i--) {
    for ($j = $horizontal; $j >= 0; $j--) {
        $rect_three[$i * $cols_plus + $j] = $seq[$n++ % $delta];
    }
}
//end - third rect

//start - second rect
$n = 0;
for ($i = 0; $i < $cols; $i++) {
    if ($i % 2) {
        for ($j = 0; $j < $rows; $j++) {
            $rect_two[$j * $cols_plus + $i] = $seq[$n++ % $delta];
        }
    } else {
        for ($j = $vertical; $j >=0; $j--) {
            $rect_two[$j * $cols_plus + $i] = $seq[$n++ % $delta];
        }
    }
}
//end - second rect

//start - first rect
$i = 0;
$j = 0;
$r = 0;
$n = 0;
do {
    for ($i; $i < $horizontal; $i++) {
        $rect_one[$j * $cols_plus + $i] = $seq[$n++ % $delta];
    }
    for ($j; $j < $vertical; $j++) {
        $rect_one[$j * $cols_plus + $i] = $seq[$n++ % $delta];
    }
    for ($i; $i > $r; $i--) {
        $rect_one[$j * $cols_plus + $i] = $seq[$n++ % $delta];
    }
    for ($j; $j > $r; $j--) {
        $rect_one[$j * $cols_plus + $i] = $seq[$n++ % $delta];
    }
    $horizontal--;
    $vertical--;
    $i++;
    $j++;
    $r++;
} while (($horizontal > $r) && ($vertical > $r));

if (($rows % 2) && ($rows < $cols)) {
    for ($i; $i <= $horizontal; $i++) {
        $rect_one[$j * $cols_plus + $i] = $seq[$n++ % $delta];
    }
} elseif (($cols % 2) && ($rows >= $cols)) {
    for ($j; $j <= $vertical; $j++) {
        $rect_one[$j * $cols_plus + $i] = $seq[$n++ % $delta];
    }
}
//end - first rect

echo "$rect_one\n$rect_two\n$rect_three\n";
