Given the following C code and unoptimized assembly code, your task is to generate highly optimized x86-64 assembly code.
C Code:

```c
// AOJ 0558: Cheese
// 2017.10.3 bal4u@uu

#include <stdio.h>
#include <ctype.h>

#define QMAX 2000000
typedef struct { int r, c, s; } Q;
Q q[2000005]; int top, end;
char map[1002][1002]; int h, w;
int sr[10], sc[10];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int mk[1002][1002];

int bfs(int sr, int sc, int gr, int gc)
{
	int r, c, s, i, r2, c2;
	static int v = 0;

	mk[sr][sc] = ++v;
	q[0].r = sr, q[0].c = sc, q[0].s = 0, top = 0, end = 1;
	while (top < end) {
		r = q[top].r, c = q[top].c, s = q[top].s; if (++top == QMAX) top = 0;
		if (gr == r && gc == c) return s;
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w || map[r2][c2] == 'X') continue;
			if (mk[r2][c2] == v) continue;
			mk[r2][c2] = v;
			q[end].r = r2, q[end].c = c2, q[end].s = s+1; if (++end == QMAX) end = 0;
		}
	}
	return 0;
}

int main()
{
	int n, i, r, c, ans;
	char a;

	scanf("%d%d%d", &h, &w, &n);
	for (r = 0; r < h; r++) {
		scanf("%s", map[r]);
		for (c = 0; c < w; c++) {
			if (map[r][c] == 'S')
				sr[0] = r, sc[0] = c, map[r][c] = '.';
			else if (isdigit(a = map[r][c]))
				sr[a-'0'] = r, sc[a-'0'] = c, map[r][c] = '.';
		}
	}
	for (ans = 0, i = 0; i < n; i++) ans += bfs(sr[i], sc[i], sr[i+1], sc[i+1]);
	printf("%d\n", ans);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	q
	.bss
	.align 32
	.type	q, @object
	.size	q, 24000060
q:
	.zero	24000060
	.globl	top
	.align 4
	.type	top, @object
	.size	top, 4
top:
	.zero	4
	.globl	end
	.align 4
	.type	end, @object
	.size	end, 4
end:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 1004004
map:
	.zero	1004004
	.globl	h
	.align 4
	.type	h, @object
	.size	h, 4
h:
	.zero	4
	.globl	w
	.align 4
	.type	w, @object
	.size	w, 4
w:
	.zero	4
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 40
sr:
	.zero	40
	.globl	sc
	.align 32
	.type	sc, @object
	.size	sc, 40
sc:
	.zero	40
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	mk
	.bss
	.align 32
	.type	mk, @object
	.size	mk, 4016016
mk:
	.zero	4016016
	.text
	.globl	bfs
	.type	bfs, @function
bfs:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -36(%rbp)
	movl	%esi, -40(%rbp)
	movl	%edx, -44(%rbp)
	movl	%ecx, -48(%rbp)
	movl	v.0(%rip), %eax
	addl	$1, %eax
	movl	%eax, v.0(%rip)
	movl	v.0(%rip), %eax
	movl	-40(%rbp), %edx
	movslq	%edx, %rcx
	movl	-36(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$1002, %rdx, %rdx
	addq	%rcx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	mk(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	-36(%rbp), %eax
	movl	%eax, q(%rip)
	movl	-40(%rbp), %eax
	movl	%eax, 4+q(%rip)
	movl	$0, 8+q(%rip)
	movl	$0, top(%rip)
	movl	$1, end(%rip)
	jmp	.L2
.L12:
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -20(%rbp)
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	4+q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -16(%rbp)
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	8+q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -12(%rbp)
	movl	top(%rip), %eax
	addl	$1, %eax
	movl	%eax, top(%rip)
	movl	top(%rip), %eax
	cmpl	$2000000, %eax
	jne	.L3
	movl	$0, top(%rip)
.L3:
	movl	-44(%rbp), %eax
	cmpl	-20(%rbp), %eax
	jne	.L4
	movl	-48(%rbp), %eax
	cmpl	-16(%rbp), %eax
	jne	.L4
	movl	-12(%rbp), %eax
	jmp	.L5
.L4:
	movl	$0, -24(%rbp)
	jmp	.L6
.L11:
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	mv(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-20(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -8(%rbp)
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	4+mv(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-16(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	cmpl	$0, -8(%rbp)
	js	.L13
	movl	h(%rip), %eax
	cmpl	%eax, -8(%rbp)
	jge	.L13
	cmpl	$0, -4(%rbp)
	js	.L13
	movl	w(%rip), %eax
	cmpl	%eax, -4(%rbp)
	jge	.L13
	movl	-4(%rbp), %eax
	movslq	%eax, %rdx
	movl	-8(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$88, %al
	je	.L13
	movl	-4(%rbp), %eax
	movslq	%eax, %rdx
	movl	-8(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	mk(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	v.0(%rip), %eax
	cmpl	%eax, %edx
	je	.L14
	movl	v.0(%rip), %eax
	movl	-4(%rbp), %edx
	movslq	%edx, %rcx
	movl	-8(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$1002, %rdx, %rdx
	addq	%rcx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	mk(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rcx
	leaq	q(%rip), %rdx
	movl	-8(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rcx
	leaq	4+q(%rip), %rdx
	movl	-4(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movl	-12(%rbp), %edx
	leal	1(%rdx), %ecx
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	8+q(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	end(%rip), %eax
	addl	$1, %eax
	movl	%eax, end(%rip)
	movl	end(%rip), %eax
	cmpl	$2000000, %eax
	jne	.L9
	movl	$0, end(%rip)
	jmp	.L9
.L13:
	nop
	jmp	.L9
.L14:
	nop
.L9:
	addl	$1, -24(%rbp)
.L6:
	cmpl	$3, -24(%rbp)
	jle	.L11
.L2:
	movl	top(%rip), %edx
	movl	end(%rip), %eax
	cmpl	%eax, %edx
	jl	.L12
	movl	$0, %eax
.L5:
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	bfs, .-bfs
	.section	.rodata
.LC0:
	.string	"%d%d%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$32, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-28(%rbp), %rax
	movq	%rax, %rcx
	leaq	w(%rip), %rax
	movq	%rax, %rdx
	leaq	h(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -20(%rbp)
	jmp	.L16
.L21:
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	leaq	map(%rip), %rdx
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -16(%rbp)
	jmp	.L17
.L20:
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$83, %al
	jne	.L18
	movl	-20(%rbp), %eax
	movl	%eax, sr(%rip)
	movl	-16(%rbp), %eax
	movl	%eax, sc(%rip)
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	jmp	.L19
.L18:
	call	__ctype_b_loc@PLT
	movq	(%rax), %rdx
	movl	-16(%rbp), %eax
	movslq	%eax, %rcx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rcx
	leaq	map(%rip), %rax
	addq	%rcx, %rax
	movzbl	(%rax), %eax
	movb	%al, -29(%rbp)
	movsbq	-29(%rbp), %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	movzwl	(%rax), %eax
	movzwl	%ax, %eax
	andl	$2048, %eax
	testl	%eax, %eax
	je	.L19
	movsbl	-29(%rbp), %eax
	subl	$48, %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	sr(%rip), %rdx
	movl	-20(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movsbl	-29(%rbp), %eax
	subl	$48, %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	sc(%rip), %rdx
	movl	-16(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
.L19:
	addl	$1, -16(%rbp)
.L17:
	movl	w(%rip), %eax
	cmpl	%eax, -16(%rbp)
	jl	.L20
	addl	$1, -20(%rbp)
.L16:
	movl	h(%rip), %eax
	cmpl	%eax, -20(%rbp)
	jl	.L21
	movl	$0, -12(%rbp)
	movl	$0, -24(%rbp)
	jmp	.L22
.L23:
	movl	-24(%rbp), %eax
	addl	$1, %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %ecx
	movl	-24(%rbp), %eax
	addl	$1, %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rsi
	leaq	sc(%rip), %rax
	movl	(%rsi,%rax), %esi
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdi
	leaq	sr(%rip), %rax
	movl	(%rdi,%rax), %eax
	movl	%eax, %edi
	call	bfs
	addl	%eax, -12(%rbp)
	addl	$1, -24(%rbp)
.L22:
	movl	-28(%rbp), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L23
	movl	-12(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.local	v.0
	.comm	v.0,4,4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
user
	.file	"temp.c"
	.text
	.p2align 4
	.globl	bfs
	.type	bfs, @function
bfs:
.LFB25:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movd	%esi, %xmm5
	movslq	%esi, %rsi
	movd	%edi, %xmm0
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	punpckldq	%xmm5, %xmm0
	movl	w(%rip), %r14d
	movl	%edi, %eax
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	mv(%rip), %xmm8
	leaq	mk(%rip), %r13
	leaq	q(%rip), %r11
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	movq	8+mv(%rip), %xmm7
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	16+mv(%rip), %xmm6
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	v.0(%rip), %ebx
	movl	$0, 8+q(%rip)
	movq	24+mv(%rip), %xmm5
	movl	$1, end(%rip)
	leal	1(%rbx), %ebp
	movl	mv(%rip), %ebx
	movl	%edx, -28(%rsp)
	movslq	%edi, %rdx
	imulq	$1002, %rdx, %rdx
	movl	%ecx, -4(%rsp)
	movl	%ebp, v.0(%rip)
	movl	$1, top(%rip)
	addq	%rsi, %rdx
	movl	8+mv(%rip), %esi
	movb	$0, -5(%rsp)
	movb	$0, -6(%rsp)
	movl	%esi, -20(%rsp)
	movl	16+mv(%rip), %esi
	movl	%ebx, -24(%rsp)
	movl	h(%rip), %ebx
	movl	%esi, -16(%rsp)
	movl	24+mv(%rip), %esi
	movl	%ebp, 0(%r13,%rdx,4)
	movl	$1, %edx
	movl	%esi, -12(%rsp)
	movl	$1, %esi
	movq	%xmm0, q(%rip)
	.p2align 4,,10
	.p2align 3
.L2:
	movdqa	%xmm0, %xmm1
	movd	%xmm0, %ecx
	paddd	%xmm5, %xmm1
	pshufd	$0xe5, %xmm1, %xmm2
	movd	%xmm2, %edi
	movdqa	%xmm0, %xmm2
	paddd	%xmm6, %xmm2
	pshufd	$0xe5, %xmm2, %xmm3
	movd	%xmm3, %r8d
	movdqa	%xmm0, %xmm3
	paddd	%xmm7, %xmm3
	pshufd	$0xe5, %xmm3, %xmm4
	movd	%xmm4, %r9d
	movdqa	%xmm8, %xmm4
	paddd	%xmm0, %xmm4
	pshufd	$0xe5, %xmm4, %xmm9
	movd	%xmm9, %r10d
	cmpl	-28(%rsp), %ecx
	jne	.L4
	pshufd	$0xe5, %xmm0, %xmm0
	movd	%xmm0, %ecx
	cmpl	-4(%rsp), %ecx
	je	.L40
.L4:
	movl	-24(%rsp), %ecx
	addl	%eax, %ecx
	movl	%ecx, %r15d
	orl	%r10d, %r15d
	js	.L8
	cmpl	%ebx, %ecx
	jge	.L8
	cmpl	%r10d, %r14d
	jle	.L8
	movslq	%ecx, %rcx
	leaq	map(%rip), %r15
	movslq	%r10d, %r10
	imulq	$1002, %rcx, %rcx
	addq	%rcx, %r15
	cmpb	$88, (%r15,%r10)
	je	.L8
	addq	%r10, %rcx
	cmpl	0(%r13,%rcx,4), %ebp
	je	.L8
	movl	%ebp, 0(%r13,%rcx,4)
	movslq	%esi, %rcx
	leal	1(%r12), %r10d
	addl	$1, %esi
	leaq	(%rcx,%rcx,2), %rcx
	movb	$1, -5(%rsp)
	movl	%r10d, 8(%r11,%rcx,4)
	movq	%xmm4, (%r11,%rcx,4)
	cmpl	$2000000, %esi
	jne	.L8
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L8:
	movl	-20(%rsp), %ecx
	addl	%eax, %ecx
	movl	%ecx, %r10d
	orl	%r9d, %r10d
	js	.L9
	cmpl	%ecx, %ebx
	jle	.L9
	cmpl	%r9d, %r14d
	jle	.L9
	movslq	%ecx, %rcx
	leaq	map(%rip), %r10
	movslq	%r9d, %r9
	imulq	$1002, %rcx, %rcx
	addq	%rcx, %r10
	cmpb	$88, (%r10,%r9)
	je	.L9
	addq	%r9, %rcx
	cmpl	0(%r13,%rcx,4), %ebp
	je	.L9
	movl	%ebp, 0(%r13,%rcx,4)
	movslq	%esi, %rcx
	leal	1(%r12), %r9d
	addl	$1, %esi
	leaq	(%rcx,%rcx,2), %rcx
	movb	$1, -5(%rsp)
	movl	%r9d, 8(%r11,%rcx,4)
	movq	%xmm3, (%r11,%rcx,4)
	cmpl	$2000000, %esi
	jne	.L9
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L9:
	movl	-16(%rsp), %ecx
	addl	%eax, %ecx
	movl	%ecx, %r9d
	orl	%r8d, %r9d
	js	.L10
	cmpl	%ecx, %ebx
	jle	.L10
	cmpl	%r8d, %r14d
	jle	.L10
	movslq	%ecx, %rcx
	leaq	map(%rip), %r9
	movslq	%r8d, %r8
	imulq	$1002, %rcx, %rcx
	addq	%rcx, %r9
	cmpb	$88, (%r9,%r8)
	je	.L10
	addq	%r8, %rcx
	cmpl	0(%r13,%rcx,4), %ebp
	je	.L10
	movl	%ebp, 0(%r13,%rcx,4)
	movslq	%esi, %rcx
	leal	1(%r12), %r8d
	addl	$1, %esi
	leaq	(%rcx,%rcx,2), %rcx
	movb	$1, -5(%rsp)
	movl	%r8d, 8(%r11,%rcx,4)
	movq	%xmm2, (%r11,%rcx,4)
	cmpl	$2000000, %esi
	jne	.L10
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L10:
	addl	-12(%rsp), %eax
	movl	%eax, %ecx
	orl	%edi, %ecx
	js	.L11
	cmpl	%eax, %ebx
	jle	.L11
	cmpl	%edi, %r14d
	jle	.L11
	cltq
	leaq	map(%rip), %rcx
	movslq	%edi, %rdi
	imulq	$1002, %rax, %rax
	addq	%rax, %rcx
	cmpb	$88, (%rcx,%rdi)
	je	.L11
	addq	%rdi, %rax
	cmpl	0(%r13,%rax,4), %ebp
	je	.L11
	movl	%ebp, 0(%r13,%rax,4)
	movslq	%esi, %rax
	addl	$1, %r12d
	addl	$1, %esi
	leaq	(%rax,%rax,2), %rax
	movb	$1, -5(%rsp)
	movl	%r12d, 8(%r11,%rax,4)
	movq	%xmm1, (%r11,%rax,4)
	cmpl	$2000000, %esi
	jne	.L11
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L11:
	cmpl	%esi, %edx
	jge	.L41
	movslq	%edx, %rax
	addl	$1, %edx
	xorl	%edi, %edi
	movb	$1, -6(%rsp)
	leaq	(%rax,%rax,2), %rax
	cmpl	$2000000, %edx
	leaq	(%r11,%rax,4), %rcx
	movq	(%r11,%rax,4), %xmm0
	cmove	%edi, %edx
	movl	(%rcx), %eax
	movl	8(%rcx), %r12d
	jmp	.L2
	.p2align 4,,10
	.p2align 3
.L40:
	cmpb	$0, -5(%rsp)
	je	.L5
	movl	%esi, end(%rip)
.L5:
	cmpb	$0, -6(%rsp)
	je	.L1
	movl	%edx, top(%rip)
.L1:
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	movl	%r12d, %eax
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L41:
	.cfi_restore_state
	cmpb	$0, -5(%rsp)
	je	.L13
	movl	%esi, end(%rip)
.L13:
	cmpb	$0, -6(%rsp)
	je	.L14
	movl	%edx, top(%rip)
.L14:
	xorl	%r12d, %r12d
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	movl	%r12d, %eax
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE25:
	.size	bfs, .-bfs
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d%d%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB26:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	w(%rip), %rdx
	leaq	h(%rip), %rsi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	leaq	.LC0(%rip), %rdi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	20(%rsp), %rcx
	call	__isoc99_scanf@PLT
	movl	h(%rip), %edx
	testl	%edx, %edx
	jle	.L43
	leaq	map(%rip), %rax
	xorl	%ebp, %ebp
	leaq	sr(%rip), %r13
	movq	%rax, 8(%rsp)
	.p2align 4,,10
	.p2align 3
.L48:
	movq	8(%rsp), %r14
	leaq	.LC1(%rip), %rdi
	xorl	%eax, %eax
	movq	%r14, %rsi
	call	__isoc99_scanf@PLT
	movl	w(%rip), %ebx
	testl	%ebx, %ebx
	jle	.L44
	xorl	%r15d, %r15d
	jmp	.L47
	.p2align 4,,10
	.p2align 3
.L45:
	call	__ctype_b_loc@PLT
	movq	%rax, %r8
	movsbq	%r12b, %rax
	movq	(%r8), %rdi
	testb	$8, 1(%rdi,%rax,2)
	je	.L46
	movsbl	%r12b, %edx
	leaq	sc(%rip), %rax
	movb	$46, (%r14)
	subl	$48, %edx
	movslq	%edx, %rdx
	movl	%ebp, 0(%r13,%rdx,4)
	movl	%r15d, (%rax,%rdx,4)
.L46:
	addl	$1, %r15d
	addq	$1, %r14
	cmpl	%ebx, %r15d
	je	.L44
.L47:
	movzbl	(%r14), %r12d
	cmpb	$83, %r12b
	jne	.L45
	leaq	sc(%rip), %rax
	movb	$46, (%r14)
	addq	$1, %r14
	movl	%r15d, (%rax)
	addl	$1, %r15d
	movl	%ebp, 0(%r13)
	cmpl	%ebx, %r15d
	jne	.L47
.L44:
	addq	$1002, 8(%rsp)
	addl	$1, %ebp
	cmpl	%ebp, h(%rip)
	jg	.L48
.L43:
	movl	20(%rsp), %eax
	testl	%eax, %eax
	jle	.L52
	leaq	sr(%rip), %r15
	leaq	sc(%rip), %r14
	xorl	%ebx, %ebx
	xorl	%r12d, %r12d
	leaq	4(%r14), %r13
	leaq	4(%r15), %rbp
	.p2align 4,,10
	.p2align 3
.L50:
	movl	0(%r13,%rbx,4), %ecx
	movl	0(%rbp,%rbx,4), %edx
	movl	(%r14,%rbx,4), %esi
	movl	(%r15,%rbx,4), %edi
	addq	$1, %rbx
	call	bfs
	addl	%eax, %r12d
	cmpl	%ebx, 20(%rsp)
	jg	.L50
.L49:
	xorl	%eax, %eax
	movl	%r12d, %edx
	movl	$1, %edi
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L60
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
.L52:
	.cfi_restore_state
	xorl	%r12d, %r12d
	jmp	.L49
.L60:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE26:
	.size	main, .-main
	.local	v.0
	.comm	v.0,4,4
	.globl	mk
	.bss
	.align 32
	.type	mk, @object
	.size	mk, 4016016
mk:
	.zero	4016016
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	sc
	.bss
	.align 32
	.type	sc, @object
	.size	sc, 40
sc:
	.zero	40
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 40
sr:
	.zero	40
	.globl	w
	.align 4
	.type	w, @object
	.size	w, 4
w:
	.zero	4
	.globl	h
	.align 4
	.type	h, @object
	.size	h, 4
h:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 1004004
map:
	.zero	1004004
	.globl	end
	.align 4
	.type	end, @object
	.size	end, 4
end:
	.zero	4
	.globl	top
	.align 4
	.type	top, @object
	.size	top, 4
top:
	.zero	4
	.globl	q
	.align 32
	.type	q, @object
	.size	q, 24000060
q:
	.zero	24000060
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

rule
	.file	"temp.c"
	.text
	.p2align 4
	.globl	bfs
	.type	bfs, @function
bfs:
.LFB25:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movd	%esi, %xmm5
	movslq	%esi, %rsi
	movd	%edi, %xmm0
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	punpckldq	%xmm5, %xmm0
	movl	w(%rip), %r14d
	movl	%edi, %eax
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	mv(%rip), %xmm8
	leaq	mk(%rip), %r13
	leaq	q(%rip), %r11
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	movq	8+mv(%rip), %xmm7
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	16+mv(%rip), %xmm6
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	v.0(%rip), %ebx
	movl	$0, 8+q(%rip)
	movq	24+mv(%rip), %xmm5
	movl	$1, end(%rip)
	leal	1(%rbx), %ebp
	movl	mv(%rip), %ebx
	movl	%edx, -28(%rsp)
	movslq	%edi, %rdx
	imulq	$1002, %rdx, %rdx
	movl	%ecx, -4(%rsp)
	movl	%ebp, v.0(%rip)
	movl	$1, top(%rip)
	addq	%rsi, %rdx
	movl	8+mv(%rip), %esi
	movb	$0, -5(%rsp)
	movb	$0, -6(%rsp)
	movl	%esi, -20(%rsp)
	movl	16+mv(%rip), %esi
	movl	%ebx, -24(%rsp)
	movl	h(%rip), %ebx
	movl	%esi, -16(%rsp)
	movl	24+mv(%rip), %esi
	movl	%ebp, 0(%r13,%rdx,4)
	movl	$1, %edx
	movl	%esi, -12(%rsp)
	movl	$1, %esi
	movq	%xmm0, q(%rip)
	.p2align 4,,10
	.p2align 3
.L2:
	movdqa	%xmm0, %xmm1
	movd	%xmm0, %ecx
	paddd	%xmm5, %xmm1
	pshufd	$0xe5, %xmm1, %xmm2
	movd	%xmm2, %edi
	movdqa	%xmm0, %xmm2
	paddd	%xmm6, %xmm2
	pshufd	$0xe5, %xmm2, %xmm3
	movd	%xmm3, %r8d
	movdqa	%xmm0, %xmm3
	paddd	%xmm7, %xmm3
	pshufd	$0xe5, %xmm3, %xmm4
	movd	%xmm4, %r9d
	movdqa	%xmm8, %xmm4
	paddd	%xmm0, %xmm4
	pshufd	$0xe5, %xmm4, %xmm9
	movd	%xmm9, %r10d
	cmpl	-28(%rsp), %ecx
	jne	.L4
	pshufd	$0xe5, %xmm0, %xmm0
	movd	%xmm0, %ecx
	cmpl	-4(%rsp), %ecx
	je	.L40
.L4:
	movl	-24(%rsp), %ecx
	addl	%eax, %ecx
	movl	%ecx, %r15d
	orl	%r10d, %r15d
	js	.L8
	cmpl	%ebx, %ecx
	jge	.L8
	cmpl	%r10d, %r14d
	jle	.L8
	movslq	%ecx, %rcx
	leaq	map(%rip), %r15
	movslq	%r10d, %r10
	imulq	$1002, %rcx, %rcx
	addq	%rcx, %r15
	cmpb	$88, (%r15,%r10)
	je	.L8
	addq	%r10, %rcx
	cmpl	0(%r13,%rcx,4), %ebp
	je	.L8
	movl	%ebp, 0(%r13,%rcx,4)
	movslq	%esi, %rcx
	leal	1(%r12), %r10d
	addl	$1, %esi
	leaq	(%rcx,%rcx,2), %rcx
	movb	$1, -5(%rsp)
	movl	%r10d, 8(%r11,%rcx,4)
	movq	%xmm4, (%r11,%rcx,4)
	cmpl	$2000000, %esi
	jne	.L8
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L8:
	movl	-20(%rsp), %ecx
	addl	%eax, %ecx
	movl	%ecx, %r10d
	orl	%r9d, %r10d
	js	.L9
	cmpl	%ecx, %ebx
	jle	.L9
	cmpl	%r9d, %r14d
	jle	.L9
	movslq	%ecx, %rcx
	leaq	map(%rip), %r10
	movslq	%r9d, %r9
	imulq	$1002, %rcx, %rcx
	addq	%rcx, %r10
	cmpb	$88, (%r10,%r9)
	je	.L9
	addq	%r9, %rcx
	cmpl	0(%r13,%rcx,4), %ebp
	je	.L9
	movl	%ebp, 0(%r13,%rcx,4)
	movslq	%esi, %rcx
	leal	1(%r12), %r9d
	addl	$1, %esi
	leaq	(%rcx,%rcx,2), %rcx
	movb	$1, -5(%rsp)
	movl	%r9d, 8(%r11,%rcx,4)
	movq	%xmm3, (%r11,%rcx,4)
	cmpl	$2000000, %esi
	jne	.L9
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L9:
	movl	-16(%rsp), %ecx
	addl	%eax, %ecx
	movl	%ecx, %r9d
	orl	%r8d, %r9d
	js	.L10
	cmpl	%ecx, %ebx
	jle	.L10
	cmpl	%r8d, %r14d
	jle	.L10
	movslq	%ecx, %rcx
	leaq	map(%rip), %r9
	movslq	%r8d, %r8
	imulq	$1002, %rcx, %rcx
	addq	%rcx, %r9
	cmpb	$88, (%r9,%r8)
	je	.L10
	addq	%r8, %rcx
	cmpl	0(%r13,%rcx,4), %ebp
	je	.L10
	movl	%ebp, 0(%r13,%rcx,4)
	movslq	%esi, %rcx
	leal	1(%r12), %r8d
	addl	$1, %esi
	leaq	(%rcx,%rcx,2), %rcx
	movb	$1, -5(%rsp)
	movl	%r8d, 8(%r11,%rcx,4)
	movq	%xmm2, (%r11,%rcx,4)
	cmpl	$2000000, %esi
	jne	.L10
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L10:
	addl	-12(%rsp), %eax
	movl	%eax, %ecx
	orl	%edi, %ecx
	js	.L11
	cmpl	%eax, %ebx
	jle	.L11
	cmpl	%edi, %r14d
	jle	.L11
	cltq
	leaq	map(%rip), %rcx
	movslq	%edi, %rdi
	imulq	$1002, %rax, %rax
	addq	%rax, %rcx
	cmpb	$88, (%rcx,%rdi)
	je	.L11
	addq	%rdi, %rax
	cmpl	0(%r13,%rax,4), %ebp
	je	.L11
	movl	%ebp, 0(%r13,%rax,4)
	movslq	%esi, %rax
	addl	$1, %r12d
	addl	$1, %esi
	leaq	(%rax,%rax,2), %rax
	movb	$1, -5(%rsp)
	movl	%r12d, 8(%r11,%rax,4)
	movq	%xmm1, (%r11,%rax,4)
	cmpl	$2000000, %esi
	jne	.L11
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L11:
	cmpl	%esi, %edx
	jge	.L41
	movslq	%edx, %rax
	addl	$1, %edx
	xorl	%edi, %edi
	movb	$1, -6(%rsp)
	leaq	(%rax,%rax,2), %rax
	cmpl	$2000000, %edx
	leaq	(%r11,%rax,4), %rcx
	movq	(%r11,%rax,4), %xmm0
	cmove	%edi, %edx
	movl	(%rcx), %eax
	movl	8(%rcx), %r12d
	jmp	.L2
	.p2align 4,,10
	.p2align 3
.L40:
	cmpb	$0, -5(%rsp)
	je	.L5
	movl	%esi, end(%rip)
.L5:
	cmpb	$0, -6(%rsp)
	je	.L1
	movl	%edx, top(%rip)
.L1:
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	movl	%r12d, %eax
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L41:
	.cfi_restore_state
	cmpb	$0, -5(%rsp)
	je	.L13
	movl	%esi, end(%rip)
.L13:
	cmpb	$0, -6(%rsp)
	je	.L14
	movl	%edx, top(%rip)
.L14:
	xorl	%r12d, %r12d
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	movl	%r12d, %eax
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE25:
	.size	bfs, .-bfs
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d%d%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB26:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	w(%rip), %rdx
	leaq	h(%rip), %rsi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	leaq	.LC0(%rip), %rdi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	20(%rsp), %rcx
	call	__isoc99_scanf@PLT
	movl	h(%rip), %edx
	testl	%edx, %edx
	jle	.L43
	leaq	map(%rip), %rax
	xorl	%ebp, %ebp
	leaq	sr(%rip), %r13
	movq	%rax, 8(%rsp)
	.p2align 4,,10
	.p2align 3
.L48:
	movq	8(%rsp), %r14
	leaq	.LC1(%rip), %rdi
	xorl	%eax, %eax
	movq	%r14, %rsi
	call	__isoc99_scanf@PLT
	movl	w(%rip), %ebx
	testl	%ebx, %ebx
	jle	.L44
	xorl	%r15d, %r15d
	jmp	.L47
	.p2align 4,,10
	.p2align 3
.L45:
	call	__ctype_b_loc@PLT
	movq	%rax, %r8
	movsbq	%r12b, %rax
	movq	(%r8), %rdi
	testb	$8, 1(%rdi,%rax,2)
	je	.L46
	movsbl	%r12b, %edx
	leaq	sc(%rip), %rax
	movb	$46, (%r14)
	subl	$48, %edx
	movslq	%edx, %rdx
	movl	%ebp, 0(%r13,%rdx,4)
	movl	%r15d, (%rax,%rdx,4)
.L46:
	addl	$1, %r15d
	addq	$1, %r14
	cmpl	%ebx, %r15d
	je	.L44
.L47:
	movzbl	(%r14), %r12d
	cmpb	$83, %r12b
	jne	.L45
	leaq	sc(%rip), %rax
	movb	$46, (%r14)
	addq	$1, %r14
	movl	%r15d, (%rax)
	addl	$1, %r15d
	movl	%ebp, 0(%r13)
	cmpl	%ebx, %r15d
	jne	.L47
.L44:
	addq	$1002, 8(%rsp)
	addl	$1, %ebp
	cmpl	%ebp, h(%rip)
	jg	.L48
.L43:
	movl	20(%rsp), %eax
	testl	%eax, %eax
	jle	.L52
	leaq	sr(%rip), %r15
	leaq	sc(%rip), %r14
	xorl	%ebx, %ebx
	xorl	%r12d, %r12d
	leaq	4(%r14), %r13
	leaq	4(%r15), %rbp
	.p2align 4,,10
	.p2align 3
.L50:
	movl	0(%r13,%rbx,4), %ecx
	movl	0(%rbp,%rbx,4), %edx
	movl	(%r14,%rbx,4), %esi
	movl	(%r15,%rbx,4), %edi
	addq	$1, %rbx
	call	bfs
	addl	%eax, %r12d
	cmpl	%ebx, 20(%rsp)
	jg	.L50
.L49:
	xorl	%eax, %eax
	movl	%r12d, %edx
	movl	$1, %edi
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L60
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
.L52:
	.cfi_restore_state
	xorl	%r12d, %r12d
	jmp	.L49
.L60:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE26:
	.size	main, .-main
	.local	v.0
	.comm	v.0,4,4
	.globl	mk
	.bss
	.align 32
	.type	mk, @object
	.size	mk, 4016016
mk:
	.zero	4016016
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	sc
	.bss
	.align 32
	.type	sc, @object
	.size	sc, 40
sc:
	.zero	40
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 40
sr:
	.zero	40
	.globl	w
	.align 4
	.type	w, @object
	.size	w, 4
w:
	.zero	4
	.globl	h
	.align 4
	.type	h, @object
	.size	h, 4
h:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 1004004
map:
	.zero	1004004
	.globl	end
	.align 4
	.type	end, @object
	.size	end, 4
end:
	.zero	4
	.globl	top
	.align 4
	.type	top, @object
	.size	top, 4
top:
	.zero	4
	.globl	q
	.align 32
	.type	q, @object
	.size	q, 24000060
q:
	.zero	24000060
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

// AOJ 0558: Cheese
// 2017.10.3 bal4u@uu

#include <stdio.h>
#include <ctype.h>

#define QMAX 2000000
typedef struct { int r, c, s; } Q;
Q q[2000005]; int top, end;
char map[1002][1002]; int h, w;
int sr[10], sc[10];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int mk[1002][1002];

int bfs(int sr, int sc, int gr, int gc)
{
	int r, c, s, i, r2, c2;
	static int v = 0;

	mk[sr][sc] = ++v;
	q[0].r = sr, q[0].c = sc, q[0].s = 0, top = 0, end = 1;
	while (top < end) {
		r = q[top].r, c = q[top].c, s = q[top].s; if (++top == QMAX) top = 0;
		if (gr == r && gc == c) return s;
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w || map[r2][c2] == 'X') continue;
			if (mk[r2][c2] == v) continue;
			mk[r2][c2] = v;
			q[end].r = r2, q[end].c = c2, q[end].s = s+1; if (++end == QMAX) end = 0;
		}
	}
	return 0;
}

int main()
{
	int n, i, r, c, ans;
	char a;

	scanf("%d%d%d", &h, &w, &n);
	for (r = 0; r < h; r++) {
		scanf("%s", map[r]);
		for (c = 0; c < w; c++) {
			if (map[r][c] == 'S')
				sr[0] = r, sc[0] = c, map[r][c] = '.';
			else if (isdigit(a = map[r][c]))
				sr[a-'0'] = r, sc[a-'0'] = c, map[r][c] = '.';
		}
	}
	for (ans = 0, i = 0; i < n; i++) ans += bfs(sr[i], sc[i], sr[i+1], sc[i+1]);
	printf("%d\n", ans);
}
3 3 1
S..
...
..1

4

Given the following C code and unoptimized assembly code, your task is to generate highly optimized x86-64 assembly code.
C Code:

```c
// AOJ 0558: Cheese
// 2017.10.3 bal4u@uu

#include <stdio.h>
#include <ctype.h>

#define QMAX 2000000
typedef struct { int r, c, s; } Q;
Q q[2000005]; int top, end;
char map[1002][1002]; int h, w;
int sr[10], sc[10];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int mk[1002][1002];

int bfs(int sr, int sc, int gr, int gc)
{
	int r, c, s, i, r2, c2;
	static int v = 0;

	mk[sr][sc] = ++v;
	q[0].r = sr, q[0].c = sc, q[0].s = 0, top = 0, end = 1;
	while (top < end) {
		r = q[top].r, c = q[top].c, s = q[top].s; if (++top == QMAX) top = 0;
		if (gr == r && gc == c) return s;
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w || map[r2][c2] == 'X') continue;
			if (mk[r2][c2] == v) continue;
			mk[r2][c2] = v;
			q[end].r = r2, q[end].c = c2, q[end].s = s+1; if (++end == QMAX) end = 0;
		}
	}
	return 0;
}

int main()
{
	int n, i, r, c, ans;
	char a;

	scanf("%d%d%d", &h, &w, &n);
	for (r = 0; r < h; r++) {
		scanf("%s", map[r]);
		for (c = 0; c < w; c++) {
			if (map[r][c] == 'S')
				sr[0] = r, sc[0] = c, map[r][c] = '.';
			else if (isdigit(a = map[r][c]))
				sr[a-'0'] = r, sc[a-'0'] = c, map[r][c] = '.';
		}
	}
	for (ans = 0, i = 0; i < n; i++) ans += bfs(sr[i], sc[i], sr[i+1], sc[i+1]);
	printf("%d\n", ans);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	q
	.bss
	.align 32
	.type	q, @object
	.size	q, 24000060
q:
	.zero	24000060
	.globl	top
	.align 4
	.type	top, @object
	.size	top, 4
top:
	.zero	4
	.globl	end
	.align 4
	.type	end, @object
	.size	end, 4
end:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 1004004
map:
	.zero	1004004
	.globl	h
	.align 4
	.type	h, @object
	.size	h, 4
h:
	.zero	4
	.globl	w
	.align 4
	.type	w, @object
	.size	w, 4
w:
	.zero	4
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 40
sr:
	.zero	40
	.globl	sc
	.align 32
	.type	sc, @object
	.size	sc, 40
sc:
	.zero	40
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	mk
	.bss
	.align 32
	.type	mk, @object
	.size	mk, 4016016
mk:
	.zero	4016016
	.text
	.globl	bfs
	.type	bfs, @function
bfs:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -36(%rbp)
	movl	%esi, -40(%rbp)
	movl	%edx, -44(%rbp)
	movl	%ecx, -48(%rbp)
	movl	v.0(%rip), %eax
	addl	$1, %eax
	movl	%eax, v.0(%rip)
	movl	v.0(%rip), %eax
	movl	-40(%rbp), %edx
	movslq	%edx, %rcx
	movl	-36(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$1002, %rdx, %rdx
	addq	%rcx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	mk(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	-36(%rbp), %eax
	movl	%eax, q(%rip)
	movl	-40(%rbp), %eax
	movl	%eax, 4+q(%rip)
	movl	$0, 8+q(%rip)
	movl	$0, top(%rip)
	movl	$1, end(%rip)
	jmp	.L2
.L12:
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -20(%rbp)
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	4+q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -16(%rbp)
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	8+q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -12(%rbp)
	movl	top(%rip), %eax
	addl	$1, %eax
	movl	%eax, top(%rip)
	movl	top(%rip), %eax
	cmpl	$2000000, %eax
	jne	.L3
	movl	$0, top(%rip)
.L3:
	movl	-44(%rbp), %eax
	cmpl	-20(%rbp), %eax
	jne	.L4
	movl	-48(%rbp), %eax
	cmpl	-16(%rbp), %eax
	jne	.L4
	movl	-12(%rbp), %eax
	jmp	.L5
.L4:
	movl	$0, -24(%rbp)
	jmp	.L6
.L11:
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	mv(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-20(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -8(%rbp)
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	4+mv(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-16(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	cmpl	$0, -8(%rbp)
	js	.L13
	movl	h(%rip), %eax
	cmpl	%eax, -8(%rbp)
	jge	.L13
	cmpl	$0, -4(%rbp)
	js	.L13
	movl	w(%rip), %eax
	cmpl	%eax, -4(%rbp)
	jge	.L13
	movl	-4(%rbp), %eax
	movslq	%eax, %rdx
	movl	-8(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$88, %al
	je	.L13
	movl	-4(%rbp), %eax
	movslq	%eax, %rdx
	movl	-8(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	mk(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	v.0(%rip), %eax
	cmpl	%eax, %edx
	je	.L14
	movl	v.0(%rip), %eax
	movl	-4(%rbp), %edx
	movslq	%edx, %rcx
	movl	-8(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$1002, %rdx, %rdx
	addq	%rcx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	mk(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rcx
	leaq	q(%rip), %rdx
	movl	-8(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rcx
	leaq	4+q(%rip), %rdx
	movl	-4(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movl	-12(%rbp), %edx
	leal	1(%rdx), %ecx
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	8+q(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	end(%rip), %eax
	addl	$1, %eax
	movl	%eax, end(%rip)
	movl	end(%rip), %eax
	cmpl	$2000000, %eax
	jne	.L9
	movl	$0, end(%rip)
	jmp	.L9
.L13:
	nop
	jmp	.L9
.L14:
	nop
.L9:
	addl	$1, -24(%rbp)
.L6:
	cmpl	$3, -24(%rbp)
	jle	.L11
.L2:
	movl	top(%rip), %edx
	movl	end(%rip), %eax
	cmpl	%eax, %edx
	jl	.L12
	movl	$0, %eax
.L5:
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	bfs, .-bfs
	.section	.rodata
.LC0:
	.string	"%d%d%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$32, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-28(%rbp), %rax
	movq	%rax, %rcx
	leaq	w(%rip), %rax
	movq	%rax, %rdx
	leaq	h(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -20(%rbp)
	jmp	.L16
.L21:
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	leaq	map(%rip), %rdx
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -16(%rbp)
	jmp	.L17
.L20:
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$83, %al
	jne	.L18
	movl	-20(%rbp), %eax
	movl	%eax, sr(%rip)
	movl	-16(%rbp), %eax
	movl	%eax, sc(%rip)
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	jmp	.L19
.L18:
	call	__ctype_b_loc@PLT
	movq	(%rax), %rdx
	movl	-16(%rbp), %eax
	movslq	%eax, %rcx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rcx
	leaq	map(%rip), %rax
	addq	%rcx, %rax
	movzbl	(%rax), %eax
	movb	%al, -29(%rbp)
	movsbq	-29(%rbp), %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	movzwl	(%rax), %eax
	movzwl	%ax, %eax
	andl	$2048, %eax
	testl	%eax, %eax
	je	.L19
	movsbl	-29(%rbp), %eax
	subl	$48, %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	sr(%rip), %rdx
	movl	-20(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movsbl	-29(%rbp), %eax
	subl	$48, %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	sc(%rip), %rdx
	movl	-16(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
.L19:
	addl	$1, -16(%rbp)
.L17:
	movl	w(%rip), %eax
	cmpl	%eax, -16(%rbp)
	jl	.L20
	addl	$1, -20(%rbp)
.L16:
	movl	h(%rip), %eax
	cmpl	%eax, -20(%rbp)
	jl	.L21
	movl	$0, -12(%rbp)
	movl	$0, -24(%rbp)
	jmp	.L22
.L23:
	movl	-24(%rbp), %eax
	addl	$1, %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %ecx
	movl	-24(%rbp), %eax
	addl	$1, %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rsi
	leaq	sc(%rip), %rax
	movl	(%rsi,%rax), %esi
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdi
	leaq	sr(%rip), %rax
	movl	(%rdi,%rax), %eax
	movl	%eax, %edi
	call	bfs
	addl	%eax, -12(%rbp)
	addl	$1, -24(%rbp)
.L22:
	movl	-28(%rbp), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L23
	movl	-12(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.local	v.0
	.comm	v.0,4,4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
train
	.file	"temp.c"
	.text
	.globl	q
	.bss
	.align 32
	.type	q, @object
	.size	q, 24000060
q:
	.zero	24000060
	.globl	top
	.align 4
	.type	top, @object
	.size	top, 4
top:
	.zero	4
	.globl	end
	.align 4
	.type	end, @object
	.size	end, 4
end:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 1004004
map:
	.zero	1004004
	.globl	h
	.align 4
	.type	h, @object
	.size	h, 4
h:
	.zero	4
	.globl	w
	.align 4
	.type	w, @object
	.size	w, 4
w:
	.zero	4
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 40
sr:
	.zero	40
	.globl	sc
	.align 32
	.type	sc, @object
	.size	sc, 40
sc:
	.zero	40
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	mk
	.bss
	.align 32
	.type	mk, @object
	.size	mk, 4016016
mk:
	.zero	4016016
	.text
	.globl	bfs
	.type	bfs, @function
bfs:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -36(%rbp)
	movl	%esi, -40(%rbp)
	movl	%edx, -44(%rbp)
	movl	%ecx, -48(%rbp)
	movl	v.0(%rip), %eax
	addl	$1, %eax
	movl	%eax, v.0(%rip)
	movl	v.0(%rip), %eax
	movl	-40(%rbp), %edx
	movslq	%edx, %rcx
	movl	-36(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$1002, %rdx, %rdx
	addq	%rcx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	mk(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	-36(%rbp), %eax
	movl	%eax, q(%rip)
	movl	-40(%rbp), %eax
	movl	%eax, 4+q(%rip)
	movl	$0, 8+q(%rip)
	movl	$0, top(%rip)
	movl	$1, end(%rip)
	jmp	.L2
.L12:
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -20(%rbp)
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	4+q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -16(%rbp)
	movl	top(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	8+q(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -12(%rbp)
	movl	top(%rip), %eax
	addl	$1, %eax
	movl	%eax, top(%rip)
	movl	top(%rip), %eax
	cmpl	$2000000, %eax
	jne	.L3
	movl	$0, top(%rip)
.L3:
	movl	-44(%rbp), %eax
	cmpl	-20(%rbp), %eax
	jne	.L4
	movl	-48(%rbp), %eax
	cmpl	-16(%rbp), %eax
	jne	.L4
	movl	-12(%rbp), %eax
	jmp	.L5
.L4:
	movl	$0, -24(%rbp)
	jmp	.L6
.L11:
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	mv(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-20(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -8(%rbp)
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	4+mv(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-16(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	cmpl	$0, -8(%rbp)
	js	.L13
	movl	h(%rip), %eax
	cmpl	%eax, -8(%rbp)
	jge	.L13
	cmpl	$0, -4(%rbp)
	js	.L13
	movl	w(%rip), %eax
	cmpl	%eax, -4(%rbp)
	jge	.L13
	movl	-4(%rbp), %eax
	movslq	%eax, %rdx
	movl	-8(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$88, %al
	je	.L13
	movl	-4(%rbp), %eax
	movslq	%eax, %rdx
	movl	-8(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	mk(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	v.0(%rip), %eax
	cmpl	%eax, %edx
	je	.L14
	movl	v.0(%rip), %eax
	movl	-4(%rbp), %edx
	movslq	%edx, %rcx
	movl	-8(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$1002, %rdx, %rdx
	addq	%rcx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	mk(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rcx
	leaq	q(%rip), %rdx
	movl	-8(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rcx
	leaq	4+q(%rip), %rdx
	movl	-4(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	end(%rip), %eax
	movl	-12(%rbp), %edx
	leal	1(%rdx), %ecx
	movslq	%eax, %rdx
	movq	%rdx, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	salq	$2, %rax
	movq	%rax, %rdx
	leaq	8+q(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	end(%rip), %eax
	addl	$1, %eax
	movl	%eax, end(%rip)
	movl	end(%rip), %eax
	cmpl	$2000000, %eax
	jne	.L9
	movl	$0, end(%rip)
	jmp	.L9
.L13:
	nop
	jmp	.L9
.L14:
	nop
.L9:
	addl	$1, -24(%rbp)
.L6:
	cmpl	$3, -24(%rbp)
	jle	.L11
.L2:
	movl	top(%rip), %edx
	movl	end(%rip), %eax
	cmpl	%eax, %edx
	jl	.L12
	movl	$0, %eax
.L5:
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	bfs, .-bfs
	.section	.rodata
.LC0:
	.string	"%d%d%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$32, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-28(%rbp), %rax
	movq	%rax, %rcx
	leaq	w(%rip), %rax
	movq	%rax, %rdx
	leaq	h(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -20(%rbp)
	jmp	.L16
.L21:
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	leaq	map(%rip), %rdx
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -16(%rbp)
	jmp	.L17
.L20:
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$83, %al
	jne	.L18
	movl	-20(%rbp), %eax
	movl	%eax, sr(%rip)
	movl	-16(%rbp), %eax
	movl	%eax, sc(%rip)
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	jmp	.L19
.L18:
	call	__ctype_b_loc@PLT
	movq	(%rax), %rdx
	movl	-16(%rbp), %eax
	movslq	%eax, %rcx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rcx
	leaq	map(%rip), %rax
	addq	%rcx, %rax
	movzbl	(%rax), %eax
	movb	%al, -29(%rbp)
	movsbq	-29(%rbp), %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	movzwl	(%rax), %eax
	movzwl	%ax, %eax
	andl	$2048, %eax
	testl	%eax, %eax
	je	.L19
	movsbl	-29(%rbp), %eax
	subl	$48, %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	sr(%rip), %rdx
	movl	-20(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movsbl	-29(%rbp), %eax
	subl	$48, %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	sc(%rip), %rdx
	movl	-16(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	-16(%rbp), %eax
	movslq	%eax, %rdx
	movl	-20(%rbp), %eax
	cltq
	imulq	$1002, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
.L19:
	addl	$1, -16(%rbp)
.L17:
	movl	w(%rip), %eax
	cmpl	%eax, -16(%rbp)
	jl	.L20
	addl	$1, -20(%rbp)
.L16:
	movl	h(%rip), %eax
	cmpl	%eax, -20(%rbp)
	jl	.L21
	movl	$0, -12(%rbp)
	movl	$0, -24(%rbp)
	jmp	.L22
.L23:
	movl	-24(%rbp), %eax
	addl	$1, %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %ecx
	movl	-24(%rbp), %eax
	addl	$1, %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rsi
	leaq	sc(%rip), %rax
	movl	(%rsi,%rax), %esi
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdi
	leaq	sr(%rip), %rax
	movl	(%rdi,%rax), %eax
	movl	%eax, %edi
	call	bfs
	addl	%eax, -12(%rbp)
	addl	$1, -24(%rbp)
.L22:
	movl	-28(%rbp), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L23
	movl	-12(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.local	v.0
	.comm	v.0,4,4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
