{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eAlice lives on a flat planet that can be modeled as a square grid of size $$$n \\times n$$$, with rows and columns enumerated from $$$1$$$ to $$$n$$$. We represent the cell at the intersection of row $$$r$$$ and column $$$c$$$ with ordered pair $$$(r, c)$$$. Each cell in the grid is either \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e or \u003cspan class\u003d\"tex-font-style-it\"\u003ewater\u003c/span\u003e.\u003c/p\u003e\u003ccenter\u003e \u003cimg class\u003d\"tex-graphics\" src\u003d\"CDN_BASE_URL/f2415e746cbd0ef196e6f8ca6b13327f?v\u003d1714816040\" style\u003d\"max-width: 100.0%;max-height: 100.0%;\" width\u003d\"265px\"\u003e \u003cspan class\u003d\"tex-font-size-small\"\u003eAn example planet with $$$n \u003d 5$$$. It also appears in the first sample test.\u003c/span\u003e \u003c/center\u003e\u003cp\u003eAlice resides in \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e cell $$$(r_1, c_1)$$$. She wishes to travel to \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e cell $$$(r_2, c_2)$$$. At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right).\u003c/p\u003e\u003cp\u003eUnfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e). As a result, Alice\u0027s trip may be impossible.\u003c/p\u003e\u003cp\u003eTo help Alice, you plan to create \u003cspan class\u003d\"tex-font-style-bf\"\u003eat most one\u003c/span\u003e tunnel between some two \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells $$$(r_s, c_s)$$$ and $$$(r_t, c_t)$$$ is $$$(r_s-r_t)^2 + (c_s-c_t)^2$$$.\u003c/p\u003e\u003cp\u003eFor now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from $$$(r_1, c_1)$$$ to $$$(r_2, c_2)$$$. If no tunnel needs to be created, the cost is $$$0$$$.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line contains one integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the width of the square grid.\u003c/p\u003e\u003cp\u003eThe second line contains two space-separated integers $$$r_1$$$ and $$$c_1$$$ ($$$1 \\leq r_1, c_1 \\leq n$$$) — denoting the cell where Alice resides.\u003c/p\u003e\u003cp\u003eThe third line contains two space-separated integers $$$r_2$$$ and $$$c_2$$$ ($$$1 \\leq r_2, c_2 \\leq n$$$) — denoting the cell to which Alice wishes to travel.\u003c/p\u003e\u003cp\u003eEach of the following $$$n$$$ lines contains a string of $$$n$$$ characters. The $$$j$$$-th character of the $$$i$$$-th such line ($$$1 \\leq i, j \\leq n$$$) is \u003cspan class\u003d\"tex-font-style-tt\"\u003e0\u003c/span\u003e if $$$(i, j)$$$ is \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e or \u003cspan class\u003d\"tex-font-style-tt\"\u003e1\u003c/span\u003e if $$$(i, j)$$$ is \u003cspan class\u003d\"tex-font-style-it\"\u003ewater\u003c/span\u003e.\u003c/p\u003e\u003cp\u003eIt is guaranteed that $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are \u003cspan class\u003d\"tex-font-style-it\"\u003eland\u003c/span\u003e.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003ePrint an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from $$$(r_1, c_1)$$$ to $$$(r_2, c_2)$$$.\u003c/p\u003e"}},{"title":"Examples","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5\n1 1\n5 5\n00001\n11111\n00111\n00110\n00110\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e10\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3\n1 3\n3 1\n010\n101\n010\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e8\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eIn the first sample, a tunnel between cells $$$(1, 4)$$$ and $$$(4, 5)$$$ should be created. The cost of doing so is $$$(1-4)^2 + (4-5)^2 \u003d 10$$$, which is optimal. This way, Alice could walk from $$$(1, 1)$$$ to $$$(1, 4)$$$, use the tunnel from $$$(1, 4)$$$ to $$$(4, 5)$$$, and lastly walk from $$$(4, 5)$$$ to $$$(5, 5)$$$.\u003c/p\u003e\u003cp\u003eIn the second sample, clearly a tunnel between cells $$$(1, 3)$$$ and $$$(3, 1)$$$ needs to be created. The cost of doing so is $$$(1-3)^2 + (3-1)^2 \u003d 8$$$.\u003c/p\u003e"}}]}