Go: package main

import (
	"fmt"
	"math"
)

type Position struct {
	X int64
	Y int64
}

func (one Position) Distance(another Position) int64 {
	return int64(math.Abs(float64(one.X-another.X)) + math.Abs(float64(one.Y-another.Y)))
}

type PersonPosition struct {
	Takayuki Position
	Kazuyuki Position
}

func (one PersonPosition) Distance() int64 {
	return one.Takayuki.Distance(one.Kazuyuki)
}

type Status struct {
	Person PersonPosition
	Turn   int64
}

type PQStatus []*Status

func (pq PQStatus) Len() int {
	return len(pq)
}
func (pq PQStatus) Less(i, j int) bool {
	if pq[i].Turn == pq[j].Turn {
		return pq[i].Person.Distance() < pq[j].Person.Distance()
	}
	return pq[i].Turn < pq[j].Turn
}

func (pq PQStatus) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PQStatus) Push(x interface{}) {
	item := x.(*Status)
	*pq = append(*pq, item)
}

func (pq *PQStatus) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

func solve(h, w int64) {
	var tx, ty, kx, ky int64
	fmt.Scan(&tx, &ty, &kx, &ky)
	tx--
	ty--
	kx--
	ky--
	m := make([][]int64, h)
	for i := range m {
		m[i] = make([]int64, w)
		for j := int64(0); j < w; j++ {
			fmt.Scan(&m[i][j])
		}
	}
	start := &Status{Person: PersonPosition{Takayuki: Position{X: tx, Y: ty}, Kazuyuki: Position{X: kx, Y: ky}}, Turn: 0}
	pq := make(PQStatus, 0)
	//heap.Init(&pq)
	//heap.Push(&pq, start)
	//pq.Push(start)
	pq = append(pq, start)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]int64, 0)
	ans := int64(101)
	for pq.Len() > 0 {
		//now := heap.Pop(&pq).(*Status)
		//now := pq.Pop().(*Status)
		now := pq[0]
		pq = pq[1:]
		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}
		if ans <= now.Turn {
			continue
		}
		if v, ok := memo[now.Person]; ok && v <= now.Turn {
			continue
		}
		memo[now.Person] = now.Turn
		for i := 0; i < 4; i++ {
			next := &Status{}
			*next = *now
			next.Person.Kazuyuki.X += dx[i]
			next.Person.Kazuyuki.Y += dy[i]
			if next.Person.Kazuyuki.X < 0 || next.Person.Kazuyuki.X >= w ||
				next.Person.Kazuyuki.Y < 0 || next.Person.Kazuyuki.Y >= h ||
				m[next.Person.Kazuyuki.Y][next.Person.Kazuyuki.X] == 1 {
				next.Person.Kazuyuki.X -= dx[i]
				next.Person.Kazuyuki.Y -= dy[i]
			}
			next.Person.Takayuki.X -= dx[i]
			next.Person.Takayuki.Y -= dy[i]
			if next.Person.Takayuki.X < 0 || next.Person.Takayuki.X >= w ||
				next.Person.Takayuki.Y < 0 || next.Person.Takayuki.Y >= h ||
				m[next.Person.Takayuki.Y][next.Person.Takayuki.X] == 1 {
				next.Person.Takayuki.X += dx[i]
				next.Person.Takayuki.Y += dy[i]
			}
			next.Turn += 1
			if next.Turn < 100 {
				//heap.Push(&pq, next)
				//pq.Push(next)
				pq = append(pq, next)
			}
		}
	}
	if ans == 101 {
		fmt.Println("NA")
	} else {
		fmt.Println(ans)
	}
}

func main() {
	var h, w int64
	for {
		fmt.Scan(&h, &w)
		if h == 0 && w == 0 {
			break
		}
		solve(h, w)
	}
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
typedef long long ll;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int X, Y;
int TX, TY, KX, KY;
int field[64][64];
int done[52][52][52][52];

int main(void) {
    while (1) {
        cin >> X >> Y;
        if (X == 0 && Y == 0) break;
        cin >> TX >> TY;
        cin >> KX >> KY;
        for (int i = 0; i <= X; i++) {
            for (int j = 0; j <= Y; j++) {
                for (int k = 0; k <= X; k++) {
                    for (int l = 0; l <= Y; l++) {
                        done[i][j][k][l] = -1;
                    }
                }
            }
        }
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) {
                cin >> field[i][j];
            }
        }
        queue<int> tX;
        queue<int> tY;
        queue<int> kX;
        queue<int> kY;
        tX.push(TX);
        tY.push(TY);
        kX.push(KX);
        kY.push(KY);
        done[TX][TY][KX][KY] = 0;
        bool flag = false;
        while (!tX.empty()) {
            int tx = tX.front(); tX.pop();
            int ty = tY.front(); tY.pop();
            int kx = kX.front(); kX.pop();
            int ky = kY.front(); kY.pop();
            for (int i = 0; i < 4; i++) {
                int ntx = tx + dx[i];
                int nty = ty + dy[i];
                int nkx = kx - dx[i];
                int nky = ky - dy[i];
                if (ntx < 1 || ntx > X || nty < 1 || nty > Y) {
                    ntx = tx; nty = ty;
                } else if (field[nty][ntx] == 1) {
                    ntx = tx; nty = ty;
                }
                if (nkx < 1 || nkx > X || nky < 1 || nky > Y) {
                    nkx = kx; nky = ky;
                } else if (field[nky][nkx] == 1) {
                    nkx = kx; nky = ky;
                }
                if (nkx == ntx && nty == nky) {
                    cout << done[tx][ty][kx][ky] + 1 << endl;
                    flag = true;
                    break;
                }
                if (done[ntx][nty][nkx][nky] < 0) {
                    done[ntx][nty][nkx][nky] = done[tx][ty][kx][ky] + 1;
                    tX.push(ntx);
                    tY.push(nty);
                    kX.push(nkx);
                    kY.push(nky);
                }
            }
            if (flag) break;
        }
        if (!flag) cout << "NA" << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int dist[52][52][52][52];
int fie[52][52];


int W, H;
int tx, ty;
int kx, ky;

struct A {
  int a;
  int b;
  int c;
  int d;
};

int& acc(A a) {
  return dist[a.a][a.b][a.c][a.d];
}

int dd[] = {1, 0, -1, 0};

A mv(A a, int d) {
  A b = a;
  if(fie[a.a + dd[d]][a.b + dd[d ^ 1]] == 0) {
    b.a = a.a + dd[d];
    b.b = a.b + dd[d ^ 1];
  }
  if(fie[a.c + dd[d ^ 2]][a.d + dd[d ^ 3]] == 0) {
    b.c = a.c + dd[d ^ 2];
    b.d = a.d + dd[d ^ 3];
  }
  return b;
}

int solve() {
  cin >> W >> H;
  if(W == 0 && H == 0) return 0;
  cin >> tx >> ty;
  swap(tx, ty);
  cin >> kx >> ky;
  swap(kx, ky);
  for(int i = 0;i <= H + 1;i++) for(int j = 0;j <= W + 1;j++) fie[i][j] = 1;
  for(int i = 1;i <= H;i++) for(int j = 1;j <= W;j++) cin >> fie[i][j];

  for(auto& w: dist) for(auto& x: w) for(auto& y: x) for(auto& z: y) z = -1;

  queue<A> que;
  que.push({tx, ty, kx, ky});
  acc({tx, ty, kx, ky}) = 0;

  while(!que.empty()) {
    A v = que.front();
    if(v.a == v.c && v.b == v.d) {
      cout << acc(v) << endl;
      return 1;
    }
    que.pop();
    for(int i = 0;i < 4;i++) {
      A u = mv(v, i);
      if(acc(u) == -1) {
        acc(u) = acc(v) + 1;
        que.push(u);
      }
    }
  }
  cout << "NA" << endl;
  return 1;
}

int main() {
  while(solve());
}