reserve i,j,k,k1,k2,n,m,i1,i2,j1,j2 for Element of NAT,
  x for set;
reserve S for set;

theorem
  for Y being Subset of S,F being Matrix of n,m, bool S st (ex i,j st i
in Seg n & j in Seg m & F*(i,j) c= Y) & (for i1,j1,i2,j2 st i1 in Seg n & i2 in
Seg n & j1 in Seg m & j2 in Seg m & i1,j1,i2,j2 are_adjacent holds F*(i1,j1)c=
  Y iff F*(i2,j2)c=Y) holds for i,j st i in Seg n & j in Seg m holds F*(i,j)c=Y
proof
  let Y be Subset of S,F be Matrix of n,m, (bool S);
  assume that
A1: ex i,j st i in Seg n & j in Seg m & F*(i,j) c= Y and
A2: for i1,j1,i2,j2 st i1 in Seg n & i2 in Seg n & j1 in Seg m & j2 in
  Seg m & i1,j1,i2,j2 are_adjacent holds F*(i1,j1)c=Y iff F*(i2,j2)c=Y;
  consider i1,j1 such that
A3: i1 in Seg n and
A4: j1 in Seg m and
A5: F*(i1,j1) c= Y by A1;
A6: j1<=m by A4,FINSEQ_1:1;
  1<=i1 by A3,FINSEQ_1:1;
  then i1-1>=1-1 by XREAL_1:9;
  then
A7: i1-'1=i1-1 by XREAL_0:def 2;
  1<=j1 by A4,FINSEQ_1:1;
  then j1-1>=1-1 by XREAL_1:9;
  then
A8: j1-'1=j1-1 by XREAL_0:def 2;
A9: i1<=n by A3,FINSEQ_1:1;
  thus for i,j st i in Seg n & j in Seg m holds F*(i,j)c=Y
  proof
    let i2,j2;
    assume that
A10: i2 in Seg n and
A11: j2 in Seg m;
A12: j2<=m by A11,FINSEQ_1:1;
    1<=i2 by A10,FINSEQ_1:1;
    then i2-1>=1-1 by XREAL_1:9;
    then
A13: i2-'1=i2-1 by XREAL_0:def 2;
    1<=j2 by A11,FINSEQ_1:1;
    then j2-1>=1-1 by XREAL_1:9;
    then
A14: j2-'1=j2-1 by XREAL_0:def 2;
A15: i2<=n by A10,FINSEQ_1:1;
    now
      per cases;
      case
        n=0 or m=0;
        hence contradiction by A3,A4;
      end;
      case
A16:    n<>0 & m<>0;
        then m>=0+1 by NAT_1:13;
        then m-1>=0 by XREAL_1:19;
        then
A17:    m-'1=m-1 by XREAL_0:def 2;
        then
A18:    j1-'1<=m-'1 & j2-'1<=m-'1 by A6,A8,A12,A14,XREAL_1:9;
        n>=0+1 by A16,NAT_1:13;
        then n-1>=0 by XREAL_1:19;
        then
A19:    n-'1=n-1 by XREAL_0:def 2;
        then i1-'1<=n-'1 & i2-'1<=n-'1 by A9,A7,A15,A13,XREAL_1:9;
        then consider fs1,fs2 being FinSequence of NAT such that
A20:    for i,k1,k2 st i in dom fs1 & k1=fs1.i & k2=fs2.i holds k1 <=
        n-'1 & k2 <= m-'1 and
A21:    fs1.1=i1-'1 and
A22:    fs1.(len fs1)=i2-'1 and
A23:    fs2.1=j1-'1 and
A24:    fs2.(len fs2)=j2-'1 and
A25:    len fs1=len fs2 and
A26:    len fs1=((i1-'1)-'(i2-'1))+((i2-'1)-'(i1-'1)) +((j1-'1)-'(j2
        -'1))+( (j2-'1)-'(j1-'1))+1 and
A27:    for i st 1<=i & i<len fs1 holds fs1/.i,fs2/.i,fs1/.(i+1),fs2
        /.(i+1) are_adjacent by A18,Th7;
        deffunc F(Nat) = (fs1/.$1)+1;
        ex p being FinSequence of NAT st len p = len fs1 & for j be Nat
        st j in dom p holds p.j = F(j) from FINSEQ_2:sch 1;
        then consider p1 being FinSequence of NAT such that
A28:    len p1 = len fs1 and
A29:    for k be Nat st k in dom p1 holds p1.k=(fs1/.k)+1;
        deffunc F(Nat) = (fs2/.$1)+1;
        ex p being FinSequence of NAT st len p = len fs2 & for k be Nat
        st k in dom p holds p.k=F(k) from FINSEQ_2:sch 1;
        then consider p2 being FinSequence of NAT such that
A30:    len p2 = len fs2 and
A31:    for k be Nat st k in dom p2 holds p2.k=(fs2/.k)+1;
A32:    dom p2 = Seg len fs2 by A30,FINSEQ_1:def 3;
        defpred P[Nat] means $1+1<=len p1 implies F*(p1/.($1+1),p2
        /.($1+1)) c= Y;
A33:    dom p1 = Seg len fs1 by A28,FINSEQ_1:def 3;
A34:    for k being Nat st P[k] holds P[k+1]
        proof
          let k be Nat;
A35:      1<=k+1 by NAT_1:12;
          assume
A36:      k+1<=len p1 implies F*(p1/.(k+1),p2/.(k+1)) c= Y;
          now
            per cases;
            case
A37:          k+1<=len p1;
              now
                per cases;
                case
A38:              k+1+1<=len p1;
                  set lp11=fs1/.(k+1+1),lp21=fs2/.(k+1+1);
                  1<=k+1+1 by NAT_1:12;
                  then
A39:              k+1+1 in Seg len p1 by A38,FINSEQ_1:1;
                  then k+1+1 in dom fs2 by A25,A28,FINSEQ_1:def 3;
                  then
A40:              fs2/.(k+1+1)=fs2.(k+1+1) by PARTFUN1:def 6;
A41:              k+1+1 in dom fs1 by A28,A39,FINSEQ_1:def 3;
                  then
A42:              fs1/.(k+1+1)=fs1.(k+1+1) by PARTFUN1:def 6;
                  then lp11<=n-1 by A19,A20,A41,A40;
                  then
A43:              lp11+1<=n-1+1 by XREAL_1:6;
                  k+1+1 in dom p2 by A25,A28,A30,A39,FINSEQ_1:def 3;
                  then p2.(k+1+1)=p2/.(k+1+1) by PARTFUN1:def 6;
                  then
A44:              p2/.(k+1+1)=lp21+1 by A25,A28,A31,A32,A39;
                  lp21<=m-'1 by A20,A41,A42,A40;
                  then
A45:              lp21+1<=m-1+1 by A17,XREAL_1:6;
                  1<=1+lp21 by NAT_1:11;
                  then
A46:              p2/.(k+1+1) in Seg m by A44,A45,FINSEQ_1:1;
                  k+1+1 in dom p1 by A39,FINSEQ_1:def 3;
                  then
A47:              p1/.(k+1+1)=p1.(k+1+1) by PARTFUN1:def 6
                    .=lp11+1 by A28,A29,A33,A39;
                  set lp1=fs1/.(k+1),lp2=fs2/.(k+1);
A48:              k+1<len p1 by A38,NAT_1:13;
                  then
A49:              k+1 in Seg len p1 by A35,FINSEQ_1:1;
                  then k+1 in dom fs2 by A25,A28,FINSEQ_1:def 3;
                  then
A50:              lp2=fs2.(k+1) by PARTFUN1:def 6;
                  k+1 in dom p1 by A49,FINSEQ_1:def 3;
                  then
A51:              p1/.(k+1)=p1.(k+1) by PARTFUN1:def 6
                    .=lp1+1 by A28,A29,A33,A49;
A52:              k+1 in dom fs1 by A28,A49,FINSEQ_1:def 3;
                  then
A53:              lp1=fs1.(k+1) by PARTFUN1:def 6;
                  then lp1<=n-1 by A19,A20,A52,A50;
                  then
A54:              lp1+1<=n-1+1 by XREAL_1:6;
                  1<=1+lp1 by NAT_1:11;
                  then
A55:              p1/.(k+1) in Seg n by A51,A54,FINSEQ_1:1;
                  k+1 in dom p2 by A25,A28,A30,A49,FINSEQ_1:def 3;
                  then
A56:              p2/.(k+1)=p2.(k+1) by PARTFUN1:def 6
                    .=lp2+1 by A25,A28,A31,A32,A49;
                  lp2<=m-'1 by A20,A52,A53,A50;
                  then
A57:              lp2+1<=m-1+1 by A17,XREAL_1:6;
                  1<=1+lp2 by NAT_1:11;
                  then
A58:              p2/.(k+1) in Seg m by A56,A57,FINSEQ_1:1;
                  1<=1+lp11 by NAT_1:11;
                  then
A59:              p1/.(k+1+1) in Seg n by A47,A43,FINSEQ_1:1;
                  k+1+1 in dom p2 by A25,A28,A30,A39,FINSEQ_1:def 3;
                  then p2/.(k+1+1)=p2.(k+1+1) by PARTFUN1:def 6
                    .=lp21+1 by A25,A28,A31,A32,A39;
                  then p1/.(k+1),p2/.(k+1),p1/.(k+1+1),p2/.(k+1+1)
                  are_adjacent by A27,A28,A35,A48,A51,A56,A47,Th3;
                  hence thesis by A2,A36,A37,A55,A58,A59,A46;
                end;
                case
                  k+1+1>len p1;
                  hence thesis;
                end;
              end;
              hence thesis;
            end;
            case
              k+1>len p1;
              hence thesis by NAT_1:13;
            end;
          end;
          hence thesis;
        end;
A60:    1<=len fs1 by A26,NAT_1:11;
        then
A61:    1 in Seg len fs1 by FINSEQ_1:1;
        then 1 in dom fs2 by A25,FINSEQ_1:def 3;
        then
A62:    fs2/.1=j1-'1 by A23,PARTFUN1:def 6;
        1 in dom p2 by A25,A30,A61,FINSEQ_1:def 3;
        then
A63:    p2/.1=p2.1 by PARTFUN1:def 6
          .=j1-'1 +1 by A25,A31,A32,A61,A62
          .=j1 by A8;
        1 in dom fs1 by A61,FINSEQ_1:def 3;
        then
A64:    fs1/.1=i1-'1 by A21,PARTFUN1:def 6;
        1 in dom p1 by A28,A61,FINSEQ_1:def 3;
        then p1/.1=p1.1 by PARTFUN1:def 6
          .=i1-'1 +1 by A29,A33,A61,A64
          .= i1 by A7;
        then
A65:    P[0] by A5,A63;
A66:    for i being Nat holds P[i] from NAT_1:sch 2(A65,A34);
        1-1<=len fs1-1 by A60,XREAL_1:9;
        then len fs1 -'1=len fs1 -1 by XREAL_0:def 2;
        then
A67:    len fs1 -'1+1=len fs1;
A68:    len fs1 in Seg len fs1 by A60,FINSEQ_1:1;
        then len fs1 in dom fs2 by A25,FINSEQ_1:def 3;
        then
A69:    fs2/.len fs1=j2-'1 by A24,A25,PARTFUN1:def 6;
        len fs1 in dom p1 by A28,A68,FINSEQ_1:def 3;
        then
A70:    (p1/.len fs1)=p1.(len fs1) by PARTFUN1:def 6
          .= (fs1/.len fs1)+1 by A29,A33,A68;
        len fs1 in dom fs1 by A68,FINSEQ_1:def 3;
        then
A71:    fs1/.len fs1=i2-'1 by A22,PARTFUN1:def 6;
        len fs1 in dom p2 by A25,A30,A68,FINSEQ_1:def 3;
        then (p2/.len fs1)=p2.(len fs1) by PARTFUN1:def 6
          .= (fs2/.len fs1)+1 by A25,A31,A32,A68;
        hence thesis by A13,A14,A28,A66,A67,A70,A71,A69;
      end;
    end;
    hence thesis;
  end;
end;
