#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

void init()
{
  srand(301017);
}

int r( int a, int b )
{
  return rand() % (b - a + 1) + a;
}

void gen1( int n, int m )
{
  printf("%d\n", n);

  int let = r('a', 'z');

  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)
      putc(let, stdout);
    putc('\n', stdout);
  }
}

void gen2( int n, int m )
{
  printf("%d\n", n);

  int let = r('a', 'z'), let2;
  do
  {
    let2 = r('a', 'z');
  } while (let == let2);

  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)
      putc(i == n ? let : let2, stdout);
    putc('\n', stdout);
  }
}

// only letter at position ni in column is ok
string genRandCol1( int n, int ni ) 
{

  int let = r(0, 25);
  string res;

  for (int i = 0; i < n; i++)
    if (i == ni)
      res += ('a' + let);
    else
      res += ((let + r(1, 25)) % 26 + 'a');
  res += (let + 'a');
  return res;
}

// bad column
string genRandBadCol( int n ) 
{

  int let = r(0, 25);
  string res;

  for (int i = 0; i < n; i++)
      res += ((let + r(1, 25)) % 26 + 'a');
  res += (let + 'a');
  return res;
}

// bad column
string genRandOkCol( int n ) 
{

  int let = r(0, 25), len = r(1, n);
  string res;

  for (int i = 0; i < n; i++)
    if (i < len)
      res += (let + 'a');
    else
      res += ((let + r(1, 25)) % 26 + 'a');
  random_shuffle(res.begin(), res.end());
  res += (let + 'a');
  return res;
}


// only one letter in each column is ok
void gen3( int n, int m )
{
  printf("%d\n", n);
  vector <string> v;
  for (int i = 0; i < m; i++)
    v.push_back(genRandCol1(n, r(0, n - 1)));
  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)

      putc(v[j][i], stdout);
    putc('\n', stdout);
  }
}


// only one letter in each column is ok and answer is m - 1
void gen4( int n, int m )
{
  if (n < m)
  {
    fprintf(stderr, "botva\n");
    return;
  }
  printf("%d\n", n);

  vector <int> per;
  for (int i = 0; i < n; i++)
    per.push_back(i);
  random_shuffle(per.begin(), per.end());


  vector <string> v;
  for (int i = 0; i < m; i++)
  {
//    cerr << per[i] << endl;
    v.push_back(genRandCol1(n, per[i]));
//    cerr << v[v.size() - 1] << endl;
  }
  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)
      putc(v[j][i], stdout);
    putc('\n', stdout);
  }
}

// only one column is bad
void gen7( int n, int m )
{
  printf("%d\n", n);
  vector <string> v;

  int bad = r(0, m - 1);

  for (int i = 0; i < m; i++)
    if (i == bad)
      v.push_back(genRandBadCol(n));
    else
      v.push_back(genRandOkCol(n));
  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)
      putc(v[j][i], stdout);
    putc('\n', stdout);
  }
}

// rand ok
void gen5( int n, int m )
{
  printf("%d\n", n);
  vector <string> v;

  for (int i = 0; i < m; i++)
      v.push_back(genRandOkCol(n));
  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)
      putc(v[j][i], stdout);
    putc('\n', stdout);
  }
}

// rand no sol
void gen6( int n, int m )
{
  printf("%d\n", n);
  vector <string> v;

  int len = r(1, m);
  for (int i = 0; i < m; i++)
    if (i < len)
      v.push_back(genRandBadCol(n));
    else
      v.push_back(genRandOkCol(n));

  random_shuffle(v.begin(), v.end());
  
  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j < m; j++)
      putc(v[j][i], stdout);
    putc('\n', stdout);
  }
}


int main( void )
{
  init();

  freopen("tests", "w", stdout);

  // all letters are same
  gen1(100, 100); 
  gen1(100, 2); 

  // very stupid tests on no solution
  gen2(100, 100);
  gen2(2, 2);
  gen2(100, 2);
  gen2(2, 100);
  
  // only one letter in each column is ok
  gen3(15, 93);
  gen3(53, 21);
  gen3(100, 100);
  gen3(2, 2);
  gen3(2, 100);
  gen3(100, 2);

  // only one column is bad
  gen7(15, 93);
  gen7(53, 21);
  gen7(100, 100);
  gen7(2, 2);
  gen7(2, 100);
  gen7(100, 2);

  // answer is (m - 1)
  gen4(100, 100);
  gen4(87, 43);
  gen4(23, 12);
  gen4(3, 3);


  vector <int> v;
  v.push_back(1);
  v.push_back(10);
  v.push_back(50);
  v.push_back(100);

  // gen rand ok
  for (int i = 0; i + 1 < (int)v.size(); i++)
    for (int j = 0; j + 1 < (int)v.size(); j++)
      for (int t = 0; t < 4; t++)
        gen5(r(v[i] + 1, v[i + 1]), r(v[j] + 1, v[j + 1]));

  // gen rand no sol
  for (int i = 0; i + 1 < (int)v.size(); i++)
    for (int j = 0; j + 1 < (int)v.size(); j++)
      for (int t = 0; t < 4; t++)
        gen6(r(v[i] + 1, v[i + 1]), r(v[j] + 1, v[j + 1]));
  return 0;
}
