I think you're missing one more row in your Similarity Matrix. Cause you don't get your last document covered. Mine is looking like this.
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] NA NA NA NA NA NA NA NA NA
[2,] 1 NA NA NA NA NA NA NA NA
[3,] 2 3 NA NA NA NA NA NA NA
[4,] 3 2 3 NA NA NA NA NA NA
[5,] 1 2 2 1 NA NA NA NA NA
[6,] 2 1 2 3 0 NA NA NA NA
[7,] 2 1 2 2 1 2 NA NA NA
[8,] 1 1 2 1 1 1 2 NA NA
[9,] 2 2 3 3 1 2 1 1 NA
To get this result I did following steps.
mat=as.data.frame(as.matrix(dtm)) # you get the dataframe from DocumentTerm Matrix
rowCount <- nrow(dtm)
colCount <- ncol(dtm)
similMatrix = matrix(nrow = rowCount, ncol = rowCount)
similMatrix[ row(similMatrix) >= col(similMatrix) ] <- 0
for(i in 1:(rowCount)){ #set all columns NA you can change to zeros if you need later
similMatrix[i,i]=NA
} # then we will do the actual job
for(i in 1:rowCount ){ # rows
for (j in 1:rowCount ){ # cols
if(is.na(similMatrix[i,j])==F){
a=mat[i,]
b=mat[j,]
for(k in 1:colCount){ #n number of Cols in Document term matrix
if(a[k]==1 && a[k]==b[k]){
similMatrix[i,j]=similMatrix[i,j]+1
}
}
}
}
}