would this work with just if j!=0: ?
Also would the meaning be clearer with a
if j==0:
  hash[i][j] = 1
else:
 hash[i][j] = hash[i][j-1]+1;