Dave's Worry
Time limit2sMemory limit1024 MB
Assign one of 5 species to each cell of an N by M grid so that no two side-adjacent cells match and each interior cell touches all four other species.
- Level
Medium7 of 10
- Topics
- Matrix, Implementation, Math
- Solved
- No attempts yet
Problem
Dave received a fish farm from Auto with the help of you all at last year's year-end contest. Over the past year he raised fish diligently and earned a lot of money. With this money, Dave greatly expanded the fish farm!
The expanded farm is a grid of square cells, in size.

[Figure 1] The newly expanded fish farm of Dave
Dave plans to raise fish of 5 species on this farm. Dave cares a great deal about the privacy of his fish, so each cell holds exactly one fish. When choosing the species for each cell, the following rules must be followed.
- Fish of the same species cannot be raised in two cells that are adjacent horizontally or vertically. Otherwise the fish would breed uncontrollably.
- A fish in a cell adjacent to all 4 cells horizontally and vertically (that is, a cell not on an edge or corner of the farm) must be adjacent to fish of all 4 species other than its own.
Dave is trying to decide which fish to raise in each cell so that the rules are followed. Find one arrangement of fish that satisfies the rules and help Dave.
Input
The first line contains two integers and , separated by a space, giving the size of the fish farm.
Output
Print lines, each containing integers separated by spaces. The -th number on the -th line is an integer from to that denotes the species of fish raised in the cell in row from the top and column from the left.