Module Module1

    Sub Main()
        Dim n = 5 : Dim m = 7
        Dim A(n - 1, m - 1) As Integer
        For i As Integer = 0 To n - 1
            For j As Integer = 0 To m - 1
                A(i, j) = i + j
            Next j
        Next i
        Dim number As Integer = Integer.Parse(Console.ReadLine())
        Dim buffer As Integer = 0
        For g As Integer = 1 To number
            Dim l As Integer : Dim k As Integer
            For i As Integer = 0 To 2 * m + 2 * n - 5
                buffer = A(l, k)
                If 0 <= l < n - 1 And k = 0 Then
                    l += 1
                ElseIf l = n - 1 And 0 <= k < m - 1 Then
                    k += 1
                ElseIf k = m - 1 And 0 < l <= n - 1 Then
                    l += -1
                ElseIf l = 0 And 0 < k <= m - 1 Then
                    k += -1
                End If
                A(l, k) = buffer
            Next i
        Next g
        For i As Integer = 0 To n - 1
            For j As Integer = 0 To m - 1
                Console.Write(A(i, j))
            Next j
            Console.WriteLine()
        Next i
        Console.ReadLine()
    End Sub

End Module
