Segmentation error for last row of grid

for(int i=0;i<(m*n);i++){

    for(int j=1;j<9;j++){

        if(pixel[i][j]==1){

            int element = (i+1) + count[j-1];

            add_edge(adj,(i+1),element);

        }

    }

}

Here input m=5, n=8 and a pixel array with 1-39 rows and 0-8 columns

This loop is working fine till 38th row and gives segmentation fault at 39th row. Please help