### constant

MAX_INT = 1 << 30

### subroutines

def stat2n(id, cids, ps)
  n = id
  for cid in cids
    n = (n << 7) | cid
  end
  for p0 in ps
    n = (n << 1) | (p0 ? 1 : 0)
  end
  n
end

def n2stat(n)
  id = ((n >> 24) & 127)
  cid0 = ((n >> 17) & 127)
  cid1 = ((n >> 10) & 127)
  cid2 = ((n >>  3) & 127)
  p0 = (((n >> 2) & 1) == 1)
  p1 = (((n >> 1) & 1) == 1)
  p2 = ((n & 1) == 1)

  [id, [cid0, cid1, cid2], [p0, p1, p2]]
end

### main

loop do
  h, w = gets.split.map(&:to_i)
  break if h == 0 && w == 0

  mtx = []
  h.times do
    mtx << gets.strip
  end
  #p mtx

  nc = np = 0
  cids = 3.times.map{0}
  pids = 3.times.map{0}
  ps = 3.times.map{false}

  sid = gid = nil

  grds = (h * w).times.map{0}

  for y in (0...h)
    for x in (0...w)
      id = y * w + x
      case mtx[y][x]
      when '#'
        grds[id] = -1
      when '@'
        sid = id
      when 'w'
        pids[np] = id
        ps[np] = true
        np += 1
        grds[id] = np
      when 'c'
        cids[nc] = id
        nc += 1
      when 'E'
        gid = id
      end
    end
  end
  #p grds
  #p [nc, cids]
  #p [np, pids, ps]

  stn = stat2n(sid, cids, ps)
  dists = Hash.new(MAX_INT)
  dists[stn] = 0
  q = [stn]

  min_dist = -1

  while ! q.empty?
    #p q.map{|n| n2stat(n) + [dists[n]]}

    un = q.min{|a, b| dists[a] <=> dists[b]}
    q.delete(un)

    uid, ucids, ups = n2stat(un)

    if uid == gid
      min_dist = dists[un]
      break
    end

    ud = dists[un]

    for did in [1, -w, -1, w]
      vid = uid + did
      gv = grds[vid]
      next if gv < 0 || (gv > 0 && ups[gv - 1])

      ci = ucids.index(vid)
      if ! ci.nil?
        vcids = ucids.clone
        vps = ups.clone

        while grds[vcids[ci] + did] >= 0 && vcids.index(vcids[ci] + did).nil?
          vcids[ci] += did
          if grds[vcids[ci]] > 0
            pi = grds[vcids[ci]] - 1
            if vps[pi]
              vps[pi] = false
              vcids[ci] = 0
              break
            end
          end
        end

        if vcids[ci] != ucids[ci]
          vn = stat2n(uid, vcids, vps)
          dists[vn] = ud
          q << vn
        end

        next
      end

      vn = stat2n(vid, ucids, ups)
      vd = dists[vn]
      if vd > ud + 1
        dists[vn] = ud + 1
        if vd >= MAX_INT
          q << vn
        end
      end
    end
  end

  p min_dist
end