Language: Java

import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
  public int M;
  public int N;
    public static void main(String[] args) throws java.io.IOException{
        Scanner scan = new Scanner(System.in);
         
        while(true){
            M = scan.nextInt();
            N = scan.nextInt();
            if(N==0 && M ==0)break;
            int ans =0;
            char [][] field =new char[M][N];
            for(int i=0;i<M;i++){
                for(int j=0;j<N;j++){
                    field[i][j] = (char)(scan.nextInt());
                }
                scan.skip("\n");
            }
 
            for(int i=0;i<M;i++){
                for(int j=0;j<N;j++){
                    if(field[i][j] != '.'){
                        dfs(i,j,field);
                        ans++;
                    }
                }
            }
            System.out.println(ans);
    }
 
 
}
  public static void dfs(int x,int y,char [][] field){
      char crop = field[y][x];
      field[y][x] = '.';
      int  [] dx= {1,0,-1,0};
       
      for(int i=0;i<3;i++){
          int nx = x+dx[i];
          int ny = y + dx[i]^1;
          if((0<=nx )&& (nx < N) &&(0<=ny )&& (ny <M)  &&( field[ny][nx]==crop)){
              dfs(ny,nx,field);
          }
      }
  }
}

Compiler output:
Main.java:11: error: non-static variable M cannot be referenced from a static context
            M = scan.nextInt();
            ^
Main.java:12: error: non-static variable N cannot be referenced from a static context
            N = scan.nextInt();
            ^
Main.java:13: error: non-static variable N cannot be referenced from a static context
            if(N==0 && M ==0)break;
               ^
Main.java:13: error: non-static variable M cannot be referenced from a static context
            if(N==0 && M ==0)break;
                       ^
Main.java:15: error: non-static variable M cannot be referenced from a static context
            char [][] field =new char[M][N];
                                      ^
Main.java:15: error: non-static variable N cannot be referenced from a static context
            char [][] field =new char[M][N];
                                         ^
Main.java:16: error: non-static variable M cannot be referenced from a static context
            for(int i=0;i<M;i++){
                          ^
Main.java:17: error: non-static variable N cannot be referenced from a static context
                for(int j=0;j<N;j++){
                              ^
Main.java:23: error: non-static variable M cannot be referenced from a static context
            for(int i=0;i<M;i++){
                          ^
Main.java:24: error: non-static variable N cannot be referenced from a static context
                for(int j=0;j<N;j++){
                              ^
Main.java:44: error: non-static variable N cannot be referenced from a static context
          if((0<=nx )&& (nx < N) &&(0<=ny )&& (ny <M)  &&( field[ny][nx]==crop)){
                              ^
Main.java:44: error: non-static variable M cannot be referenced from a static context
          if((0<=nx )&& (nx < N) &&(0<=ny )&& (ny <M)  &&( field[ny][nx]==crop)){
                                                   ^
12 errors