fix bugs in sample.py

d0 = min(h0, w0),d0 - w0 might be negative