#include "testlib.h"

const int MAXT = 250'000;
const int MAXSUM = 1'000'000;

const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};

using Field = std::vector<std::string>;

Field generate(int n, int m, int x1, int y1, int x2, int y2, int mask) {
	Field a(n);
	for (int i = 0; i < n; ++i) {
		a[i].resize(m, '.');
	}

	for (int id = 0; id < n * m; ++id) {
		if (((mask >> id) & 1) == 1) {
			int i = id / m;
			int j = id % m;
			a[i][j] = '#';
		}
	}
	a[x1][y1] = 's';
	a[x2][y2] = 'f';

	return a;
}

bool check(const Field& f, int n, int m, int x1, int y1, int x2, int y2) {
	std::vector<std::vector<bool>> used(n, std::vector<bool>(m));

	auto dfs = [&f, &used, &n, &m](auto self, int x, int y) -> void {
		used[x][y] = true;

		for (int i = 0; i < 4; ++i) {
			int newX = x + dx[i];
			int newY = y + dy[i];

			if (newX < 0 || newY < 0 || newX >= n || newY >= m) {
				continue;
			}
			if (used[newX][newY] || f[newX][newY] == '#') {
				continue;
			}

			self(self, newX, newY);
		}
	};
	dfs(dfs, x1, y1);

	return used[x2][y2];
}

int main(int argc, char** argv) {
	registerGen(argc, argv, 1);

	int startNM = atoi(argv[1]);
	int startN = atoi(argv[2]);
	int startX1 = atoi(argv[3]);
	int startY1 = atoi(argv[4]);
	int startX2 = atoi(argv[5]);
	int startY2 = atoi(argv[6]);
	int startMask = atoi(argv[7]);

	int nm = startNM;
	int n = startN;
	int x1 = startX1;
	int y1 = startY1;
	int x2 = startX2;
	int y2 = startY2;
	int mask = startMask;

	int sumNM = 0;
	int t = 0;
	std::vector<Field> tests;
	bool finish = false;
	for (; ; ++nm) {
		for (; n <= nm; ++n) {
			if (nm % n != 0) {
				continue;
			}

			int m = nm / n;
			if (n > m) {
				break;
			}

			for (; x1 < n; ++x1) {
				for (; y1 < m; ++y1) {
					for (; x2 < n; ++x2) {
						for (; y2 < m; ++y2) {
							int id1 = x1 * m + y1;
							int id2 = x2 * m + y2;
							if (id1 >= id2) {
								continue;
							}

							for (; mask < 1ll << nm; ++mask) {
								if (((mask >> id1) & 1) == 1) {
									continue;
								}
								if (((mask >> id2) & 1) == 1) {
									continue;
								}

								Field f = generate(n, m, x1, y1, x2, y2, mask);
								if (!check(f, n, m, x1, y1, x2, y2)) {
									continue;
								}

								if (sumNM + nm > MAXSUM || t + 1 > MAXT) {
									finish = true;
									break;
								}
								sumNM += nm;
								++t;
								tests.push_back(f);
							}
							if (finish) {
								break;
							}
							mask = 0;
						}
						if (finish) {
							break;
						}
						y2 = 0;
					}
					if (finish) {
						break;
					}
					x2 = 0;
				}
				if (finish) {
					break;
				}
				y1 = 0;
			}
			if (finish) {
				break;
			}
			x1 = 0;
		}
		if (finish) {
			break;
		}
		n = 2;
	}

	std::cout << t << std::endl;
	for (const auto& f : tests) {
		int curN = f.size();
		int curM = f[0].size();
		std::cout << curN << ' ' << curM << std::endl;
		for (int i = 0; i < curN; ++i) {
			std::cout << f[i] << std::endl;
		}
	}

	std::cerr << nm << ' ' << n << ' ' << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << ' ' << mask << std::endl;

	return 0;
}