Alice lives on a flat planet that can be modeled as a square grid of size n × 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 land or water.

<image> An example planet with n = 5. It also appears in the first sample test.

Alice resides in land cell (r_1, c_1). She wishes to travel to land 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).

Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible.

To help Alice, you plan to create at most one tunnel between some two land 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.

For 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.

Input

The first line contains one integer n (1 ≤ n ≤ 50) — the width of the square grid.

The second line contains two space-separated integers r_1 and c_1 (1 ≤ r_1, c_1 ≤ n) — denoting the cell where Alice resides.

The third line contains two space-separated integers r_2 and c_2 (1 ≤ r_2, c_2 ≤ n) — denoting the cell to which Alice wishes to travel.

Each of the following n lines contains a string of n characters. The j-th character of the i-th such line (1 ≤ i, j ≤ n) is 0 if (i, j) is land or 1 if (i, j) is water.

It is guaranteed that (r_1, c_1) and (r_2, c_2) are land.

Output

Print 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).

Examples

Input


5
1 1
5 5
00001
11111
00111
00110
00110


Output


10


Input


3
1 3
3 1
010
101
010


Output


8

Note

In 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 = 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).

In 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 = 8.
import java.io.*;
import java.util.*;

public class Stack3 {

    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }

    static Reader sc = new Reader();
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    // static long mod = (long) (1e9 + 7);
    static Map<Integer, List<String>> listMap = new HashMap<>();
    static String str = "abcdefghijklmnopqrstuvwxyz";


    public static void main(String[] args) throws IOException {
        //subsequence("abcdefghij");

        /*
         * For integer input: int n=inputInt();
         * For long input: long n=inputLong();
         * For double input: double n=inputDouble();
         * For String input: String s=inputString();
         * Logic goes here
         * For printing without space: print(a+""); where a is a variable of any datatype
         * For printing with space: printSp(a+""); where a is a variable of any datatype
         * For printing with new line: println(a+""); where a is a variable of any datatype
         */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int r1 = sc.nextInt();
        int c1 = sc.nextInt();
        int r2 = sc.nextInt();
        int c2 = sc.nextInt();
        char[][] ar = new char[n + 1][n + 1];
        int[][] dp = new int[n + 2][n + 2];
        boolean[][] vis = new boolean[n + 2][n + 2];
        for (int i = 1; i <= n; i++) {
            String s = sc.next();
            char[] chars = s.toCharArray();
            for (int j = 1; j <= n; j++) {
                ar[i][j] = chars[j - 1];
                dp[i][j] = Integer.MAX_VALUE;
            }
        }
        List<int[]> a1 = new ArrayList<>();

        List<int[]> a2 = new ArrayList<>();

        getAns(ar, r1, c1, r2, c2, 0, 0, dp, vis, a1);
        dp = new int[n + 2][n + 2];
        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                dp[i][j] = Integer.MAX_VALUE;
            }
        }
        vis = new boolean[n + 2][n + 2];
        getAns(ar, r2, c2, r1, c1, 0, 0, dp, vis, a2);
        /*for (int[] a : a1) {
            System.out.println(Arrays.toString(a));
        }
        for (int[] a : a2) {
            System.out.println(Arrays.toString(a));
        }*/
        int min = Integer.MAX_VALUE;
       /* if(a1.size()==0||a2.size()==0){
            System.out.println(0);
            return;
        }*/
        for (int[] a : a1) {
            for (int[] b : a2) {
                min=Math.min((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]),min);
            }
        }
        System.out.println(min);
        /*for(int[] a: dp){
            System.out.println(Arrays.toString(a));
        }*/

        bw.flush();
        bw.close();


    }

    public static int getAns(char[][] ar, int i, int j, int r2, int c2, int prevr, int prevc, int[][] dp, boolean[][] vis, List<int[]> ls) {
        //  System.out.println(i+" "+j);
        if (i <= 0 || j <= 0 || i >= ar.length || j >= ar.length) {
            return Integer.MAX_VALUE;

        }
        if (i == r2 && j == c2) {
            int[] a = {i, j};
            ls.add(a);
            dp[i][j] = 0;
            return dp[i][j];
        }

        if (dp[i][j] != Integer.MAX_VALUE) {
            return dp[i][j];
        }

        if (ar[i][j] == '1') {
            vis[i][j] = true;
            if (prevr <= 0 || prevc <= 0 || prevr >= ar.length || prevc >= ar.length) {
                dp[i][j] = Integer.MAX_VALUE;

            } else {
                dp[i][j] = (prevr - r2) * (prevr - r2) + (prevc - c2) * (prevc - c2);
            }
            return dp[i][j];
        } else {
            vis[i][j] = true;
            int[] a = {i, j};
            ls.add(a);
            int a1 = dp[i + 1][j], a2 = dp[i][j + 1], a3 = dp[i - 1][j], a4 = dp[i][j - 1];
            if (!vis[i + 1][j])
                a1 = getAns(ar, i + 1, j, r2, c2, i, j, dp, vis, ls);
            if (!vis[i][j + 1])
                a2 = getAns(ar, i, j + 1, r2, c2, i, j, dp, vis, ls);
            if (!vis[i - 1][j])
                a3 = getAns(ar, i - 1, j, r2, c2, i, j, dp, vis, ls);
            if (!vis[i][j - 1])
                a4 = getAns(ar, i, j - 1, r2, c2, i, j, dp, vis, ls);
            int[] ar1 = {a1, a2, a3, a4};
            Arrays.sort(ar1);
            dp[i][j] = ar1[0];
            return dp[i][j];
        }
    }

    static void printSubSeqRec(String str, int n,
                               int index, String curr) {
        // base case
        if (index == n) {
            return;
        }
        if (curr != null && !curr.trim().isEmpty()) {
            listMap.putIfAbsent(curr.length(), new ArrayList<>());
            List<String> y = listMap.get(curr.length());
            y.add(curr);
            listMap.put(curr.length(), y);
            //System.out.println(curr);
        }
        for (int i = index + 1; i < n; i++) {
            curr += str.charAt(i);
            printSubSeqRec(str, n, i, curr);

            // backtracking
            curr = curr.substring(0, curr.length() - 1);
        }
    }

    // Generates power set in
    // lexicographic order.
    // Set to store all the subsequences
    static HashSet<String> st = new HashSet<>();

    // Function computes all the subsequence of an string
    static void subsequence(String str) {

        // Iterate over the entire string
        for (int i = 0; i < str.length(); i++) {

            // Iterate from the end of the string
            // to generate substrings
            for (int j = str.length(); j > i; j--) {
                String sub_str = str.substring(i, j);

                if (!st.contains(sub_str))
                    st.add(sub_str);

                // Drop kth character in the substring
                // and if its not in the set then recur
                for (int k = 1; k < sub_str.length() - 1; k++) {
                    StringBuffer sb = new StringBuffer(sub_str);

                    // Drop character from the string
                    sb.deleteCharAt(k);
                    if (!st.contains(sb)) ;
                    subsequence(sb.toString());
                }
            }
        }
    }

    static void printSubSeq(String str) {
        int index = -1;
        String curr = "";

        printSubSeqRec(str, str.length(), index, curr);
    }

    public static boolean checkcorrect(String s, int p) {
        if (p == 0) {
            int t = 0;
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (ch == '(') {
                    t++;
                } else if (t == 0) {
                    return false;
                } else {
                    t--;
                }
            }
            return t == 0;
        } else if (p < 0) {
            int t = 0;
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (ch == '(') {
                    t++;
                } else if (t > 0) {
                    t--;
                }
            }
            return t == 0;
        } else {
            int t = 0;
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (ch == '(') {
                    t++;
                } else if (t > 0) {
                    t--;
                } else {
                    t = -1;
                    break;
                }
            }
            return t >= 0;
        }
    }


    public static void dfs1(List<List<Integer>> g, boolean[] visited, Stack<Integer> stack, int num) {
        visited[num] = true;
        for (Integer integer : g.get(num)) {
            if (!visited[integer]) {
                dfs1(g, visited, stack, integer);
            }
        }
        stack.push(num);
    }

    public static void dfs2(List<List<Integer>> g, boolean[] visited, List<Integer> list, int num, int c,
                            int[] raj) {
        visited[num] = true;

        for (Integer integer : g.get(num)) {
            if (!visited[integer]) {
                dfs2(g, visited, list, integer, c, raj);
            }
        }
        raj[num] = c;
        list.add(num);

    }

    public static int inputInt() throws IOException {
        return sc.nextInt();
    }

    public static long inputLong() throws IOException {
        return sc.nextLong();
    }

    public static double inputDouble() throws IOException {
        return sc.nextDouble();
    }

    public static String inputString() throws IOException {
        return sc.readLine();
    }

    public static void print(String a) throws IOException {
        bw.write(a);
    }

    public static void printSp(String a) throws IOException {
        bw.write(a + " ");
    }

    public static void println(String a) throws IOException {
        bw.write(a + "\n");
    }


    public static long getAns(int[] ar, int c, long[][] dp, int i, int sign) {
        if (i < 0) {
            return 1;
        }

        if (c <= 0) {
            return 1;
        }

        dp[i][c] = Math.max(dp[i][c], Math.max(ar[i] * getAns(ar, c - 1, dp, i - 1, sign), getAns(ar, c, dp, i - 1, 1)));
        return dp[i][c];
    }


    public static long power(long a, long b, long c) {
        long ans = 1;
        while (b != 0) {
            if (b % 2 == 1) {
                ans = ans * a;
                ans %= c;
            }
            a = a * a;
            a %= c;
            b /= 2;
        }
        return ans;
    }

    public static long power1(long a, long b, long c) {
        long ans = 1;
        while (b != 0) {
            if (b % 2 == 1) {
                ans = multiply(ans, a, c);
            }
            a = multiply(a, a, c);
            b /= 2;

        }
        return ans;

    }

    public static long multiply(long a, long b, long c) {
        long res = 0;
        a %= c;
        while (b > 0) {
            if (b % 2 == 1) {
                res = (res + a) % c;
            }
            a = (a + a) % c;
            b /= 2;
        }

        return res % c;

    }

    public static long totient(long n) {
        long result = n;
        for (long i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                //sum=sum+2*i;
                while (n % i == 0) {
                    n /= i;
                    // sum=sum+n;
                }
                result -= result / i;

            }
        }
        if (n > 1) {
            result -= result / n;

        }
        return result;
    }


    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        } else {
            return gcd(b, a % b);
        }
    }

    public int sumOfFlooredPairs(int[] nums) {
        // long sum=0;
        long mod = (long) (1e9 + 7);
        Arrays.sort(nums);
        long[] numerator = new long[nums.length];
        long[] denominator = new long[nums.length];

        long[] numerator1 = new long[nums.length];
        long[] denominator1 = new long[nums.length];

        long num = 1;
        long den = nums[0];
        numerator[0] = 1;
        denominator[0] = den;
        long num1 = 1;
        long den1 = nums[nums.length - 1];
        numerator1[nums.length - 1] = 1;
        denominator1[nums.length - 1] = den1;


        for (int i = 1; i < nums.length; i++) {

            long gcd1 = gcd(denominator[i - 1], nums[i]);
            long lcm = (denominator[i - 1] * nums[i]) / gcd1;
            long n1 = lcm / denominator[i - 1] * numerator[i - 1] + lcm / nums[i];
            numerator[i] = n1;
            denominator[i] = lcm;

            long gcd2 = gcd(denominator1[nums.length - i], nums[nums.length - 1 - i]);
            long lcm1 = (denominator1[nums.length - i] * nums[nums.length - 1 - i]) / gcd2;
            long n2 = lcm1 / denominator1[nums.length - i] * numerator1[nums.length - i] + lcm1 / nums[nums.length - 1 - i];
            numerator1[nums.length - 1 - i] = n2;
            denominator1[nums.length - 1 - i] = lcm1;




           /* prefix[i]=sum;
            sum12+=1d/nums[nums.length-1-i];
            prefix2[nums.length-1-i]=sum12;*/

        }
        /*System.out.println(Arrays.toString(numerator));
         System.out.println(Arrays.toString(denominator));*/
        int sum2 = 0;
        for (int i = 0; i < nums.length; i++) {
            //float d1=(float)(numerator[i]/(double)denominator[i]);
            sum2 += (int) (Math.floor((numerator[i] * nums[i]) / (double) denominator[i]));
            sum2 %= mod;
            if (i + 1 < nums.length) {
                // float d2=(float)(numerator1[i+1]/(double)denominator1[i+1]);
                sum2 += (int) (Math.floor((numerator1[i + 1] * nums[i]) / (double) denominator1[i + 1]));
                sum2 %= mod;
            }
        }
        return sum2;


    }

    public static boolean[] primes(int n) {
        boolean[] p = new boolean[n + 1];
        p[0] = false;
        p[1] = false;
        for (int i = 2; i <= n; i++) {
            p[i] = true;
        }

        for (int i = 2; i * i <= n; i++) {
            if (p[i]) {
                for (int j = i * i; j <= n; j += i) {
                    p[j] = false;
                }
            }

        }
        return p;
    }

    public String LargestEven(String S) {
        //  char[] ar=S.toCharArray();
        int[] count = new int[10];
        for (int i = 0; i < S.length(); i++) {
            count[S.charAt(i) - '0']++;
        }
        int num = -1;
        for (int i = 0; i <= 8; i += 2) {
            if (count[i] > 0) {
                num = i;
                break;
            }
        }
        StringBuilder ans = new StringBuilder();
        for (int i = 9; i >= 0; i--) {
            if (i != num) {
                for (int j = 1; j <= count[i]; j++) {
                    ans.append(i);
                }
            } else {
                for (int j = 1; j <= count[num] - 1; j++) {
                    ans.append(num);
                }

            }
        }
        if (num != -1 && count[num] > 0) {
            ans.append(num);
        }
        return ans.toString();
    }

}