B - Count Adjacent Cells Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。

マス (x_1, y_1) とマス (x_2, y_2)辺で隣接するとは、|x_1 - x_2| + |y_1 - y_2| = 1 が成り立つことをいいます。

すべてのマスについて、そのマスに辺で隣接するマスの個数を求めてください。

制約

  • 1 \leq H,W \leq 50
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

H W

出力

答えを以下の形式で出力せよ。

x_{1,1} x_{1,2} \cdots x_{1,W}
x_{2,1} x_{2,2} \cdots x_{2,W}
\vdots
x_{H,1} x_{H,2} \cdots x_{H,W}

ここで、x_{i,j} はマス (i,j) に辺で隣接するマスの個数を表す。


入力例 1

4 5

出力例 1

2 3 3 3 2
3 4 4 4 3
3 4 4 4 3
2 3 3 3 2

マス (1, 5) に辺で隣接するマスは、マス (1, 4), (2, 5)2 個です。

マス (2, 3) に辺で隣接するマスは、マス (1, 3), (2, 2), (2, 4), (3, 3)4 個です。

マス (4, 2) に辺で隣接するマスは、マス (3, 2), (4, 1), (4, 3)3 個です。


入力例 2

1 1

出力例 2

0

マス (1,1) に辺で隣接するマスは存在しません。


入力例 3

12 8

出力例 3

2 3 3 3 3 3 3 2
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
2 3 3 3 3 3 3 2

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is denoted as cell (i, j).

Cells (x_1, y_1) and (x_2, y_2) are said to be edge-adjacent when |x_1 - x_2| + |y_1 - y_2| = 1.

For every cell, find the number of cells that are edge-adjacent to it.

Constraints

  • 1 \leq H, W \leq 50
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

H W

Output

Output the answer in the following format:

x_{1,1} x_{1,2} \cdots x_{1,W}
x_{2,1} x_{2,2} \cdots x_{2,W}
\vdots
x_{H,1} x_{H,2} \cdots x_{H,W}

Here, x_{i,j} represents the number of cells that are edge-adjacent to cell (i, j).


Sample Input 1

4 5

Sample Output 1

2 3 3 3 2
3 4 4 4 3
3 4 4 4 3
2 3 3 3 2

The cells edge-adjacent to cell (1, 5) are cells (1, 4), (2, 5), for a total of two cells.

The cells edge-adjacent to cell (2, 3) are cells (1, 3), (2, 2), (2, 4), (3, 3), for a total of four cells.

The cells edge-adjacent to cell (4, 2) are cells (3, 2), (4, 1), (4, 3), for a total of three cells.


Sample Input 2

1 1

Sample Output 2

0

There are no cells edge-adjacent to cell (1, 1).


Sample Input 3

12 8

Sample Output 3

2 3 3 3 3 3 3 2
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
2 3 3 3 3 3 3 2